Announcement

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

  • How to code so that consecutive globals are called within a foreach loop

    Dear Statalisters,

    I use foreach and forvalues often in my syntax and now I want to call globals consecutively, but I do have a problem with that (using Stata 14). Let me first explain briefly what I want to do (without using data). My most simple code running normally is:
    Code:
    global vars1 "var1 var2 var3 var4"
    global vars2 "var5 var6 var7 var8"
    global vars3 "var9 var10 var11 var12"
    
    // display the strings in Stata result window:
    dis "$vars1"
    dis "$vars2"
    dis "$vars3"
    Next, use code to loop from 1 to 3, and display it:
    Code:
    forvalues i = 1/3 {
        dis `i'
    }
    Which leads to the code that I thought would do the same so that the string of each global is displayed:
    Code:
    forvalues i = 1/3 {
        dis "$vars`i'"
    }
    But that does not happen, I now only get the numbers:
    1
    2
    3

    Probably this is a awfully simple error, but I fail to solve it. Where did the string [$vars] go? I mean, if this code is wrong, why does it not generate an error?
    And, does anyone know how to code properly so that multiple globals can be used consecutively in a forvalues loop (e.g. for running regressions, generating graphs)?
    http://publicationslist.org/eric.melse

  • #2
    Documented at [U] 18.3.10.

    Code:
    . local i 1
    
    . global vars1 foo
    
    . di "$vars`i'"
    1
    
    . di "${vars`i'}"
    foo
    There is no error in your code except to you. Stata sees a reference to $vars and then a reference to `i' but it's not an error in itself to refer to a macro that does not exist: the consequence is just substitution of an empty string. (Whether that in turn leads to a separate error is a separate question.) To force Stata to parse as you wish you must be explicit in punctuation, which is why the braces must be used.
    Last edited by Nick Cox; 05 Jun 2015, 07:05.

    Comment


    • #3
      To mix global and local macros the following syntax is needed:
      Code:
      forvalues i = 1/3 {
          dis "${vars`i'}"
      }
      See Section "18.3.10 Advanced global macro manipulation" in the Stata User's Guide.


      Edit: Nick was faster.
      Last edited by Sebastian Kripfganz; 05 Jun 2015, 07:06.
      https://twitter.com/Kripfganz

      Comment


      • #4
        As an aside, it is better practice to use local macros than global macros in almost all circumstances. First, local macros eliminate the problem of name clashes with macros defined elsewhere, so they are safer. Second, because the syntax for referencing local macros involves paired delimiters ` and ', you don't run into the kind of problem with order of interpretation that you did in your original post.

        Comment


        • #5
          Dear Nick, Sebastian, Clyde, thank you for explaining what to do-and indeed this works fine.
          Dear Clyde, now I solved this task using locals as:
          Code:
          forvalues i = 1(1)3 {
              local vars1 "var1 var2 var3 var4"
              local vars2 "var5 var6 var7 var8"
              local vars3 "var9 var10 var11 var12"
                  display "`vars`i''"
          }
          http://publicationslist.org/eric.melse

          Comment

          Working...
          X