Local macros inside a loop

1k views Asked by At

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.

It would be really helpful if somebody can point out an efficient alternative.

I'm using Stata version 12.0.

2

There are 2 answers

6
Nick Cox On

As @Roberto Ferrer correctly points out, there is nothing here that is reproducible. Indeed the claim that "Stata refuses to acknowledge ..." is just wording in anthropomorphic terms that itself does not make clear what is happening. 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.

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.

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. If macro references are substituted by empty code, Stata cannot see the local macros; they really are not local, but somewhere else in your code.

  2. Attempt to display macros just before they are used. Same story as #1.

  3. Use macro list to list (global and) local macros known to Stata that apply to your code. Same story as #1.

7
Roberto Ferrer On

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.

You can also check the stepwise command, but that is something to use wisely.