Search code examples
statapanel-data

How to complete (fill) a panel dataset with a time variable that has a delta > 1?


Say I have a bi-yearly panel only with observations at odd years, such as

input id year var
       1 2011  23
       1 2013  12
       1 2015  11
       
       2 2011  44
       2 2013  42
       2 2015  13
end 

and I would like to fill up the missing even years. Here years 2012 and 2014 is missing for all ids.

input id year var
       1 2011  23
       1 2012   .
       1 2013  12
       1 2014   .
       1 2015  11
       
       2 2011  44
       2 2012   .
       2 2013  42
       2 2014   .
       2 2015  13
end 

  • I had a look at help expand but I am unsure that's what I need, since it does not take the by prefix.

  • As a background info, I need to fill up with even years to able to merge with another panel data-set conducted in even years only


Solution

  • You can set the panel id as id and the time variable as year and use tsfill:

    clear
    input id year var
           1 2011  23
           1 2013  12
           1 2015  11
           
           2 2011  44
           2 2013  42
           2 2015  13
    end 
    
    
    xtset id year
    tsfill
    

    If the min and max year is not constant across panels, you could look at the ,full option.

    
    . list
    
         +-----------------+
         | id   year   var |
         |-----------------|
      1. |  1   2011    23 |
      2. |  1   2012     . |
      3. |  1   2013    12 |
      4. |  1   2014     . |
      5. |  1   2015    11 |
         |-----------------|
      6. |  2   2011    44 |
      7. |  2   2012     . |
      8. |  2   2013    42 |
      9. |  2   2014     . |
     10. |  2   2015    13 |
         +-----------------+