Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: generating alternative time variables


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: generating alternative time variables
Date   Mon, 28 Oct 2002 18:47:26 -0000

Metcalfe, Paul

> I need to generate a time2 variable in my panel dataset 
> which corresponds to
> quarterly time periods around an "event" which occurs in 
> different months
> for each ID. My data looks like the following except for 
> Time2 which I am
> unable to generate. The panel is unbalanced so it doesn't 
> have observations
> for every month for every ID. 
> 
> e.g.
> ID	Time	Event	Time2
> 1	1	.	-1
> 1	2	.	-1
> 1	3	.	0
> 1	4	1	0
> 1	5	.	0
> 1	6	.	1
> 1	7	.	1
> 1	8	.	1
> 1	9	.	2
> 1	10	.	2
> 
> I've tried using explicit subscripting to do this using the 
> following syntax
> but Stata cascades the changes all the way rather than changing one
> observation like I want it to.
> 
> sort id time
> by id: gen time2=0 if event!=.
> by id: replace time2=0 if (time2[_n-1]==0)|(time2[_n+1]==0)
> by id: replace time2=-1 if
> (time2==.)*((time2[_n+1]==0)|(time2[_n+2]==0)|(time2[_n+3]==0))
> by id: replace time2=1 if
> (time2==.)*((time2[_n-1]==0)|(time2[_n-2]==0)|(time2[_n-3]==0))
> 
> etc...
> 
> Please can someone let me know how I can do this? Thanks in 
> advance for any
> help anyone can give me.

Spread the event to other times for same id: 

. bysort id : egen time2 = max(event * time) 

Various other -egen- functions would also 
do the job, on the understanding that the event 
occurs just once for each id. 

Centre on -time2- and scale to quarters: 

. replace time2 = round(time - time2,3) / 3 

Nick 
n.j.cox@durham.ac.uk 

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index