Announcement

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

  • Trying to loop graph export

    I'm using Stata/MP 13.0 , and I'm trying to export several graphics from a longitudinal based data( 69 variables and 26 cross-section units) , So far I tried two different approaches
    First, I tried to loop over the cross-sectional units:
    Code:
    cd "$graphfolder"
    levelsof city
    foreach a in r(levels){
    xtline y1 if id==`a' , title(`a')
    graph export "$graphfolder\y1-demeaned.png" , as(png) replace
    }
    I got "type mismatch" error , r(109)

    And tried to loop over variables:
    Code:
    cd "$graphfolder"
    forval num = 1/69 {
    xtline y`num' if id==1 , title("city id==1")
    graph export "$graphfolder\y`num' -demeaned.png" , as(png) replace
    }
    And the problem is, I think, with the first onde is that title() does not "accept" the loop. And the second one is the same thing except with the file name.
    How do I overcome this problem? Should I write for each graphic ? Because the 'overlay' option is almost impossible to understand the graphic with so many cross-section units.
    Thanks

  • #2
    In your first code block r(levels) is offered to foreach in your code as literal text, which is not what you want. The title() option is not biting here because the code implies a legal title.

    Code:
    . sysuse auto
    (1978 Automobile Data)
    
    . levelsof mpg
    12 14 15 16 17 18 19 20 21 22 23 24 25 26 28 29 30 31 34 35 41
    
    . foreach x in r(levels) {
      2. di "`x'"
      3. }
    r(levels)
    Note how the text r(levels) is just echoed.

    This has more chance of working

    Code:
     cd "$graphfolder"
    levelsof city
    foreach a in `r(levels)' {    
         xtline y1 if id==`a' , title(`a')    
         graph export "$graphfolder\y1-demeaned.png" , as(png) replace
    }
    except that you are repeatedly saving to the same file.

    I can't see a problem with your second code block. But then again I can't check your global definition or your file structure. What is the exact error report?

    I can't see that dozens of graph files will help you. How are you going to compare them?
    Last edited by Nick Cox; 30 Oct 2015, 09:40.

    Comment


    • #3
      Is city a string variable, or is it a numeric variable (perhaps with value labels attached)? Same question for id. If they are not of the same type, you will get a type mismatch from the -if id == `a'- condition.

      Also, if you get your first loop to run, note that you are exporting to the same filename each time through the loop, so you just clobber each iteration with the next one. At the end you will have only a single png file containing the results for the last value of city.

      Comment


      • #4
        Nick Cox,
        Thanks for your help,actually I have two database which has exactly the same variables, cities and timespan, except that , in one of those data I made some changes , and I wanted to compare them. But Stata does not "Open" 2 database at once. And I know that I was saving to the same file, the thing is, I naively thought that you could loop the files , that's why I added the `num' after the variable name ( y`num') , but you can't do that
        Clyde Schechter,
        Yes that's right , I got that second block wrong, city is a string variable . Id is a numeric variable , which is
        Code:
         egen id = group(city)

        Comment


        • #5
          I still don't understand why the second code block doesn't work for you.

          Comment

          Working...
          X