Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: AW: split single records into multiple episode records


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   st: AW: split single records into multiple episode records
Date   Mon, 26 Oct 2009 14:37:45 +0100

<> 

You may want to take a cue from this code. Are you sure the assignment of
"x" is consistent in your example, going from top to bottom?


*************
clear*

inp byte(id) str8 year x 				
1      20001       1
1      20043       0
2      19973       1
3      20011       1
4      19974       0
4	     20032       1
end

compress
list, noobs sepby(id) 

bys id (year):  /* 
*/ gen str8 begin="1997 1"  /* 
*/ if _n==1
rename year end

gen begin2=quarterly(begin, "YQ")
gen str8 end2=  /* 
*/ substr(end, 1, 4) + " " +  /* 
*/ substr(end,5,5)
gen end3=quarterly(end2, "YQ")
drop begin end end2

rename begin2 begin
rename end3 end

bys id (end):  /* 
*/ replace x=9  /* 
*/ if _n==1

list, noobs sepby(id)

levelsof id, loc(mylev)
su id, mean
loc numofid=`r(max)'

tempfile mytemp
save `mytemp', replace


clear*
set obs `numofid'

gen id=_n
gen x=1
gen begin=. 
gen end=.

append using `mytemp'

bys id (end): gen byte episode=_n
format begin end %tq

gen aux=end[_n-1]+1
bys id (episode):  /* 
*/ replace begin=aux /* 
*/  if _n!=1
drop aux
order id episode begin end x

list, noobs sepby(id)
*************



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von Marcus Casey
Gesendet: Montag, 26. Oktober 2009 01:47
An: [email protected]
Betreff: st: split single records into multiple episode records

Hi,

I have a  question that I hope somebody can help me with. I want to  
transform a single record into multiple records to analyze individual  
episodes with an spell specific id.


Specifically I have  panel data with the following form with year  
denoting year-quarter

id 	year 	   x 				
1      20001       1
1      20043       0
2      19973       1
3      20011       1
4     19974        0
4	20032       1


The year variable  denotes when an event occurred for the specific  
unit. I want to transform this data into
the following format (data starts in 19971 and assuming that only have  
non-left censored data):

id episode       begin		end				x
1	1           	  19971          20001		        9 (unknown)
1      2               20002		20043			1
1      3	          20044              .  (censored)	0
2      1               19971		19973			9
2      2               19974               .				1
3      1  		  19971         20011			9
3       2               20012             .				1
4      1		  19971		19974			9
4      2		  19981		20032			0
4	3		  20033		    .				1


Could someone help me out with this? Thanks.


Regards,
Marcus

Marcus Casey
Duke University
Durham NC, 27708


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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