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/