Monday, January 16, 2012

How do I identify runs of consecutive observations in panel data?

The STATA mailing list has a way to identify runs of consecutive observations. With some googling, SAS can do the same thing. Here's how.


Suppose you want to figure out how many observations you have per GVKEY:


data merge2; set merge; by gvkey; cnt+1; if first.gvkey then cnt=1; run;

proc sort data=merge2; by gvkey descending cnt; run;

data merge3; set merge2; by gvkey; retain totcnt; if first.gvkey then totcnt=cnt; output; run;

No comments: