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   "Albert Newton" <[email protected]>
To   [email protected]
Subject   Re: st: RE: How to construct a duration variable?
Date   Thu, 6 Apr 2006 16:25:57 -0700

Thank you all for replying.

The codes I am using to create the duration variable are:

tsset hhid period ;
spell brand ;
bysort _spell: gen duration = _N ;
drop _*  ;

It works perfect.

Albert




On 4/6/06, Nick Cox <[email protected]> wrote:
> 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
> [email protected]
>
> 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/
>

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