Statalist The Stata Listserver


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

RE: st: RE: How to construct a duration variable?


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: RE: How to construct a duration variable?
Date   Thu, 6 Apr 2006 21:54:02 +0100

The problem identified by Scott is that
Brand is a string variable, at least 
as created by him when setting up the 
example.  

This would be another way 

tsset hhid Period
tsspell, fcond(Switch == 1 | L.Period == .) 
bysort  hhid _spell : gen duration = _N

L.Period == . catches the first observation
in each panel. 

tsspell, fcond(Switch == 1 | _n == 1) 

would also work, but this hinges on knowing
how -tsspell- does the job. 

Nick 
n.j.cox@durham.ac.uk 

Scott Merryman
 
> A slight modification is necessary:
> 
> tsset hhid per
> encode b, gen(b2)
> tsspell b2
> bysort hhid _spell: replace _seq = _seq[_N]
> rename _seq duration
> drop _* b2
> 
> 
> -tsspell- creates _spell by:
> = by hhid: gen long _spell = sum((brand != L.brand) | (_n == 
> 1) & __000001)
> 
> while -spell- uses explicit subscripting.
 

*
*   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