基于其他本地的Stata本地 [英] Stata local based on other local
问题描述
我正在尝试将local与早期使用local的值一起使用.一个例子:我想定义"final",并且我希望它包含"var1 var2".但是,我想先定义"temp",然后在final的定义中重用其内容. 这是我尝试过的:
I am trying to use local with the value of an earlier use of local. An example: I want to define "final" and I want it to contain "var1 var2". However, I want to define "temp" first, and reuse its contents in the definition of final. Here is what I tried:
local temp "var2"
local final "var1 " `temp'
有人可以告诉我我在做什么错吗?
Can anyone tell me what I am doing wrong?
推荐答案
有效的示例:
// 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'
此处的双引号被解释为字符串定界符,因此将被删除.在local second "mpg `first'"
中(取消)引用local first
时,宏替换将插入weight
,而不是"weight"
.之后,当在regress ...
中(取消)引用local second
时,宏替换将导致mpg weight
,而不是"mpg weight"
.所以这都是合法的语法.
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'
那被认为是非法的.
Stata不在乎我们是否在local
语句中省略双引号,这就是为什么我的第一个示例有效.请参见 [U] 18.3.4 .宏和表达式.
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.
另请参见 http://www.stata.com/statalist/archive/2009-01/msg00282.html 和help quotes
.
这篇关于基于其他本地的Stata本地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!