I've been reading the posts on related questions on the forum but I am still somewhat stuck trying to figure this out - I am trying to adjust the loop below, but I keep getting the same error "no observations".
I am working on the following code:
Code:
global datayears "1965 1968 1971 1977 1983 1989 1992 1995 1998 2001 2004 2007 2010 2013 2016 2019" foreach y of numlist $datayears{ clear all use "data", clear keep if year ==`y' drop if ageh==0 gen birthyear=year-ageh gen birthcohort=floor(birthyear/10) order year sum birthcohort local i=r(min) local j=r(max) foreach w of numlist `i'/`j'{ preserve keep if birthcohort==`w' sum tinc [aw=wgtI95W95], d gen group=10 if tinc>r(p90) replace group=40 if tinc>r(p50) & tinc<=r(p90) replace group=50 if tinc<=r(p50) tabstat inheritances [aw=wgtI95W95], by(group) stats(sum) restore } di "`y'" }
Code:
global datayears "1965 1968 1971 1977 1983 1989 1992 1995 1998 2001 2004 2007 2010 2013 2016 2019" foreach y of numlist $datayears{ clear all use "data", clear keep if year ==`y' drop if ageh==0 gen birthyear=year-ageh gen birthcohort=floor(birthyear/10) by year birthcohort, sort: egen agemedian=median(ageh) gen agegroup=10*(round((agemedian-10)/10,1)+1) order year foreach w of numlist 20 30 40 50 60 70 80 90{ preserve keep if agegroup==`w' sum tinc [aw=wgtI95W95], d gen group=10 if tinc>r(p90) replace group=40 if tinc>r(p50) & tinc<=r(p90) replace group=50 if tinc<=r(p50) tabstat inheritances [aw=wgtI95W95], by(group) stats(sum) restore } di "`y'" }
Thank you in advance for your help.
0 Response to Foreach loop - skip no observations
Post a Comment