简体   繁体   English

循环内的局部宏

[英]Local macros inside a loop

I am running a panel regression and I have to try combinations of variables. 我正在运行面板回归,因此我必须尝试变量的组合。

I have been trying to run the code below: 我一直在尝试运行以下代码:

local x0 elec_qtr_dummy 
local x1 elec_qtr_dummy elec_qtr_1b 
local x2 elec_qtr_dummy elec_qtr_1b elec_qtr_2b 
local x3 elec_qtr_dummy elec_qtr_1b elec_qtr_2b elec_qtr_3b 
local x4 elec_qtr_dummy elec_qtr_1b elec_qtr_2b elec_qtr_3b elec_qtr_4b 
local x5 elec_qtr_dummy elec_qtr_1b elec_qtr_2b elec_qtr_3b elec_qtr_4b elec_qtr_1a elec_qtr_2a 
local x6 elec_qtr_dummy elec_qtr_1b elec_qtr_2b elec_qtr_3b elec_qtr_4b elec_qtr_5b elec_qtr_6b

xtset companyid 

forvalue v = 0/6 {  

    eststo,title("log_stqf_deal"): xi: xtreg log_stqf_deal `x`v'' i.year,fecluster(state_code)
    est2vec table`v', e(N) vars(`x`v'') name(lstqf_deal) replace 

    eststo,title("log_totln"): xi: xtreg log_stqf_deal `x`v'' i.year,fe cluster(state_code)  
    est2vec table`v', addto(table`v') name(ltotln)

    est2rowlbl `x`v'', saving replace path(`file') addto(table`v')
    est2tex table`v', preserve path(`file') mark(starb)fancy levels(90 95 99) label replace 
    estimates clear
} 

However, Stata refuses to acknowledge the presence of locals inside the forvalues loop. 但是,Stata拒绝承认forvalues循环中是否存在本地人。

It would be really helpful if somebody can point out an efficient alternative. 如果有人可以指出一个有效的替代方案,那将真的很有帮助。

I'm using Stata version 12.0. 我正在使用Stata 12.0版。

The quotes are not good. 报价不好。 They Should be: 他们应该是:

xi: xtreg log_stqf_deal `x`v'' i.year,fe

But I can't tell if they were good and your original formatting messed them up. 但是我无法确定它们是否很好,并且您原来的格式弄乱了它们。 You should confirm. 您应该确认。

A working example: 一个工作示例:

clear
set more off

sysuse auto

local x0 mpg 
local x1 mpg rep78

forvalue v = 0/1 {
    reg price `x`v''
}

You don't quote the error Stata gives you, which is a desirable thing. 您不要引用Stata给您的错误,这是可取的。

You can also check the stepwise command, but that is something to use wisely. 您也可以检查stepwise命令,但这是明智使用的方法。

As @Roberto Ferrer correctly points out, there is nothing here that is reproducible. 正如@Roberto Ferrer正确指出的那样,这里没有可复制的内容。 Indeed the claim that "Stata refuses to acknowledge ..." is just wording in anthropomorphic terms that itself does not make clear what is happening. 确实,“ Stata拒绝承认……”的说法只是用拟人化的措词表达的,其本身并不能弄清楚正在发生的事情。 But the key to the question appears to be that the local macros are not visible to the code being executed. 但是,问题的关键似乎在于本地宏对于正在执行的代码不可见。

A basic error with local macros is so common that it is worth an answer, as even if it is not the answer to the OP's problem, it is the answer to many problems that might be posted under similar titles. 局部宏的基本错误非常普遍,因此值得一个答案,即使即使不是OP问题的答案,它也是许多可能以相似标题发布的问题的答案。

Local macros are local to the space in which they are defined, which means precisely one of 局部宏对于定义它们的空间是局部的,这恰好意味着

  • the main interactive session 主要的互动环节

  • a particular program 特定程序

  • a particular do-file 一个特定的文件

  • (part of) the contents of a particular do-file editor window 特定执行文件编辑器窗口的内容(的一部分)

Notice that the "(part of)" in the last really can bite, as when (for example) you are executing chunks of code separately. 请注意,最后一个中的“(部分)”确实可以咬人,例如(例如)您分别执行代码块时。 The definition of the local macro being referenced must be visible to Stata within the same code space. 被引用的本地宏的定义必须在同一代码空间内对Stata可见。

Outside that space, local macros will be invisible, meaning that references to them will be interpreted as referring to non-existent macros and empty strings illegal. 在该空间之外,局部宏将是不可见的,这意味着对它们的引用将被解释为引用了不存在的宏,并且空字符串是非法的。 Referring to macros that do not exist is not in itself illegal, but the resulting statements may be illegal or may just not do what the user wants, as is presumably the case here. 引用不存在的宏本身并不是非法的,但是结果语句可能是非法的,或者可能只是不执行用户想要的操作,这大概就是这里的情况。

To test for problems with local macros, you can 要测试本地宏的问题,您可以

  1. set trace on to see line-by-line interpretation of code. set trace on以查看代码的逐行解释。 If macro references are substituted by empty code, Stata cannot see the local macros; 如果用空代码替换宏引用,则Stata无法看到本地宏; they really are not local, but somewhere else in your code. 它们确实不是本地的,而是代码中的其他位置。

  2. Attempt to display macros just before they are used. 尝试在使用宏之前display它们。 Same story as #1. 与#1故事相同。

  3. Use macro list to list (global and) local macros known to Stata that apply to your code. 使用macro list可以列出(适用于您的代码的)Stata已知的(全局和)本地宏。 Same story as #1. 与#1故事相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM