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

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)