Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Invalid name Error when using post

    Hi Statalist,

    I was running regressions in loop and trying to store coefficients. I used post for the purpose but kept getting an error message of "( invalid name". I saw similar posts online and tried debugging but the error persists.

    I tried two different sets of codes and got the same errors from both of them.
    Code:
    tempname myfile
    postfile `myfile' double ID double intercept double gradient double se using myfile.dta
    quietly reg income age education ethniciy i.year i.county, robust
    post `myfile' (`id') (_b[_cons]) (_b[education]) (_se[education])
    Code:
    tempname myfile
    postfile `myfile' double ID double intercept double gradient double se using myfile.dta
    quietly reg income age education ethniciy i.year i.county, robust
    post `myfile' (`id') (`=_b[_cons]') (`=_b[education]') (`=_se[education]')
    Thanks for your helps in advance.

  • #2
    You should not have (`id') in your -post- command, as there is no local macro id defined. Alternatively, include code that does define local macro id. If that is not the source of your problem in the actual code, then you need to provide fuller and more accurate information about what you are doing. Because, in all other respects, this code runs fine in my setup.

    Perhaps it is defined somewhere in the loop you say you are using to run several regressions, but as you show nothing about it, this is idle speculation.

    Please, in the future, do not "simplify" your code to show an example. Show the actual code you ran, (including the loop in this case), exactly as you ran it--with no editing whatsoever. And also show the output you got from Stata, also exactly as Stata gives it to you. Your post leaves us to guess where in the code the error message cropped up.

    Comment


    • #3
      Hi Clyde,

      Thanks for your responses. The -post- command is used in a loop which involves many steps not related to my question. That was why I tried to simplify my code in the initial post. However, my completed codes are attached below:

      Code:
      tempname myfile postfile `myfile' double id1 double id2 double intercept double gradient double se using myfile.dta
      
      local files : dir "~Data/" files "*.dta"
      foreach file in `files' {
        use `file', clear
        save "temp1.dta",replace
      
        foreach file1 in `files' {
          use `file1', clear
          keep id education time
          rename id id2
          rename education education2
          save "temp2.dta",replace
          use "temp1.dta",clear
          merge m:m time using "temp2.dta"
          keep if _merge==3
          drop _merge
          
          local id1 = id in 1
          local id2 = id2 in 1
        
          reg income age education education2 ethniciy i.year i.county, robust  
          post `myfile' (`id1') (`id2') (_b[_cons]) (_b[education2]) (_se[education2])
        }
      }
      The error message is "( invalid name r(198)"

      Thanks!

      Comment


      • #4
        I'm sorry, this may be completely unrelated to your problem, but I just can't see past this line:
        Code:
        merge m:m time using "temp2.dta"
        m:m merges should basically never be used. You might actually want joinby; see this for instance: https://journals.sagepub.com/doi/pdf...6867X211063416

        Comment


        • #5
          Hi Hemanshu,

          Thanks for pointing this out. In fact, I need to use m:1 merge for some of the loops and 1:m merge for some other loops. I thought m:m do the work.

          Comment


          • #6
            To address the original question, perhaps (a) separating the tempname command from the postfile command and (b) enclosing `myfile' in quotation marks will resolve your problem, since your path may have spaces.
            Code:
            tempname myfile
            postfile `"`myfile'"' double id1 double id2 double intercept double gradient double se using myfile.dta
            I've used compound double quotes in the above for the greatest generality.
            Last edited by William Lisowski; 24 Oct 2022, 08:23.

            Comment


            • #7
              Hi William,

              Thanks for your suggestion.

              I tried that but received an error message of "__000000 invalid name r(198)" and failed to create the myfile.dta.



              Comment


              • #8
                I made a mistake, quotation marks around `myfile' are unnecessary since it is a tempname and not a tempfile.

                With that said, the following, based on your code in post #1, clearly works on my system.
                Code:
                . tempname myfile
                
                . macro list _myfile
                _myfile:        __000000
                
                . postfile `myfile' double id1 double id2 double intercept double gradient double se using myfile.dta, replace
                
                . sysuse auto, clear
                (1978 automobile data)
                
                . reg price weight
                
                      Source |       SS           df       MS      Number of obs   =        74
                -------------+----------------------------------   F(1, 72)        =     29.42
                       Model |   184233937         1   184233937   Prob > F        =    0.0000
                    Residual |   450831459        72  6261548.04   R-squared       =    0.2901
                -------------+----------------------------------   Adj R-squared   =    0.2802
                       Total |   635065396        73  8699525.97   Root MSE        =    2502.3
                
                ------------------------------------------------------------------------------
                       price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
                -------------+----------------------------------------------------------------
                      weight |   2.044063   .3768341     5.42   0.000     1.292857    2.795268
                       _cons |  -6.707353    1174.43    -0.01   0.995     -2347.89    2334.475
                ------------------------------------------------------------------------------
                
                . local id1 42
                
                . local id2 666
                
                . post `myfile' (`id1') (`id2') (_b[_cons]) (_b[weight]) (_se[weight])
                
                . postclose `myfile'
                
                . 
                . use myfile.dta, clear
                
                . list, clean
                
                       id1   id2    intercept    gradient          se  
                  1.    42   666   -6.7073534   2.0440626   .37683413  
                
                .

                Comment


                • #9
                  Hi William,

                  Thanks. The code you provided work on my system too.

                  I now added the following line to my code. Unfortunately, my code still doesn't work. However, I am getting a different error message from Stata. Now it says 'post __000003 not found r(111)'.
                  Code:
                  macro list _myfile
                  Any idea on this error message? The myfile.dta is created under the same directory of the files that I want to loop through.

                  Thanks!

                  Comment


                  • #10
                    Please again copy your entire code as it now stands (even though much of it is the same as in post #1) and paste it into a CODE block. Then copy from Stata's Results window everything starting with the tempfile command and ending with the error message, and paste it into a second CODE block.

                    Comment

                    Working...
                    X