Although I've been trying for several hours, I'm afraid I can't solve this one by myself. To be more specific, I have a dataset with a rotational design for the period 2005-2015, where the sample of each year consists of four subsamples, one that has been selected for the specific year and three others that have been followed for 2, 3 and 4 years, respectively. Every subsample is dropped after a four-year follow-up. Hence, I have (at most) four-year observations for each person (person_id) for variable wstatus (working status) that may or may not change during this period.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float line int year long(hh_id person_id) byte wstatus 64965 2006 138460 13846001 2 64966 2007 138460 13846001 1 64967 2008 138460 13846001 1 64968 2009 138460 13846001 2 64969 2006 138460 13846002 4 64970 2007 138460 13846002 4 64971 2008 138460 13846002 4 64972 2009 138460 13846002 4 64973 2006 138470 13847001 6 64974 2007 138470 13847001 6 64975 2008 138470 13847001 6 64976 2009 138470 13847001 6 64977 2006 138470 13847002 7 64978 2007 138470 13847002 7 64979 2008 138470 13847002 7 64980 2009 138470 13847002 7 64981 2006 138470 13847003 5 64982 2007 138470 13847003 5 64983 2008 138470 13847003 2 64984 2009 138470 13847003 1 end
What I'm trying to do is to construct a loop which generates a new variable, say "trans", whose values depend on the change of the values of wstatus between each combination of two consecutive years. Hence, for the example above, and for person 13846001, the value of the said variable trans would be empty for 2006, while for 2007 would be based on the comparison of the values of wstatus for the years 2006 and 2007, for the year 2008 would be based on the comparison of the values of wstatus for the years 2007 and 2008, and for the year 2009 would be based on the comparison of the values of wstatus for the years 2008 and 2009. I don't think that the specific qualifiers are of much relevance, but for the sake of this example let's say that if wstatus==1 at t and wstatus==1 at t+1 for person i , then trans==100 at t+1 for the same person. I have another 10 combinations to consider.
I did come up with something, but it doesn't work obviously, as it changes the values of all observations for each person
Code:
gen trans=. by person_id (year), sort: gen yid = _n summarize yid, meanonly forval i= 1/`r(max)' { by person_id: replace trans=100 if wstatus[`i']==1 & wstatus[`i'+1]==1 }
I could really use your help!
Thank you in advance
Thanos
0 Response to Create loop based on multiple qualifiers and panel dataset
Post a Comment