Hello,

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'"
}
I would need to run the same code, but across age groups rather than by birth cohorts:

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'"
}
I think the issue is that across datayears, I do not have all of the age groups, but I am not sure how to adjust the loop so that it skips the age groups in a given year for which there are no data?

Thank you in advance for your help.