Announcement

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

  • loop for graphs

    I'm a stata novice, and I've searched and tried things I've found, but I just can't figure this out. I am trying to make a scatter plot for each state. I was able to do it by using the number associated with each state (Alabama = 1, Alaska =2, etc) by using

    Code:
     forvalues i = 1/51 {
     twoway (scatter edge percentFRL if state_num == `i', mcolor(blue%50) msize(tiny)), ytitle(Percent SIDE Neighborhood Poverty) ylabel(#5, angle(horizontal)) ymtick(##1) xtitle(Percent Students Free and   Reduced Price Lunch) xlabel(#5, angle(horizontal)) xmtick(##1) title(`i') legend(off)
     graph save `i'.gph, replace
         }

    So the above was a success, but I would like to save using state names instead.

    I have tried

    Code:
    levels state, local(states)
    local i = 1
    foreach s of local states {
     twoway (scatter edge percentFRL if state = `"s"', mcolor(blue%50) msize(tiny)), ytitle(Percent SIDE Neighborhood Poverty) ylabel(#5, angle(horizontal)) ymtick(##1) xtitle(Percent Students Free and Reduced Price Lunch) xlabel(#5, angle(horizontal)) xmtick(##1) title(`"s"') legend(off)
      graph save `"s"'.gph, replace
          }
    To which I receive a type mismatch error. or I try

    Code:
     levels state, local(states)
    local i = 1
    foreach s of local states {
     twoway (scatter edge percentFRL if state = `"s"', mcolor(blue%50) msize(tiny)), ytitle(Percent SIDE Neighborhood Poverty) ylabel(#5, angle(horizontal)) ymtick(##1) xtitle(Percent Students Free and Reduced Price Lunch) xlabel(#5, angle(horizontal)) xmtick(##1) title(`"s"') legend(off)
      graph save '=strtoname("'s'").gph, replace
          }
    and am told twoway is not a valid command name.

    Here is the stata output:

    Code:
    levels state, local(states)
    `"Alabama"' `"Alaska"' `"Arizona"' `"Arkansas"' `"California"' `"Colorado"' `"Connecticut"' `"Delaware"' `"District Of Columbia"' `"Florida"' `"Georgia"' `"Hawaii"' `"Idaho"' `"Illinois"' `"Indiana"' `"Iowa"' `"Kansas"' `"Kentucky"' `"Louisiana"' `"Maine"' `"Maryland"' `"Massachusetts"' `"Michigan"' `"Minnesota"' `"Mississippi"' `"Missouri"' `"Montana"' `"Nebraska"' `"Nevada"' `"New Hampshire"' `"New Jersey"' `"New Mexico"' `"New York"' `"North Carolina"' `"North Dakota"' `"Ohio"' `"Oklahoma"' `"Oregon"' `"Pennsylvania"' `"Rhode Island"' `"South Carolina"' `"South Dakota"' `"Tennessee"' `"Texas"' `"Utah"' `"Vermont"' `"Virginia"' `"Washington"' `"West Virginia"' `"Wisconsin"' `"Wyoming"'
    
    local i = 1
    
    foreach s of local states {scatter edge percentFRL if state = `"s"', mcolor(blue%50) msize(tiny), ytitle(Percent SIDE Neighborhood Poverty) ylabel(#5, angle(horizontal)) ymtick(##1) xtitle(Percent Students Free and Reduced Price Lunch) xlabel(#5, angle(horizontal)) xmtick(##1) title(`"s"') legend(off)
    graph save '=strtoname("'s'").gph, replace
          }
    Thank you for any help!
    Last edited by Stephanie Leigh; 02 Apr 2021, 18:14.

  • #2
    All of your references to `"s"' are wrong: they should be `"`s'"'. Inside a loop iterating s over local states, any use of s must first be in local macro quotes (`s'), and then, because it is a string, further surrounded by regular quotes, hence `"`s'"'. You come close to getting that right in the -graph save- commands of the last three code blocks in your post, but not quite: it isn't 's', it's `s'. The ` is the lower case character just to the left of the 1! key on a standard US keyboard.

    Comment


    • #3
      Thank you, Clyde! The 's' was a typo on my part that I didn't realize was there, but how I was trying to do it was still wrong. So it's macro quotes, surrounded by regular quotes, inside macro quotes again? Again, I appreciate your help so much!

      Comment


      • #4
        Yes, that's one way of saying it. The combination `"whatever"' is, in Stata jargon, referred to as compound double-quotes. So in Stata jargon, we would say it's macro quotes surrounded by compound double quotes. I think it is important to use that language, because, while compound double quotes are instantiated as "macro quotes" surrounding "regular quotes" they are not related to extracting the contents of macros. And I think that one of the most important things to keep in mind with Stata code is when you are, or are not, dereferencing a local macro.

        Comment

        Working...
        X