Announcement

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

  • Saving bar graphs with loop

    Hi folks. First time poster, relatively new-ish stata user. I'm trying to create multiple bar graphs for race and gender percentages based on one of my variables (AcademicRankMajor) using a loop. The loop works perfectly...until I try to add a command to save the graphs. I've tried everything I can think of to get it to work; I've tried omitting the continuation, using the "graph export" command instead, using the name/ replace command. None of these solutions work. When I run this command specifically, I get an error message that reads "invalid 'replace'". Here's my code:

    levelsof(AcademicRankMajor), local(levels)
    foreach level of local levels {
    catplot Gender Ethnicity if AcademicRankMajor=="`level'", title("Gender and Ethnicity by `level'") ///
    style(compact) ///
    stack asyvars percent (AcademicRankMajor) ///
    graph save "EthGen by `level'" , replace
    }

    I'm sure the solution is simple-ish, but I've exhausted my own working knowledge here. Thanks!
    Last edited by Mallory Hartwig; 02 Aug 2019, 14:50.

  • #2
    -graph save- is separate command from Nick Cox's -catplot- (ssc desc catplot).

    Also, it appear that is a space between percent suboption of the variable in the parentheses.

    The example below works:
    Code:
    sysuse auto,clear
    gen himpg = mpg > 25
    levelsof fore, local(levels)
    
    foreach level of local levels {
        catplot rep himpg if fore == `level' /// 
            , title("Gender and Ethnicity by `level'") ///
            style(compact) ///
            stack asyvars percent(foreign) 
    
        graph save "Graph by `level'", replace
    }

    Comment


    • #3
      Thanks, Scott. I've matched the code above, but without the continuating after the stack command line, it just shoots through each graph without saving. I watch them go by helplessly as if the graph save command wasn't there at all.

      Comment


      • #4
        Cross--posted at https://www.reddit.com/r/stata/comme...phs_from_loop/

        Please note our policy on cross-posting -- explicit at https://www.statalist.org/forums/help#crossposting -- which is, in essence, that you should tell us about it.

        More crucially, https://www.statalist.org/forums/help#stata explains that you should give exact code and a reproducible example Without that even experienced Stata users are hard pushed to know what is going on in #3.

        With Scott Merryman 's helpful example in mind, I am led to emphasise that by() lets you produce all the graphs at once.

        Code:
        sysuse auto,clear
        gen himpg = mpg > 25
        
        catplot rep himpg, by(foreign) style(compact) stack asyvars percent(foreign)

        Comment


        • #5
          Like I said-- I'm new at this, and appreciate any patience you can extend.

          And using the by command creates all graphs in one window-- which is less helpful, as I need each graph individually.

          Comment


          • #6
            OK, so you want each graph individually.

            Sorry, but I still can't diagnose the problem in #3 as you have not added any information on precisely what you did.

            (No hostility here, or even impatience, but being new is precisely why you need to read and act on the FAQ Advice.)

            Comment


            • #7
              Ok, let's try this.



              Code:
              sysuse auto2, clear
              
              levelsof(make), local(levels)
              
              foreach level of local levels {
              catplot foreign rep78 if make=="`level'" , title "Foreign and Rep 78 by `level'" ///
              style(compact) ///
              stack asyvars percent (make) ///
              graph save "for87 by `level' , replace
              }

              Comment


              • #8
                Scott Merryman pointed out in #2 that graph save is a separate command.

                The code you give isn't legal and results in an error message

                Code:
                invalid 'replace' 
                r(198);
                although the real error is the connecting ///

                The consequence won't be as described in #3.


                ​​​​​​​

                Comment


                • #9
                  Right- so honestly, I just don't know how to fix it. My underlying question is: how do I run this loop AND get them all to save? Is it a different code entirely? When I remove the /// in my code, it runs through without saving. When I run it using the code above it says "quotes unbalanced." I don't know how to make this work. Any suggestions?

                  Comment


                  • #10
                    There is an extra bug in #7

                    Code:
                    graph save "for87 by `level' , replace
                    should be

                    Code:
                    graph save "for87 by `level'", replace
                    I corrected this in my version automatically, thinking that it was my typo, but it's yours.

                    Already in #2 Scott gave code that works. The analogous code modifying the code in #1 just omits the unnecessary .comment connector that makes the command illegal.

                    Code:
                    levelsof AcademicRankMajor, local(levels)
                    
                    foreach level of local levels {
                        catplot Gender Ethnicity if AcademicRankMajor=="`level'", ///
                        title("Gender and Ethnicity by `level'") ///
                        style(compact) stack asyvars percent(AcademicRankMajor)
                    
                        graph save "EthGen by `level'" , replace
                    }
                    In copying the code above I made some stylistic changes but didn't consciously change syntax except by correcting the error named.

                    Necessarily I cannot test this as you don't give a corresponding data example. Note that this code must be run either line by line in an interactive session or as a block from a do-file or do-file editor window.

                    If this doesn't work, then we really need a data example. https://www.statalist.org/forums/help#stata explains.
                    Last edited by Nick Cox; 06 Aug 2019, 08:49.

                    Comment


                    • #11
                      I'm super embarrassed; they've been saving this whole time. They were just in a super hidden filepath. Insert face palm emoji here.

                      Thanks for your help. Hopefully my comps go better than this do-file has.

                      Comment


                      • #12
                        ^Thanks for reporting the result.

                        Comment

                        Working...
                        X