[Solved] Stata local based on other local


An example that works:

// example data
sysuse auto, clear

// what you want
local first weight
local second `first' mpg

// example use of local second
regress price `second'

Edit

To answer your comment:

Yes, your problem are the double quotes. That doesn’t mean, however, that using double quotes will automatically get you in trouble. For example, this will work:

// example data
sysuse auto, clear

// what you want
local first "weight"
local second "mpg  `first'"

// example use of local second
regress price `second'

Double quotes here are interpreted as string delimiters, and so will be stripped. When local first is (de)referenced in local second "mpg `first'", macro-substitution will insert weight, not "weight". Afterwards, when local second is (de)referenced in regress ..., macro-substitution results in mpg weight, not "mpg weight". So it’s all legal syntax.

Your example contains invalid syntax. You delimit the first piece with double quotes and then continue with another piece:

`temp'

That is considered illegal.

Stata doesn’t mind if we omit the double quotes in the local statement, which is why my first example works. See [U] 18.3.4 Macros and expressions.

See also http://www.stata.com/statalist/archive/2009-01/msg00282.html and help quotes.

1

solved Stata local based on other local