Dear all,
Suppose I have the following data
For each id, I want to count the number of consecutive observations and save the frequency as c1, c2, c3 and so on. Take id=A as an example, there are 3 consecutive 1s (from m1996-m1998) so c1 is 3. Similarly, from m2000-m2001, we have another variable c2 denoting 2 consecutive 1s. And the consecutive observations are from m2003-m2004, so c3=2. Any suggestion for how to do it?
Suppose I have the following data
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str2 id float(m1995 m1996 m1997 m1998 m1999 m2000 m2001 m2002 m2003 m2004 c1 c2 c3) "A" 0 1 1 1 0 1 1 0 1 1 3 2 2 "B" 0 0 0 1 1 1 0 1 . . 3 1 . "C" 0 1 1 1 1 1 0 1 1 0 5 2 . "D" 0 1 1 1 1 0 0 0 1 0 4 1 . end
Comment