1

Why do I get a "invalid name" (see last line)?

tempname vector
postfile `vector' beta_lag    beta_const /// 
              se_mvalue   se_const   /// 
          using vettore, replace

xtreg Perf lag if t>=396 & t<=408
post `vector' (_b[lag])  (_b[_cons]) ///
              (_se[lag]) (_se[_cons])

RESULT:

. (regression is ok, omitted result...)
. post `vector'  (_b[lag])  (_b[_cons]) (_se[lag]) (_se[_cons])
( invalid name

Thank you in advance

Giuseppe
  • 518
  • 10
  • 22
  • 4
    Are you running this as one block of code in the same place? If not, Stata may not be able to see the name `vector` and be choking on the ( as the next token and as an unacceptable handle. – Nick Cox Jan 16 '14 at 20:17
  • I am executing it from a .do , I select the piece of code (after loading the data) and ctrl+D. – Giuseppe Jan 17 '14 at 23:43
  • Try running the code as a whole then. – Nick Cox Jan 18 '14 at 01:12
  • That's what I am doing, obviously. It stops at that error – Giuseppe Jan 19 '14 at 17:02
  • I couldn't reproduce your problem changing only the -xtreg- statement to -regress- in the auto data set and changing the identity of the variables in the -post- statement. I would suggest a copy and paste of your -post- statement from your Stackoverflow post into the Stata editor. Also, open a new do file editor; copy and paste into that; but write the -post- statement by hand. – Steve Samuels Jan 19 '14 at 17:24
  • @Steve Samuels What do you mean "by hand" here? – Nick Cox Jan 19 '14 at 17:34

1 Answers1

0

It's not obvious what you are doing, contrary to assertion. This code segment illustrates technique and you should be able to reproduce its running without error.

webuse nlswork, clear 
xtset idcode
tempname myout 
postfile `myout' constant grade age using myout.dta
xtreg ln_w grade age 
post `myout' (_b[_cons]) (_b[grade]) (_b[age]) 
postclose `myout' 
describe using myout 

The example shows that using expressions such as _b[_cons] is fine, in case there were any doubt.

My conjecture remains that the post command cannot see the tempname (vector in your example) and is thus evaluating it as an empty string. The first token it detects is the first ( which cannot be part of an acceptable file handle, so it complains.

This problem could (will) arise if you define the tempname in a different locale. That could be for example that part of the code is run in the main interactive session and part from a do-file or do-file editor. It could also be if you select the code to run in parts (e.g. selecting individual lines and running them individually).

Nick Cox
  • 35,529
  • 6
  • 31
  • 47
  • Thank you, running the full code it works. I did not expect at all that this could be the problem, sorry for not having posted the code from the beginning, I thought the problem could be isolated to those few lines. Do you know how could run, without errors, only the part of the code from xtreg on? maybe previously defining `vector' as a global macro ? – Giuseppe Jan 20 '14 at 18:50
  • If you define `vector` as a global macro, it would be visible everywhere. Poor style, however, and it should not be necessary to that that. – Nick Cox Jan 20 '14 at 21:07
  • The thing is, the first lines of the code (which I did not report) are quite long to execute (it has to import a very big dataset), and I was not sure whether the second part (the one I reported) would work or need some adjustment. So I was trying to run only the second part each time I found something to correct. If it's not necessary, what should be the 'good practice' alternative? (tnks for your previous answer again). – Giuseppe Jan 21 '14 at 08:33
  • Same question, same answer. If you use temporary names, they must be intelligible wherever they are used, so a temporary name can't be referenced in code in which it is not defined. This is an example of a general programming principle, so nothing new. – Nick Cox Jan 21 '14 at 10:59