A variation on Scott's theme is to run -tsspell- twice.
First we identify spells with positive -value- in every year:
egen id = group(seller product)
tsset id year
tsspell, cond(value > 0)
-tsspell- generates a sequence number variable, which gives
us a length for each spell:
egen length = max(_seq) , by(id _spell)
We are not interested in spells of length 1 or 2:
replace _spell = 0 if length < 3
Get rid of left-overs:
drop _seq _end
Now we just want the first positive value of -_spell-:
tsspell , fcond(_spell > 0) spell(longenough)
gen start = _seq == 1
drop _* longenough
Nick
[email protected]
[email protected]
> Try this:
>
> egen id = group(seller product)
> tsset id year
> tsspell value, cond(value>0)
> egen count = count(_spell) if _spell > 0 , by(id _spell)
> gen byte begin = .
> levels id, local(levels)
> foreach l of local levels {
> replace begin = cond(_seq == 1, ///
> cond(_seq[_n+1] > _seq , 1, . ///
> ) , .) if id == `l' & count == 3
> }
> drop _* count
>
> For example:
>
> +---------------------------------+
> | seller product year value |
> |---------------------------------|
> | 13 456 1988 0 |
> | 13 456 1989 0 |
> | 13 456 1990 9 |
> | 13 456 1991 0 |
> | 13 456 1992 2 |
> |---------------------------------|
> | 13 456 1993 6 |
> | 13 456 1994 7 |
> | 14 1 1988 1 |
> | 14 1 1989 2 |
> | 14 1 1990 3 |
> |---------------------------------|
> | 14 2 1991 4 |
> | 14 2 1992 5 |
> | 14 2 1993 0 |
> | 14 2 1994 0 |
> | 15 1 1989 0 |
> |---------------------------------|
> | 15 1 1990 1 |
> | 15 1 1991 2 |
> | 15 1 1992 3 |
> +---------------------------------+
>
> . do "C:\DOCUME~1\SCOTT~1.MER\LOCALS~1\Temp\STD07000000.tmp"
>
>
> +----------------------------------------------+
> | seller product year value id begin |
> |----------------------------------------------|
> | 13 456 1988 0 1 . |
> | 13 456 1989 0 1 . |
> | 13 456 1990 9 1 . |
> | 13 456 1991 0 1 . |
> | 13 456 1992 2 1 1 |
> | 13 456 1993 6 1 . |
> | 13 456 1994 7 1 . |
> |----------------------------------------------|
> | 14 1 1988 1 2 1 |
> | 14 1 1989 2 2 . |
> | 14 1 1990 3 2 . |
> |----------------------------------------------|
> | 14 2 1991 4 3 . |
> | 14 2 1992 5 3 . |
> | 14 2 1993 0 3 . |
> | 14 2 1994 0 3 . |
> |----------------------------------------------|
> | 15 1 1989 0 4 . |
> | 15 1 1990 1 4 1 |
> | 15 1 1991 2 4 . |
> | 15 1 1992 3 4 . |
> +----------------------------------------------+
>
*
* 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/