Announcement

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

  • Using -foreach- to export graphs

    I am trying to save my graphs from a loop but every time the command runs, it just saves over and replaces the saved graph before it, leaving me with only one graph (of the last variable).

    below is my code.

    Code:
    foreach var in x1 x2 x3 x4 x5 x6 x7 {
        hist `var', title("`var'")
        graph export "file location\`var'.png", replace
    }
    thanks!

  • #2
    What is the name of the file in which it stores the graph?

    I ask because if you are literally typing the backslash before the single quote of `var', then more likely than not, it is saving a file with a name like "file location`var'.png" because the backslash is an escape character for Stata, telling it to taking the quote as a literal character. You should use forward slashes in your path instead, even on a Windows machine. Or use double backslashes \\, to escape the backslash itself.

    Comment


    • #3
      If you want it saved in the directory you are using, just drop the file location\ part.

      Comment


      • #4
        changing it to the forward slashes worked, thank you!

        the first few graphs I created I didn't use a loop and saved them directly from stata and then copied and pasted the file path from the output to my do file. I didn't realize when typing the command it would be any different. Thanks again!

        Comment

        Working...
        X