Announcement

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

  • -ereturn clear- not working as expected

    I know there are many ways around this, but I am wondering why Stata is copying a matrix that presumably does not exist:

    Code:
    sysuse auto, clear
    mean price
    ereturn list
    mat P1=e(b)
    matlist P1
    ereturn clear
    
    cap noi mean price if foreign==2
    cap noi mat P2=e(b)
    matlist P2
    StataNow/MP 19.5 (64-bit x86-64)
    Win 11

  • #2
    Could you post your output? I just ran that code and got the expected results:

    Code:
    . sysuse auto, clear
    (1978 automobile data)
    
    . 
    . mean price
    
    Mean estimation                             Number of obs = 74
    
    --------------------------------------------------------------
                 |       Mean   Std. err.     [95% conf. interval]
    -------------+------------------------------------------------
           price |   6165.257   342.8719      5481.914      6848.6
    --------------------------------------------------------------
    
    . 
    . mat P1=e(b)
    
    . 
    . matlist P1
    
                 |     price 
    -------------+----------
              y1 |  6165.257 
    
    . 
    . ereturn clear
    
    . 
    . 
    . 
    . cap noi mean price if foreign==2
    no observations
    
    . 
    . cap noi mat P2=e(b)
    last estimates not found
    
    . 
    . matlist P2
    P2 not found
    r(111);

    Comment


    • #3
      Very strange!

      Code:
      . sysuse auto, clear
      (1978 automobile data)
      r; t=0.01 15:12:49
      
      . mean price
      
      Mean estimation                             Number of obs = 74
      
      --------------------------------------------------------------
                   |       Mean   Std. err.     [95% conf. interval]
      -------------+------------------------------------------------
             price |   6165.257   342.8719      5481.914      6848.6
      --------------------------------------------------------------
      r; t=0.04 15:12:49
      
      . ereturn list
      
      scalars:
                     e(df_r) =  73
                   e(N_over) =  1
                        e(N) =  74
                     e(k_eq) =  1
                     e(rank) =  1
      
      macros:
                  e(cmdline) : "mean price"
                      e(cmd) : "mean"
                      e(vce) : "analytic"
                    e(title) : "Mean estimation"
                e(estat_cmd) : "estat_vce_only"
                  e(varlist) : "price"
             e(marginsnotok) : "_ALL"
               e(properties) : "b V"
      
      matrices:
                        e(b) :  1 x 1
                        e(V) :  1 x 1
                       e(sd) :  1 x 1
                       e(_N) :  1 x 1
                    e(error) :  1 x 1
      
      functions:
                   e(sample)   
      r; t=0.02 15:12:49
      
      . mat P1=e(b)
      r; t=0.00 15:12:49
      
      . matlist P1
      
                   |     price 
      -------------+----------
                y1 |  6165.257 
      r; t=0.00 15:12:49
      
      . ereturn clear
      r; t=0.00 15:12:49
      
      . 
      . cap noi mean price if foreign==2
      no observations
      r; t=0.00 15:12:49
      
      . cap noi mat P2=e(b)
      last estimates not found
      r; t=0.00 15:12:49
      
      . matlist P2
      
                   |     price 
      -------------+----------
                y1 |  6165.257 
      r; t=0.00 15:12:49
      StataNow/MP 19.5 (64-bit x86-64)
      Win 11

      Comment


      • #4
        Brian Bradfield What version of Stata are you running and on what OS?
        StataNow/MP 19.5 (64-bit x86-64)
        Win 11

        Comment


        • #5
          StataNow/MP 19.5 for Windows (64-bit x86-64), Revision 18 Feb 2026. I'm on Windows 11.

          Comment


          • #6
            Hmm... I have the same specs except I'm on revision 15apr2026.

            Can anyone using the most recent update confirm my results? If so, I'll take it to Tech Support since it might be a bug in the update.
            StataNow/MP 19.5 (64-bit x86-64)
            Win 11

            Comment


            • #7
              I just updated and re-ran it, same as before for me. Even weirder!

              Comment


              • #8
                Ok, I'll email Tech Support. Thanks, Brian.
                StataNow/MP 19.5 (64-bit x86-64)
                Win 11

                Comment


                • #9
                  Carole J. Wilson, what happens if you run the following:

                  Code:
                  mat dir
                  cap mat drop P2
                  sysuse auto, clear
                  mean price
                  ereturn list
                  mat P1=e(b)
                  matlist P1
                  ereturn clear  
                  cap noi mean price if foreign==2
                  cap noi mat P2=e(b)
                  matlist P2

                  Comment


                  • #10
                    Now it behaves as expected. But this is strange because P2 wasn't defined (I opened a new Stata window). I'll see if I can replicate in another case. Hua Peng (StataCorp) Thanks! Hopefully I just defined it earlier and forgot to drop it.
                    Last edited by Carole J. Wilson; 22 Apr 2026, 14:41.
                    StataNow/MP 19.5 (64-bit x86-64)
                    Win 11

                    Comment


                    • #11
                      Interesting.

                      Comment


                      • #12
                        Originally posted by Carole J. Wilson View Post
                        Now it behaves as expected. But this is strange because P2 wasn't defined (I opened a new Stata window). I'll see if I can replicate in another case. Hua Peng (StataCorp) Thanks! Hopefully I just defined it earlier and forgot to drop it.
                        It is possible that you had defined P2 earlier and did not drop it. If you had somehow done this, then rerunning your script would not have cleared it. This is because -sysuse, clear- would only clear the current dataset, but not the rest of the workspace, like existing matrices. It's easy enough to demonstrate this by defining P2 anywhere earlier in the program than the final line.

                        I bet that if you had started your script with -clear *- it would not give you any issues.

                        Comment


                        • #13
                          Re #12, Carole could also try the 'nuclear' option of starting her script with -cscript-, and that would guarantee a totally blank sheet, so to speak

                          Comment

                          Working...
                          X