Announcement

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

  • Problems saving graphs within foreach loop

    Hey all,

    ​New member here, first time posting. I'm working in stata right now, using a foreach loop to create graphs. The problem comes with saving the graphs. My code keeps writing the plots on top of each other instead of saving, then writing a new graph for the next one.

    Here is my code:

    foreach x of var country {
    twoway (line _total_ year if country == `x', yaxis(1)) (line _score year if country == `x', yaxis(2)), xtitle(Year) title(`x' Total and Score) saving(`x' Total and Score)
    }


    It gives me one graph with the total and score of each country instead of separate graphs. Ideally I'd like to put in $path\save graphs here\ but I can't seem to get the `x' in the filename to work. It always comes out as country


    Thanks for any help!
    Last edited by Nate Miller; 04 Feb 2015, 16:58.

  • #2
    To Stata you have written a loop over precisely one variable and your condition becomes

    Code:
    if country==country
    which is always true and so selects all the data, as you report. So that syntax will not loop over the distinct values of country, which you want.

    Much of http://www.stata.com/support/faqs/da...-with-foreach/ is relevant.

    Comment

    Working...
    X