Announcement

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

  • for each loop

    I am trying to do a series of stata bar graph using a loop. The main graph command is this:

    graph bar proceduretype1 proceduretype2 proceduretype3 proceduretype4, ylabel(, angle(0)) stack percent over(surgeryyear, label(angle(rvertical))) nooutsides ytitle(Percent) title ("Distribution of Procedures by Center") legend(rows(1) label(1 "isol CABG") label(2 "isol Valve") label(3 "CABG+Valve") label(4 "Other") size(*.8))

    I wish to produce a graph based on another variable, let's call it medicalcenter.

    I tried this command, but it didn't work. I received a "r(198)" error. I would welcome input from others.

    foreach var medicalcenter {
    graph bar proceduretype1 proceduretype2 proceduretype3 proceduretype4, ylabel(, angle(0)) stack percent over(surgeryyear, label(angle(rvertical))) nooutsides ytitle(Percent) title ("Distribution of Procedures by Center") legend(rows(1) label(1 "isol CABG") label(2 "isol Valve") label(3 "CABG+Valve") label(4 "Other") size(*.8))
    name(gr`var', replace)
    }

  • #2
    The error came from the incomplete syntax. The first line should be
    Code:
     foreach var in medicalcenter {
    or more explicitly stating it is a variable,
    Code:
     foreach var for varlist medicalcenter {
    See -help foreach- for the complete syntax.

    The syntax problem aside, I do not see the purpose of using a loop in this context. For one thing, a loop is redundant if you are using it for only one variable--assigning the variable to a macro would suffice. Perhaps you were just using it to provide an example. But still, nothing other than the name of the graph would be altered by this loop..
    Last edited by Aspen Chen; 07 Oct 2014, 16:41.

    Comment


    • #3
      There is a typo in Aspen's second code example. Should be

      Code:
       foreach var of varlist medicalcenter {
      The major misunderstanding here is different.

      I guess the belief is that such a syntax would cycle over the distinct values of medicalcenter. As Aspen points out, it would not do that.

      But that problem has been documented at length elsewhere: see e.g. http://www.stata.com/support/faqs/da...ach/index.html

      dlikosky: Please note our request that you use a full real name on Statalist, such as "Aspen Chen". See FAQ Advice Section 5.
      Last edited by Nick Cox; 07 Oct 2014, 18:06.

      Comment


      • #4
        Thanks for catching the typo, Nick.

        If cycling through the distinct values of medicalcenter was the intention, perhaps the OP could use the option of by(medicalcenter), assuming there are not too many values.
        Last edited by Aspen Chen; 07 Oct 2014, 18:12.

        Comment


        • #5
          Thanks Aspen and Nick -
          I am attempting to cycle through each unique value of the variable "medicalcenter", and create a unique graph for each. there are about 40 unique values, and I have a number of these graphs, so developing an easy routine would be helpful for me.
          Is there a way to add a suffix at the end of each filename based on the unique value label of variable "medicalcenter"? i would like to export each unique graphs as a .wmf, and usually use the graph export command to do so.

          I will work to address my name on this forum.
          -Donald Likosky

          Comment


          • #6
            Most of the answer to your question is within the FAQ already cited. See how far you can get after studying the advice there.

            Here's dopey example illustrating some basic technique:

            Code:
             
            sysuse auto, clear 
            
            foreach value in 0 1 { 
                local which : label (foreign) `value'    
                histogram mpg if foreign==`value', start(10) width(2) t1title("`which'") saving(mpg`value')  
            }

            Comment


            • #7
              I have figured out now how to do the loop using a numeric field for the medical center. I am now trying to figure out how to do it when using a nominal variable (i.e. a medical center's actual name). I receive an "invalid syntax" error. Thoughts on where I went astray? Many thanks. levelsof Abbreviation, local(levels) foreach Abbreviation of local levels { if Abbreviation == `l' graph bar proctype1 proctype2 proctype3 proctype4 if Abbreviation==`l', ylabel(, angle(0)) stack percent over(surgeryyear, label(angle(horizontal))) nooutsides ytitle(Percent) title ("Distribution of Procedures by Year" "`l'") legend(rows(1) label(1 "isol CABG") label(2 "isol Valve") label(3 "CABG+Valve") label(4 "Other") size(*.8)) graph export "test`l'.wmf", replace }

              Comment


              • #8
                The code here is essentially unreadable without formatting. Please note how Aspen and I used CODE delimiters earlier in the thread to format code: see FAQ Advice for how (it takes about a minute to learn).

                Here's the code in more civilised form:

                Code:
                levelsof Abbreviation, local(levels)
                foreach Abbreviation of local levels {
                      if Abbreviation == `l' graph bar proctype1 proctype2 proctype3 proctype4 if Abbreviation==`l', ylabel(, angle(0)) stack percent over(surgeryyear, label(angle(horizontal))) nooutsides ytitle(Percent) title ("Distribution of Procedures by Year" "`l'") legend(rows(1) label(1 "isol CABG") label(2 "isol Valve") label(3 "CABG+Valve") label(4 "Other") size(*.8))
                     graph export "test`l'.wmf", replace
                }
                You are getting confused between the if command and the if qualifier.

                The opening command if Abbreviation == `l' won't work as you expect and should be deleted.

                Also if Abbreviation is a string variable, you need " " to delimit its values.







                Comment


                • #9
                  Thank you Nick.

                  I apologize about the formatting. I have rectified it as you suggested.

                  I am using Stata/IC 13.1

                  Here is the revised code. I now receive a "no observations" error message. It appears that it is reading the first row, as Stata returns the unique values of the variable Abbreviation.

                  Code:
                  levelsof Abbreviation, local(levels)
                  foreach Abbreviation of local levels {
                  graph bar proctype1 proctype2 proctype3 proctype4 if Abbreviation== " ", ylabel(, angle(0)) stack percent over(surgeryyear, label(angle(horizontal))) nooutsides ytitle(Percent) title ("Distribution of Procedures by Year") legend(rows(1) label(1 "isol CABG") label(2 "isol Valve") label(3 "CABG+Valve") label(4 "Other") size(*.8))
                  graph export "test`l'.wmf", replace
                  }

                  Comment


                  • #10
                    You are testing whether the value of Abbreviation is precisely one space and so it's expectable that there are no such values. My suggestion was different: to use double quotes as delimiters. Thus in general if your abbreviations are say "ABC", "DEF", etc. you need the abbreviations as just shown and within delimiters.

                    Otherwise put, if you test

                    Code:
                    if Abbreviation == ABC
                    Stata expects ABC to be a (possibly abbreviated) variable name for a string variable.

                    In this case, given the loop, the syntax is

                    Code:
                    ... if Abbreviation == "`Abbreviation'" ...
                    You have at least one other bug in your code. Whether the reference to the macro is or not defined, the filename defined by

                    Code:
                      
                    "test`l'.wmf"
                    will be the same each time around the loop. Hence you will produce only one graph file, containing the last graph you produced. You need to replace that macro reference by one to Abbreviation -- as the macro name defined in the loop start.
                    Last edited by Nick Cox; 04 Nov 2014, 13:59.

                    Comment


                    • #11
                      Thank you - works like a charm.

                      Comment

                      Working...
                      X