Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Foreach loop - skip no observations

    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.





  • #2
    Well, if 20, 30, ..., 90 are the only possible values of agegroup, which seems likely given the code that created it, assuming you don't have people younger than 20 or older than 99 in your data, then the simple fix is to replace -foreach w of numlist 20 30 40 50 60 70 80 90 {- by

    Code:
    levelsof agegroup, local(wlist)
    foreach w of local wlist {
    Since at that point in the code, you have dropped observations so that you have only those with year == `y', this code will identify the surviving values of agegroup, ptu them in wlist, and then you iterate over those.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Well, if 20, 30, ..., 90 are the only possible values of agegroup, which seems likely given the code that created it, assuming you don't have people younger than 20 or older than 99 in your data, then the simple fix is to replace -foreach w of numlist 20 30 40 50 60 70 80 90 {- by

      Code:
      levelsof agegroup, local(wlist)
      foreach w of local wlist {
      Since at that point in the code, you have dropped observations so that you have only those with year == `y', this code will identify the surviving values of agegroup, ptu them in wlist, and then you iterate over those.
      thank you very much! that works perfectly!

      Comment

      Working...
      X