Search code examples
globalstatalocalstata-macros

Using local macros in names of global macros


How can I use local macros in the names of global macros in Stata 14?

For example:

global test1 = 250
local n = 1

. di $test1 // works
250

. di $test`n' // does not work (should be 250 and not 1)
1

Solution

  • The 18 Programming Stata Manual explains:

    "...You can mix global and local macros. Assume that local macro j contains 7. Then, ${x`j’} expands to the contents of $x7..."

    So you just need to use curly brackets {} in your global macro:

    . global test1 = 250
    . local n = 1
    
    . display $test1
    250
    
    . display ${test`n'}
    250