Home Forums Fillmissing: Fill Missing Values in Stata Variables How to fill missing values by companies or groups in Stata

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • Jahangir Khan
    Guest
    Post count: 118

    Hello Sir

    I want to fill missing values in a variable named prices with any previously available values of the same variable. However, the missing values should be filled within each company (ie my grouping variable is company). Further, I want to fill the missing values in chronological order, that is the current missing values should be filled with the available values in preceding days, not from the following days. I wonder how can I use the fillmissing program for this.

    Thanks for your cooperation.

    Attaullah Shah
    Keymaster
    Post count: 69

    First, you need to install the fillmissing program. Then you can use use the bys prefix with the company id variable. See the following example.

    *To download  
     ssc install fillmissing
    
    *Create a dummy data for use in our example
    
    clear 
     input id price month year
            123 1 1 2000
            123 . 2 2000
            123 . 3 2000
            123 1 4 2000
            123 . 5 2000
            123 2 6 2000
            123 2 7 2000
            456 . 1 2000
            456 . 2 2000
            456 1.5 3 2000
            456 3 4 2000
            456 . 5 2000
            456 . 6 2000
            456 3 7 2000
     end
    
    *Create a monthly date in Stata format
    gen mdate = ym(year, month) 
    format mdate %tm 
     
    *Fill the missing values within each company ID, using the mdate as a sorting variable.
    bys id (mdate): fillmissing price, with(previous) 
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.