Using Nick Cox's -tsspell- (see also "How do I identify runs of consecutive
observations in panel data?" by Nick Cox and Vince Wiggins at:
http://www.stata.com/support/faqs/data/panel.html ) this seems to work:
egen id = group(seller product)
tsset id year
tsspell value, cond(value>0)
by id: gen byte begin = cond(_seq == 1, ///
cond(_seq[_n+1] > _seq , 1, ///
cond(_seq == 1 & _n ==1, 1, ///
cond(_seq ==1 & _n ==_N, 1, . ///
))) , .)
For example:
. l
+---------------------------------+
| seller product year value |
|---------------------------------|
1. | 13 456 1988 0 |
2. | 13 456 1989 0 |
3. | 13 456 1990 9 |
4. | 13 456 1991 0 |
5. | 13 456 1992 2 |
|---------------------------------|
6. | 13 456 1993 6 |
7. | 13 456 1994 7 |
8. | 14 1 1988 1 |
9. | 14 1 1989 2 |
10. | 14 1 1990 3 |
|---------------------------------|
11. | 14 2 1991 4 |
12. | 14 2 1992 0 |
13. | 14 2 1993 0 |
14. | 14 2 1994 0 |
15. | 15 1 1990 0 |
|---------------------------------|
16. | 15 1 1991 0 |
17. | 15 1 1992 1 |
+---------------------------------+
. do "C:\DOCUME~1\Scott_2\LOCALS~1\Temp\STD0h000000.tmp"
. egen id = group(seller product)
. tsset id year
panel variable: id, 1 to 4
time variable: year, 1988 to 1994
. tsspell value, cond(value>0)
.
. by id: gen byte begin = cond(_seq == 1, ///
> cond(_seq[_n+1] > _seq , 1, ///
> cond(_seq == 1 & _n ==1, 1, ///
> cond(_seq ==1 & _n ==_N, 1, . ///
> ))) , .)
(13 missing values generated)
.
end of do-file
. drop _*
. l , sepby(id)
+----------------------------------------------+
| seller product year value id begin |
|----------------------------------------------|
1. | 13 456 1988 0 1 . |
2. | 13 456 1989 0 1 . |
3. | 13 456 1990 9 1 . |
4. | 13 456 1991 0 1 . |
5. | 13 456 1992 2 1 1 |
6. | 13 456 1993 6 1 . |
7. | 13 456 1994 7 1 . |
|----------------------------------------------|
8. | 14 1 1988 1 2 1 |
9. | 14 1 1989 2 2 . |
10. | 14 1 1990 3 2 . |
|----------------------------------------------|
11. | 14 2 1991 4 3 1 |
12. | 14 2 1992 0 3 . |
13. | 14 2 1993 0 3 . |
14. | 14 2 1994 0 3 . |
|----------------------------------------------|
15. | 15 1 1990 0 4 . |
16. | 15 1 1991 0 4 . |
17. | 15 1 1992 1 4 1 |
+----------------------------------------------+
Hope this helps,
Scott
> -----Original Message-----
> From: [email protected] [mailto:owner-
> [email protected]] On Behalf Of Jason Hwang
> Sent: Monday, February 28, 2005 7:06 PM
> To: [email protected]
> Subject: st: Locating Point Fitting Criteria in Panel Data
>
> Dear Stata Users,
>
> May I ask somebody how to code the following?
>
> I have panel data of the form:
>
> 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
>
> In the real data I have millions of obs for many sellers, products and
> different years available for each seller-product.
>
> I would like to find for each seller-product, the first year of the first
> three year period where value is positive for all three of those years. In
> the above example, for seller 13, product 456, the year would be 1992.
>
> I can't seem to write the correct if statement to do what I want.
>
> Thanks very much for your help.
>
> Jason
> *
> * 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/