Generate single longest spell for each unit in panel data, listwise
onespell varlist [if exp] [in range] , saving(name) [ replace ]
onespell is for use with panel data. You must tsset your data before using onespell; see help tsset.
onespell produces a subset of a panel data set in which all observations on varlist are non-missing and contiguous in the time dimension. If a panel unit contains more than one such subset, the longest "spell" is retained.
For instance, if a panel contains up to 20 years of data per firm, and firm i contains non-missing values for all variables of interest for years 1-10 and 12-20, the first subset will be retained as that firm's single "spell" and years 12-20 will be discarded.
If there are two or more spells of equal length, the latest spell will be retained. If firm i has non-missing values for all variables of interest for years 1-6, 8-13, 15-20, the spell for years 15-20 will be retained.
Although performing this operation for a single variable of interest is fairly straightforward, this routine deals with a more challenging task: generating the single longest spell in a listwise fashion, across a set of variables. This applies the same logic as does, e.g., regress: an observation with a missing value in any variable will be deleted from the regression sample. Some matrix software (e.g., DPD for Ox) requires that panel data be organized in this fashion, with each unit appearing as a single contiguous block of non-missing observations. onespell will produce such a dataset, which after conversion to Stata version 6 format (by, e.g., Stat/Transfer) may be read directly by Ox. The routine has been tested with the latest version of Ox (3.40) and the DPD package (1.21).
saving(name) (a required option) causes the pruned dataset to be saved as name.dta. If that dataset already exists, the replace option must be used.
. webuse grunfeld, clear
. replace invest = . in 28 . replace mvalue = . in 55 . replace kstock = . in 87 . replace kstock = . in 94
. onespell invest mvalue kstock, saving(grun1)
Christopher F. Baum, Boston College, USA firstname.lastname@example.org
Most of the computations in this routine are generated by N.J. Cox's tsspell routine. Thanks to David Roodman for assistance with the issues of moving data between his xtabond2 routine and DPD for Ox.