Henrik,
I assume you do not want to combine observations with the
same -id- and -state-, which would simplify the problem.
So how about:
gen newstart=start
bys id (start): replace newstart=newstart[_n-1] if state==state[_n-1]
bys id newstart: gen duration = end-newstart+1 if _n==_N
This produces:
+-----------------------------------------------------------+
| id start end state next_s~e newstart duration |
|-----------------------------------------------------------|
| 1 1 60 1 2 1 60 |
| 1 61 72 2 3 61 12 |
| 1 73 204 3 . 73 132 |
| 2 1 60 2 1 1 60 |
| 2 61 65 1 1 61 . |
| 2 66 67 1 1 61 . |
| 2 68 69 1 3 61 9 |
| 2 70 204 3 . 70 135 |
+-----------------------------------------------------------+
which I think is what you want.
hth,
Jeph
Henrik Lindegaard Andersen wrote:
Dear Statalisters,
I want to generate a variable that counts the duration in some state. My data
looks like this:
id start end state next_state
1 1 60 1 2
1 61 72 2 3
1 73 204 3 .
2 1 60 2 1
2 61 65 1 1
2 66 67 1 1
2 68 69 1 3
2 70 204 3 .
If the state does not change (as for id 2 from 61 to 69) I want the new
variable to be missing for all but one line. Therefore
gen duration = end - start + 1 if state != next_state
works from some of the spells. But what will work for the spells that are
split over several lines?
Regards
Henrik Lindegaard
Research Associate
AKF, Institute of Local Government Studies - Denmark
Nyropsgade 37, DK-1602 Copenhagen V
*
* 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/
*
* 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/