Announcement

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

  • Nothing to graph after loop

    Hi everyone,
    I'm working on ESS data and I want to graph the ESeC occupational class for all the 5 rounds I'm working on and for each country.
    Therefore, I coded:

    Code:
    levelsof cntry, local(c) clean
    levelsof essround, local(r)
    foreach cntry in `c' {
    foreach essround in `r' {
    graph hbar (percent) if cntry=="`cntry'" & essround==`essround' [aw=dweight], over (esec) by(gndr, note("") title("Respondents ESeC Class")) ytitle("Percent", size(small)) blabel(bar, position(inside) format(%9.1f) color(white))
    graph export "$path/graphs/`essround'_`cntry'.png", as(png) replace
    
    }
    }

    After exporting the graphs for first country (AT) it returns
    nothing to graph
    . I also tried with
    Code:
    levelsof cntry, local(c) clean
    levelsof essround, local(r)
    foreach cntry in `c' {
    foreach essround in `r' {
    keep if cntry=="`cntry'" & essround==`essround'
    graph hbar (percent) [aw=dweight], over (esec) by(gndr, note("") title("Respondents ESeC Class")) ytitle("Percent", size(small)) blabel(bar, position(inside) format(%9.1f) color(white))
    graph export "$path/graphs/`essround'_`cntry'.png", as(png) replace
    restore
    }
    }
    But still after the AT, it seems like the restore doesn't work and all observation are canceled.
    Any suggestion?
    Thank you
    Last edited by Luca Giangregorio; 17 Jan 2019, 11:12.

  • #2
    your -foreach- code is wrong in every case; you want instead:
    Code:
    foreach cntry of local c
    and similarly in the other case; please see
    Code:
    help levelsof
    for an example of using the results of -levelsof- with -foreach-

    Comment


    • #3
      Welcome to Statalist.

      For your loops to work, it must be the case that every combination of cntry and essround appears in your data. Perhaps this is not the case. Here are some changes to your code that might point you in a useful direction.
      Code:
      levelsof cntry, local(c) clean
      levelsof essround, local(r)
      foreach cntry in `c' {
      foreach essround in `r' {
      count if cntry=="`cntry'" & essround==`essround'
      if r(N)>0 {
      graph hbar (percent) if cntry=="`cntry'" & essround==`essround' [aw=dweight], over (esec) by(gndr, note("") title("Respondents ESeC Class")) ytitle("Percent", size(small)) blabel(bar, position(inside) format(%9.1f) color(white))
      graph export "$path/graphs/`essround'_`cntry'.png", as(png) replace
      }
      }
      }
      Also,
      Code:
      tabulate cntry essround
      will confirm or refute my assumption that some combination is missing.
      Last edited by William Lisowski; 17 Jan 2019, 11:21.

      Comment


      • #4
        Originally posted by William Lisowski View Post
        Welcome to Statalist.

        For your loops to work, it must be the case that every combination of cntry and essround appears in your data. Perhaps this is not the case. Here are some changes to your code that might point you in a useful direction.
        Code:
        levelsof cntry, local(c) clean
        levelsof essround, local(r)
        foreach cntry in `c' {
        foreach essround in `r' {
        count if cntry=="`cntry'" & essround==`essround'
        if r(N)>0 {
        graph hbar (percent) if cntry=="`cntry'" & essround==`essround' [aw=dweight], over (esec) by(gndr, note("") title("Respondents ESeC Class")) ytitle("Percent", size(small)) blabel(bar, position(inside) format(%9.1f) color(white))
        graph export "$path/graphs/`essround'_`cntry'.png", as(png) replace
        }
        }
        }
        Also,
        Code:
        tabulate cntry essround
        will confirm or refute my assumption that some combination is missing.
        Yes, you're perfectly right. I did not control for this within the loop, that with your suggestion works as I wanted.
        Many thanks.

        Comment


        • #5
          The double loop and the preserve/restore cycle should all be avoidable. William fixed the second, but the the first can be cut down too. Necessarily not tested, but consider this as an alternative:

          Code:
          egen group = group(cntry essround), label
          su group, meanonly
          
          forval g = 1/`r(max)' {
          
          local note : label (group) `g'
          
          graph hbar (percent) [aw=dweight] if group == `g', ///
          over (esec) by(gndr, note("`note'") title("Respondents ESeC Class")) ///
          ytitle("Percent", size(small)) blabel(bar, position(inside) format(%9.1f) color(white))
          
          graph export "$path/graphs/`g'.png", as(png) replace
          
          }

          Comment

          Working...
          X