-1

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.

  • I fixed the formatting for you but please note that caring for your quotes, which can be messed up by Markdown formatting, is important in this case. You **still** have not showed us the error Stata gave you. See http://stackoverflow.com/editing-help. – Roberto Ferrer Dec 12 '14 at 13:50
  • Thanks for your help. I'm a newbie to this forum hence the aloofness. As i mentioned earlier STATA doesn't give any errors, it just runs the panel regression without the local macro dummies i create. – Lakshminarayanan Iyer Dec 12 '14 at 13:56
  • `xi:` is no longer the standard way to go if the estimation command allows use of _factor variable_ notation (which you use). See `help fvvarlist` and `help xi`. – Roberto Ferrer Dec 12 '14 at 13:56
  • I tried the regressions without xi prefix also. But not working. When I loop over just the variable (use display to verify contents of my local macro variable) instead of the regressions the contents of the local variables are perfectly displayed. – Lakshminarayanan Iyer Dec 12 '14 at 14:00
  • 1
    Run the code all at once and not by parts. – Roberto Ferrer Dec 12 '14 at 14:07

2 Answers2

1

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.

Roberto Ferrer
  • 11,024
  • 1
  • 21
  • 23
  • Testing: ``xi: xtreg log_stqf_deal `x`v'' i.year,fe`` – Roberto Ferrer Dec 12 '14 at 13:30
  • I used the same quotes as you mentioned in your comment, but STATA doesn't pick them up. I had tried this piece yesterday & fortunately STATA had picked up the regressions then. But just that one instance. Also, STATA doesn't give any errors it just runs the regression without the local macro dummies. – Lakshminarayanan Iyer Dec 12 '14 at 13:36
  • You need to give us something reproducible. We can't fix a problem we can't reproduce. Like I said, quoting the exact input/output, including the error is best. When I say **exact** it doesn't have to be your complete code, just enough of it to be representative and relevant. – Roberto Ferrer Dec 12 '14 at 13:38
  • Please, post code within your original post with appropriate formatting, using the **edit** button. Quotes are important in this case, so make sure what appears on screen is what you actually typed in Stata. This appears not to be the case with your original post. Also, if the quotes in your code are in the correct position, make sure they are actually the correct ones: backtick and single quote. – Roberto Ferrer Dec 12 '14 at 13:41
  • The spelling "STATA" was used briefly by the company in 1985 but has been superseded by the current spelling "Stata" for almost 30 years now. – Nick Cox Dec 12 '14 at 14:18
  • Even though the question did not live up to your standards, I arrived here via google and found both of your answers as well as the reproducible example extremely useful. Therefore, this thread is valuable. Maybe it is time to clean up (delete) the comments a bit for readability? I don't have permission to do that. – Jakob Sep 09 '15 at 13:47
  • 1
    @Jakob Comments can only be deleted by their authors. This implies I can only delete my own; if I did that, all other comments would be read out of context. In general, I think it's a good idea to leave them all, however long the comment thread ends up. They usually contain useful information, either complementing an answer/question, correcting, advising, or spelling out a thought process. – Roberto Ferrer Sep 09 '15 at 16:00
1

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.

Nick Cox
  • 35,529
  • 6
  • 31
  • 47
  • Thanks. I have in my question shared the local variables and the fixed effects regression specification.What more should i share? I used display to see the contents of the macro. It displays a blank screen when i run the loop shared above. – Lakshminarayanan Iyer Dec 12 '14 at 14:38
  • 1
    We don't have your data. You use (but don't explain) user-written commands as well as official commands. So, tacitly, you are expecting people answering your question to invent datasets with the same variable names and install those commands. A reproducible error would be one that anyone else could reproduce using data introduced in the question or publicly available. In this comment, you don't address the detail of whether you are using separate programs, do-files, or do-file editor windows. (For "local variables" read "local macros".) – Nick Cox Dec 12 '14 at 14:42
  • I have a combined dataset from 5 databases which is saved as .dta raw file.All the regression analyses mentioned above are part of single do file. This is separate from data preparation do files. – Lakshminarayanan Iyer Dec 12 '14 at 14:54
  • The essentials are that you define the locals **and** run the other code given in a single .do file run all at once, not in segments. If that doesn't explain what's going on, you still don't have a reproducible problem. – Nick Cox Dec 12 '14 at 15:46
  • This is a very helpful answer, Nick. Is there a way to use locals when executing chunks of your code? If not could you edit into your answer something like: "If you want to call a list of variables while running chunks of code separately, use a global instead and call it with '$yourglobal' in you regression specification". See also my comment to Robertos answer, I think this thread deserves more love. – Jakob Sep 09 '15 at 13:55
  • @Jakob I don't want to commend what I think you are suggesting. Feel free to add your own answer. – Nick Cox Sep 09 '15 at 15:43