Search code examples
statapostfile

Stata - postfile error


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


Solution

  • 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).