Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: generating duration-variable with multi-line spells


From   n j cox <[email protected]>
To   n j cox <[email protected]>
Subject   Re: st: generating duration-variable with multi-line spells
Date   Fri, 01 Dec 2006 18:18:03 +0000

bysort id _spell (_seq) : gen duration = end[_N] - start[1] + 1

n j cox wrote:

Jeph Herrin gave good answers to this. In addition,
note that -tsspell- from SSC purports to be a general tool in
this area. You would need to -tsset- the data first, but
that can be done via a pseudo-time variable:

bysort id (start) : gen t = _n
tsset id t

Then your spells are defined by -state- remaining
constant:

tsspell state

at which various variables -- _spell _seq _end -- spring
into being. Now your durations are just

bysort id _spell (_seq) : gen duration = end[_N] - start[_N] + 1

Now that's just re-inventing Jeph's logic, I imagine, but the help
file has several small examples for other problems, so that -tsspell- may help you in other ways.

Nick
[email protected]


Henrik Lindegaard Andersen asked

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?

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index