Announcement

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

  • Double Macros Over loop

    I am having trouble using two macros in a loop. Here's what I'm trying.

    I set these macros:

    Code:
    local sub1 Zero
    local sub2 15-19
    local sub3 50-54
    local sub4 80-84
    And I try the following graphs for ages 1 to 4

    Code:
    foreach age in 1 2 3 4{
    twoway ..., ///
    subtitle(Age `sub`age'') saving(`age', replace) ...
    }
    The text I want in the subtitle (Age Zero, Age 15-19, etc.) isn't showing up. Any ideas about how to fix this? Thank you in advance.

  • #2
    Does wrapping the subtitle in quotes work?

    Code:
    clear
    
    local sub1 Zero
    local sub2 15-19
    local sub3 50-54
    local sub4 80-84
    
    
    foreach age in 1 2 3 4{
        di "Age `sub`age''"
    }
    Age Zero
    Age 15-19
    Age 50-54
    Age 80-84

    Comment


    • #3
      The examples of subtitles in, for example, the output of the help title options command, suggests that you need surrounding quotation marks.
      Code:
      subtitle("Age `sub`age''")

      Comment


      • #4
        Well, you provide only a skeleton of your code, and I can't be certain there isn't something in the … sections that is causing you problems. But I am unable to replicate your problem. Here's a demonstration of what I did:

        Code:
        sysuse auto, clear
        
        local sub1 Zero
        local sub2 15-19
        local sub3 50-54
        local sub4 80-84
        
        foreach age in 1 2 3 4{
            twoway scatter price mpg if rep78 == `age',  ///
            subtitle(Age `sub`age'') name(A`age', replace)
        }
        I ran that and the subitltes showed up in each graph just the way you would like them to. The only substantive change I made to your code skeleton is to use -name()- instead of -savihng()- just because I don't want to clutter up my hard drive with graphs that I don't need. Since the "name" of a graph can't begin with a digit, I had to specify A`age' instead of `age' (filenames can start with digits), but I'm quite confident that doesn't matter.

        There's probably nothing wrong with your code. I'll guess that you are running the code incorrectly. When working with local macros, you must run the entire code, from the first time the macro is defined until the last time it is used, in one fell swoop. If you are running the local macro definitions, then stop, and try to run the foreach-loop, the definitions of sub1 through sub4 will disappear between those two runs, and then -subtitle(Age `sub`age'')- refers to an undefined local macro. Undefined macros are interpreted as empty strings, so the subtitle option looks to Stata like -subtitle(Age )-, and the graph titles will all be just "Age ".

        So run it all at once from beginning to end and I'll bet your problem is solved.

        Added: Crossed with #2 and #3 which suggests a different diagnosis. But I had no difficulty running this without wrapping the subtitle in double-quotes.

        Comment

        Working...
        X