Announcement

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

  • Problem combining foreach and a local macro

    I have summary billing data for 3000+ hospitals across 500+ diagnoses groups. I'm trying to produce twoway graphs for each of the diagnoses groups. The diagnoses groups (variable drg) range 1-900, but there are many missing integers in between. The following code works, but produces many blank graphs.

    Code:
    foreach i of num 1/900 {
    local t:label(drg)`i'
    twoway (scatter discharges payments if drg==`i', mlabel(providername) scale(0.4) title("`i' - `t'") ), saving(drg`i', replace)
    graph export "drg`i'.pdf", replace
    }
    I therefore tried to use the following code, based on the 2nd method explained in a Stata FAQ here. The code runs without an error, but does not produce any results.
    Code:
    levelsof drg, local(drgnums)
    
    foreach i of local drgnums {
    local t:label(drg)`i'
    twoway (scatter discharges payments if drg==`i', mlabel(providername) scale(0.4) title("`i'-`t'")), saving(drg`i', replace)
    graph export "drg`i'.pdf", replace
    }
    Thanks!
    Last edited by Will Fleischman; 17 Jul 2017, 14:27.

  • #2
    Can you post some example data that illustrates this problem? I made up a toy data set and used essentially the same code and it ran just fine for me. (All I changed was skipping the export to pdf, having the graphs stored in memory instead of saved to disk, and I didn't bother with the providername label on the points. But none of that should matter.

    Added: One quick thought. Are you sure you are looking in the right directory for the result graphs? Stata will save them (both the .gph and the .pdf) in the current working directory. To see what directory that is, run
    Code:
    pwd

    Comment


    • #3
      Thanks for the reply. It's definitely not running as I'm not running it quietly, so would expect to see it running, and I also checked the working directory. Here's an example of the code. As mentioned, the drg variable is encoded but with non-consecutive integers.

      drg discharges payments providername
      Heart failure 119 233324.1 Hospital 1
      Kidney failure 11 24414.07 Hospital 1
      Heart attack 33 276342.2 Hospital 1
      Heart failure 15 18193.22 Hospital 2
      Kidney failure 11 30586.94 Hospital 2
      Heart attack 14 15845.73 Hospital 2

      Comment


      • #4
        Update: I figured out what I was doing wrong. I was creating the local macro first, and then executing the loop in a separate step. Creating the macro during the same execution as the loop seems to be necessary. Any idea why? Can't the loop call a previously-created macro?

        Comment


        • #5
          Local macros are just that - local to the do-file within which they are run. Once you see
          Code:
          end of do-file
          in your Results window, any local macros have vanished.

          While the Do-file editor allows you to run selected lines from those in the window, if you look closely at the Results window, you will see that the selection is copied to a temporary do-file and run from within that.

          For completeness let me add that local macros are also invisible to do-files called from within the do-file in which they are defined, so they cannot be used to pass values between do-files.

          Comment


          • #6
            Ah - understood. Thank you, William and Clyde!

            Comment


            • #7
              I had the same problem with my foreach-loop with locals. I didn't get an error, but stata didn't went through the loop again. Then I changed this:
              Code:
              levelsof drg, local(drgnums) 
               foreach i of local drgnums {
              to this:
              Code:
              levelsof drg, local(drgnums) 
               foreach i in `drgnums' {
              and it worked. I read that it would mean the same, but apperently it doesn't. Can somebody explain me why?
              Thanks!

              Comment


              • #8
                They should be equivalent. Something else is needed to explain why they did not seem to work in the same way for you. William's explanation in #5 points to the most common pitfall, running lines separately so a command line cannot "see" the local macro definition.

                Comment


                • #9
                  Nick's answer seems the most likely. However, if

                  but stata didn't went through the loop again
                  means Stata went through the first loop, but only one time, then perhaps within the loop that we weren't shown, the value of the local macro drgnums is reset. This affects the first format of the loop, but not the second, which retrieves all the values of the local macro before executing the loop.
                  Code:
                  . local names a b
                  
                  . foreach i of local names {
                    2. display "`i'"
                    3. local names 
                    4. }
                  a
                  
                  . local names a b
                  
                  . foreach i in `names' {
                    2. display "`i'"
                    3. local names 
                    4. }
                  a
                  b
                  
                  .

                  Comment

                  Working...
                  X