Announcement

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

  • Global macro in twoway graph

    My task requires me to write the twoway commands many times, like
    Code:
    twoway (line land year if city=="NewYork")
                 (line land year if city=="Boston")
                 ······
                 (line land year if city=="Tucson")
                 , options
    Since the number of city names is large, the conventional command style is inconvenient. I want to make use of macro, like
    Code:
    local city "NewYork" "Boston" ······ "Tucson"
    global  twograph
    foreach i of local city {
      global twograph ${twograph} (line land year if city == `i',lwidth(medthick))
    } 
    twoway $twograph, legend(off)
    But the stata keeps telling me that "parentheses do not balance". Can anyone tell me the source of this error. If there is other possible way to
    write multiple twoway commands in a more clear and convenient way, it's welcomed. Thank you.

  • #2
    No real need for globals. Here's an example from which you can work.
    One issue would have been the red bits in the example below.

    Code:
    webuse grunfeld, clear
    drop if company >3
    gen city ="NewYork" if company==1
    replace city ="Boston" if company==2
    replace city ="Tucson" if company==3
    
    levelsof city, local(city)
    local twograph
    foreach i of local city {
      local temp (line invest year if city == "`i'",lwidth(medthick))
      local twograph `twograph'`temp'
    }
    twoway `twograph', legend(off)

    Edit: you could also make a local with a list of all city names, but there'd be no need to 'adorn' these with quotes. Just the names of the city, and then introduce the quote marks around the individual city names in the if city == "`i'" part
    Last edited by Jorrit Gosens; 26 Jul 2018, 05:08.

    Comment

    Working...
    X