Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: counting the time of being in a specific state


From   Thomas Mählmann <maehlmann@wiso.uni-koeln.de>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: counting the time of being in a specific state
Date   Fri, 3 Sep 2004 14:15:49 +0200

Dear Statalisters,

I have a problem with counting the number of successive times a subject is
in a specific state. Consider the example given below.

At the beginning of year 1996 subject 101 is staying already for two years
in state 4, therefore my count variable should be 3.
At the beginning of 1997 it changes it state from 4 to 3 and stays within 3
for the year 1997, so the value of "count" should be 1. Note that even
though subject 103 is staying for two years (1994 and 1996) in state 4
altogether, this period is interrupted by a change to 2 in 1995, so the
count variable should be 1 for 1996 (and not 2)!


  id  state  year  count
 101   4     1994   1
 101   4     1995   2
 101   4     1996   3
 101   3     1997   1
 102   1     1994   1
 102   1     1995   2
 102   2     1996   1
 102   3     1997   1
 103   4     1994   1
 103   2     1995   1
 103   4     1996   1


How can I generate such a count variable? I have searched the archives and
the help for egen functions but without success.

Any help is highly appreciated.

Greetings,
Thomas

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index