# st: Re: creating a "years since event" variable in panel data

 From "Sergiy Radyakin" To Subject st: Re: creating a "years since event" variable in panel data Date Thu, 8 Mar 2007 20:51:22 +0100

Hello Christer,

I assume you have data in the following form:
. l ,sepby(id)

+-------------------+
id year event
-------------------
1. 1 1997 0
2. 1 1998 0
3. 1 1999 1
4. 1 2000 0
5. 1 2001 0
6. 1 2002 1
7. 1 2003 0
8. 1 2004 0
-------------------
9. 2 1997 1
10. 2 1998 1
11. 2 1999 1
12. 2 2000 0
13. 2 2001 0
14. 2 2002 0
15. 2 2003 0
16. 2 2004 0
-------------------
17. 3 1997 0
18. 3 1998 0
19. 3 1999 0
20. 3 2000 0
21. 3 2001 0
22. 3 2002 1
+-------------------+

(Namely, I don't understand what you mean by time ranging from 0 to 7)

Type:

gen d=.
bys id (year): replace d=cond(event==1,0,d[_n-1]+1)

l ,sepby(id)

Which results in the following:
+-----------------------+
id year event d
-----------------------
1. 1 1997 0 .
2. 1 1998 0 .
3. 1 1999 1 0
4. 1 2000 0 1
5. 1 2001 0 2
6. 1 2002 1 0
7. 1 2003 0 1
8. 1 2004 0 2
-----------------------
9. 2 1997 1 0
10. 2 1998 1 0
11. 2 1999 1 0
12. 2 2000 0 1
13. 2 2001 0 2
14. 2 2002 0 3
15. 2 2003 0 4
16. 2 2004 0 5
-----------------------
17. 3 1997 0 .
18. 3 1998 0 .
19. 3 1999 0 .
20. 3 2000 0 .
21. 3 2001 0 .
22. 3 2002 1 0
+-----------------------+

Notice that in this version it is "duration since the most recent occurance of the event". Alternatively it can be "duration since the first occurance of the event".
First is reasonable if your event is "got a new job" -- then d is tenure. Alternatively, if your event is something like "a child is born", -- then dd might be "duration of life with children" (though no account is given for possible move-outs). Then one will need to change the program:
gen dd=.

bys id (year): replace dd=cond(dd[_n-1]==.,cond(event==1,0,.),dd[_n-1]+1)

+----------------------------+
id year event d dd
----------------------------
1. 1 1997 0 . .
2. 1 1998 0 . .
3. 1 1999 1 0 0
4. 1 2000 0 1 1
5. 1 2001 0 2 2
6. 1 2002 1 0 3
7. 1 2003 0 1 4
8. 1 2004 0 2 5
----------------------------
9. 2 1997 1 0 0
10. 2 1998 1 0 1
11. 2 1999 1 0 2
12. 2 2000 0 1 3
13. 2 2001 0 2 4
14. 2 2002 0 3 5
15. 2 2003 0 4 6
16. 2 2004 0 5 7
----------------------------
17. 3 1997 0 . .
18. 3 1998 0 . .
19. 3 1999 0 . .
20. 3 2000 0 . .
21. 3 2001 0 . .
22. 3 2002 1 0 0
+----------------------------+

Best regards,

----- Original Message ----- From: "Christer Thrane" <lineea@broadpark.no>
To: <statalist@hsphsun2.harvard.edu>
Sent: Thursday, March 08, 2007 7:23 PM
Subject: st: creating a "years since event" variable in panel data

Hi,

I have a annual panel data set (1 obs per year), where ID identifies the person and the variable OBSYEAR the year of observation (1995=1, 1996=2, ..., 2002=7).

The variable TIME is the time dimension, ranging from 0 to 7.

The variable EVENT is a dymmy (1=event happened during the year in question; 0=event did not happen during the year in question).

Q: How do I create the variable NUMBER OF YEARS SINCE (A POSSIBLE) CHANGE FROM 0 to 1 on EVENT?

Best regards,

Christer Thrane
*
* 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/
```