, ,

Create balanced panels in Stata

Mainly when I merge spell data from different sources, I sometimes need to create a fully balanced panel such that I have one row for each individual and (eg) month (or any other time unit). Previously, I have written about using --expand-- which can be used to create balanced samples but now found a much easier and quicker way to do so.

Suppose you have a sample that has observations between two months (e.g. 2015m1 and 2018m12), but some individuals have no observations in the beginning, some not towards the end, and some somewhere in between. I would then like to create a balanced sample which contains months for each individual and month. Simply write:

xtset id month
tsfill, full

And Stata creates a fully balanced sample in which all individuals id have one observation for each month. Obviously, this requires that you can set your data to xtset and that there are no two observations for the same individual and month.