Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

[no subject]



bys id (year): replace f=cond(event==1,0,f[_n-1]+year[_n]-year[_n-1])
(this is for the first of the two cases)

However, this assumes that the event didn't occur in the gaps. One can also 
change the code appropriately to assign missings, if a gap occured until the 
next event happens. And other options depending on what you are doing.

Regards, Sergiy


----- Original Message ----- 
From: "Christer Thrane" <[email protected]>
To: <[email protected]>
Sent: Thursday, March 08, 2007 9:12 PM
Subject: st: Re: Re: creating a "years since event" variable in panel data


> Thanks! I think I understand it. Does this also hold for the following 
> data structure?
> id    year    time    event
> 1    1994    0        0
> 1    1995    1        0
> 1    1996    2        0
> ..
> 1    2002    7        0
> ----
> 2    1994    0        0
> 2    1995    1        0
> 2    1996    2        1
> 2    1997    3        1
> ..
> 2     2002   7         1
> ----
> 3    1994    0        1
> 3    1995    1        1
> 3    1996    2        1
> ..
> 3    2002    7        1
> ...
>
>
>
> ----- Original Message ----- 
> From: "Sergiy Radyakin" <[email protected]>
> To: <[email protected]>
> Sent: Thursday, March 08, 2007 8:51 PM
> Subject: st: Re: creating a "years since event" variable in panel data
>
>
>> 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,
>>   Sergiy Radyakin
>>
>>
>>
>>
>>
>> ----- Original Message ----- 
>> From: "Christer Thrane" <[email protected]>
>> To: <[email protected]>
>> 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/
>
> *
> *   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