Announcement

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

  • Outreg2 to Excel R(198) error diagnosis

    I am trying to run the below code in STATA 16.1. STATA is fully updated, and I've uninstalled and re-installed the outreg2 command, re-started STATA, and restarted my laptop to no avail.
    I was previously able to successfully run this code on the same laptop a few days ago. I've made no change to the below code, or the earlier part of the do-file that tells STATA where to save outputs. Note there are "" around the output file path and the file name in the problem code, which is because my output file path as a space that I cannot remove (Emily XPS13).

    Output file path: global outputs "C:\Users\Emily XPS13\Desktop\HESAanalysis\AlmudenaHESAOutputs"

    Problem code: reg lsalary16 BLACKFEMALE BLACKMALE ASIANFEMALE ASIANMALE OTHERFEMALE OTHERMALE WHITEFEMALE, robust
    outreg2 using "$outputs/TableEthnicGender.xls", replace dec(3) keep(BLACKFEMALE BLACKMALE ASIANFEMALE ASIANMALE OTHERFEMALE OTHERMALE WHITEFEMALE) label addtext(Controls, NO, University Dummies, NO, Year Dummies, NO)

    When I try to run the below problem code today, every time I'm getting an error reading:
    invalid 'XPS13'
    r(198);


    The regression itself will run fine in STATA if I just ru the reg line of code. If I run the whole thing, I get the above error. However, I can see by looking at the file where my outputs should go, that STATA is generating a txt file there with the results information, but it is failing to translate that into an Excel file to complete the action; giving me the r(198) error instead.

    I also tried to make the file a .xslx file instead just in case, but same error problem. Any further trouble shooting suggestions? Feel a bit crazy since this worked a few days ago!


  • #2
    The error message is consistent with the output path/file not being enclosed in quotation marks. If you had omitted them in your outreg2 command, I expect you would have noticed that.

    You tell us when you run the whole thing you get the error, and you tell us the regression runs fine if you just run the reg line of code. Does the regression also run fine when you run the whole thing? If it does not, that suggests your program is failing somewhere before it gets to the reg command.

    You tell us you made no change to the code you showed us, but that suggests perhaps you made a change elsewhere in your code.

    Comment


    • #3
      Yes, I've just checked and if I run the full problem code listed in my OP and again, the regresion runs fine. The error as previously described is still flagged by STATA then after the regression is run, when STATA hits the outreg2 command. I really don't think I've made any changes to the code elsewhere since it last ran fine, and I've double check all places that I think should be relevant to this issue and the correct components appear to be enclosed in quotation marks. It's very puzzling, tbh. I'll have to keep digging at this weirdness - there must surely be some typo I've hit someplace, but darned if I can spot it so far. Thanks for your initial thought at least! Appreciate it!

      Comment


      • #4
        Wow. Back in the day, my approach would be to turn to the person sitting at the terminal next to mine in the computer lab and ask them to take a look at my code, because the First Law of Reviewing Your Own Writing is that our eyes see what we intended to write, not what we actually typed. But that was.then and this is now.

        If your code is not a million lines long, consider posting it here. Perhaps someone with fresh eyes will see something you've overlooked.

        Now the important thing about posting code on Statalist is to do it code delimiters [CODE] and [/CODE], as explained in section 12 of the Statalist FAQ linked to at the top of the page. For example, the following:

        [CODE]
        // sample code
        sysuse auto, clear
        // "now not later!"
        describe
        [/CODE]

        will be presented in the post as the following, in a readable monospace font with all spaces preserved:
        Code:
        // sample code
        sysuse auto, clear
        // "now     not later!"
        describe
        On the advanced editor toolbar that appears above where you type when you post, the hash key inserts [CODE][/CODE] with the cursor between them so that you can pasted the text you copied from the Do-file editor and have it automatically surrounded.

        Comment


        • #5
          Thanks for the tip on how to post the formatted code here. Yes, pre-covid, I would have asked one of my colleagues to look over my should but not so easy these days! In the meantime, I've uninstalled and reinstalled my whole STATA 16 SE programme just in case that fixed the problem, but unfortunately, it did not. My whole do-file is very long, but I've separated out all code lines needed to run the first of the regressions that gives this strange error, and unfortunately, the problem persists. Code is below:

          Code:
              
              clear
              set more off
              clear 
              
              set mat 11000
          
              *** Emily's current laptop directories ***
          
              cd "C:\Users\Emily XPS13\Desktop\HESAanalysis"
              global maindir "C:\Users\Emily XPS13\Desktop\HESAanalysis"
              global outputs "C:\Users\Emily XPS13\Desktop\HESAanalysis\AlmudenaHESAOutputs"
              global dofiles "C:\Users\Emily XPS13\Desktop\HESAanalysis\AlmudenaDoFiles"
              global statadata "C:\Users\Emily XPS13\Desktop\HESAanalysis\AlmudenaStataData"
          
              
              *** Open Dataset *** 
              
              use "$statadata/HESA_SBM_working.dta", clear
                  set more off
                  
              *** Regression ***
          
              reg lsalary16 BLACK ASIAN OTHER, robust   
              outreg2 using "$outputs/TableEthnic.xls", replace dec(3) keep(BLACK ASIAN OTHER) label addtext(Controls, NO, University Dummies, NO, Year Dummies, NO)
          It all works correctly until STATA gets to the outreg2 line (last line). At that stage, I continue to get the error:
          invalid 'XPS13'
          r(198);

          Bit at wits end as this same set up worked about a week ago on the same machine. I can't see any obvious errors.

          Comment


          • #6
            First, let's confirm that you're using the latest version of outreg2. This is what's currently installed from SSC, it's hard to imagine you could have anything older unless you came by your copy of outreg2 from a nonstandard source.
            Code:
            . which outreg2
            /Users/lisowskiw/Library/Application Support/Stata/ado/plus/o/outreg2.ado
            *! outreg2 2.3.2  17aug2014 by [email protected]
            *! based on outreg 3.0.6/4.0.0 by [email protected]
            Added in edit: I removed here a suggestion to uninstall and reinstall outreg2 since you have already done that, per post #1.

            I don't see anything obviously wrong in your code, but I do see a way that promises to avoid the problem. Since you cd to your HESAanalysis directory, you shouldn't need to use the full path in your globals. Try
            Code:
                cd "C:\Users\Emily XPS13\Desktop\HESAanalysis"
                pwd // review this to confirm that the cd worked!
                global maindir "."
                global outputs "./AlmudenaHESAOutputs"
                global dofiles "./AlmudenaDoFiles"
                global statadata "./AlmudenaStataData"
            which - if the cd indeed works - eliminates the need for the troublesome directory name from the subsequent global macros, so the outreg2 command should never see the that directory name.

            For what it's worth, my guess is that the problem is occurring somewhere in the depths of outreg2 with an inadvertant omission of quotation marks around the using path. But two minutes perusing outreg2.ado convinced me its complicated enough that I'd never locate the problem.
            Last edited by William Lisowski; 03 Jan 2021, 09:53.

            Comment


            • #7
              outreg2 is from SSC (FAQ Advice #12). See also https://www.statalist.org/forums/help#spelling. Does this work?

              Code:
              global statadata "C:\Users\Emily XPS13\Desktop\HESAanalysis\AlmudenaStataData"
              use "$statadata/HESA_SBM_working.dta", clear
              reg lsalary16 BLACK ASIAN OTHER, robust 
              cd "C:\Users\Emily XPS13\Desktop\HESAanalysis\AlmudenaHESAOutputs"
              outreg2 using TableEthnic.xls, replace
              Bit at wits end as this same set up worked about a week ago on the same machine. I can't see any obvious errors.
              Also maybe try reinstalling outreg2 in case some files have been corrupted.

              Code:
              ssc install outreg2, replace

              Comment


              • #8
                Thanks very much, both Andrew and William. My outreg2 is the correct version, you show William, and I also tried your suggestion to simplify my file paths by removing the repetition. Whilst this does work also, it didn't resolve the error.

                Additionally, Andrew, the code you posted does run, but doesn't achieve the full formattting I was aiming for in my table, as per my initial code.

                In other words, I've diagnosed the specific problem, although still not fully resolved it. My STATA 16 SE will run the outreg2 command, generate the relavant txt file and translate it into an excel file in the correct location now. I then added the additional options that appear after "replace" in my original code per above. Every option I was using works except "label." In other words, I only get the error described when my outreg2 command includes the label option (to report the variable labels instead of the variable names in my finished table output). So, it's reassuring to know at least the specific crux of the problem.

                I'll have to revisit this tomorrow to see if there is any way to get this final issue resolved, ideally without for instance changing the variable names to be the same as the variable labels (as I don't think that would work as some of my variable labels have 2 words and in any event, would require editing a lot in my dofile for each instance of the affected variable if I had to do this, or else manually editing table labels instead.

                Comment


                • #9
                  Spaces in variable labels should not be a problem. This works for me. Check if it a general problem with variable labels for you or it is specific to your current dataset.

                  Code:
                  sysuse auto, replace
                  reg price mpg weight rep78
                  outreg2 using myfile.xls, replace label

                  If the latter, show the output of

                  Code:
                  describe BLACK ASIAN OTHER
                  from your code in # 5.

                  Comment


                  • #10
                    I only get the error described when my outreg2 command includes the label option
                    The "Occam's Razor" answer to the question of why the command failed to run the past few days is that the label option was added since the last time the command was successfully run. :-)

                    You might try contacting the author of outreg2 at the email address shown in post #6.


                    Comment


                    • #11
                      Thanks again, Andrew and William.

                      Andrew, regarding your suggested test code in post #9, it does not run in my machine. I get the same error I'm getting with my own code and data set, when I leave "label" in the final line. The result looks like this:

                      Code:
                      . sysuse auto, replace
                      (1978 Automobile Data)
                      
                      .
                      . reg price mpg weight rep78
                      
                            Source |       SS           df       MS      Number of obs   =        69
                      -------------+----------------------------------   F(3, 65)        =     12.44
                             Model |   210350667         3  70116888.9   Prob > F        =    0.0000
                          Residual |   366446292        65  5637635.26   R-squared       =    0.3647
                      -------------+----------------------------------   Adj R-squared   =    0.3354
                             Total |   576796959        68  8482308.22   Root MSE        =    2374.4
                      
                      ------------------------------------------------------------------------------
                             price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                      -------------+----------------------------------------------------------------
                               mpg |  -52.21724   83.73955    -0.62   0.535    -219.4567    115.0222
                            weight |   2.111488   .6190081     3.41   0.001      .875243    3.347732
                             rep78 |   820.8123   320.8986     2.56   0.013     179.9335    1461.691
                             _cons |  -1939.871   3622.351    -0.54   0.594    -9174.206    5294.465
                      ------------------------------------------------------------------------------
                      
                      .
                      . outreg2 using myfile.xls, replace label
                      invalid 'XPS13'
                      r(198);
                      **EDIT** Meant to add also that if I run the above and just remove the "label" command, it does run properly, just as it does with my own code and dataset. ***

                      For what it's worth, I'm also showing the output you suggested from my own original code, which looks like this. It shows what I think you were trying to confirm - that my variables do have both names and labels.

                      Code:
                      . describe BLACK ASIAN OTHER
                      
                                    storage   display    value
                      variable name   type    format     label      variable label
                      --------------------------------------------------------------------------------------------------------------------------------------------------------------
                      BLACK           float   %9.0g                 Black
                      ASIAN           float   %9.0g                 Asian
                      OTHER           float   %9.0g                 Other
                      To William's points in post #10, I definitely did not add the label option recently. I've used it throughout my v long dofile, for a paper I've been working on for some time, every time I use the outreg2 command. I wish it were so simple I will write now to the outreg2 author at any rate to see if they have any further advice. If I make any further progress or hear back from the author, I will update. Thanks again both for your thoughts!

                      Comment


                      • #12
                        When you ran the code presented in post #11, did you run it immediately after launching a new session of Stata?

                        Do you have a profile.do installed that is run each time Stata is started? If so, there will be a message like the following near the top of your Results window when you launch Stata.

                        The thrust of both of these questions is to try to see if something is being set in your environment that is causing problems in outreg2.

                        Comment


                        • #13
                          Thanks, William. The code presented in post #11 was immediately after launching a new session of Stata, yes. I've just double checked again. Closed Stata, opened Stata, and again run the same code as below:

                          Code:
                           
                           sysuse auto, replace reg price mpg weight rep78 outreg2 using myfile.xls, replace label
                          And again get the same error issue as below:

                          Code:
                          sysuse auto, replace
                          (1978 Automobile Data)
                          
                          . 
                          . reg price mpg weight rep78
                          
                                Source |       SS           df       MS      Number of obs   =        69
                          -------------+----------------------------------   F(3, 65)        =     12.44
                                 Model |   210350667         3  70116888.9   Prob > F        =    0.0000
                              Residual |   366446292        65  5637635.26   R-squared       =    0.3647
                          -------------+----------------------------------   Adj R-squared   =    0.3354
                                 Total |   576796959        68  8482308.22   Root MSE        =    2374.4
                          
                          ------------------------------------------------------------------------------
                                 price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                          -------------+----------------------------------------------------------------
                                   mpg |  -52.21724   83.73955    -0.62   0.535    -219.4567    115.0222
                                weight |   2.111488   .6190081     3.41   0.001      .875243    3.347732
                                 rep78 |   820.8123   320.8986     2.56   0.013     179.9335    1461.691
                                 _cons |  -1939.871   3622.351    -0.54   0.594    -9174.206    5294.465
                          ------------------------------------------------------------------------------
                          
                          . 
                          . outreg2 using myfile.xls, replace label
                          invalid 'XPS13' 
                          r(198);
                          Regarding your profile.do question, I'm not aware of having set one up. Was there a message you meant to include in your post #12? It seems like there was something missing after your question.
                          When I just launch Stata and do nothing else, the text in my results window looks like this (i've just redacted my Stata serial number):

                          Code:
                            ___  ____  ____  ____  ____ (R)
                           /__    /   ____/   /   ____/
                          ___/   /   /___/   /   /___/   16.1   Copyright 1985-2019 StataCorp LLC
                            Statistics/Data analysis            StataCorp
                                                                4905 Lakeway Drive
                               Special Edition                  College Station, Texas 77845 USA
                                                                800-STATA-PC        https://www.stata.com
                                                                979-696-4600        [email protected]
                                                                979-696-4601 (fax)
                          
                          Stata license: Single-user  perpetual
                          Serial number: XXXXXXXXXXXX
                            Licensed to: Emily Pfefer
                                         Independent Researcher
                          
                          Notes:
                                1. Unicode is supported; see help unicode_advice.
                                2. Maximum number of variables is set to 5,000; see help set_maxvar.
                          
                          .

                          Comment


                          • #14
                            Sorry, I neglected the example, but what you shows confirms that you do not have a profile.do. In my installation, the notes are followed by
                            Code:
                            Running /Users/lisowskiw/Library/Application Support/Stata/profile.do ...
                            which in turn is followed by the output produced by my profile.do.

                            The problem is somehow localized to your system, in that Andrew's test fails on your system but succeeds on other systems. The question is, what is different about your system, that in interaction with the label option on outreg2, an error message is presented including a portion of the path to your directory that was not specified to the command?

                            Because you uninstalled and reinstalled Stata, I think it unlikely that you've modified - in a permanent fashion - any of the settings reported by
                            Code:
                            creturn list
                            But you could though post the output of
                            Code:
                            adopath
                            display c(username)
                            which are the two places I would expect to see XPS13.

                            Grasping at straws, while you've uninstalled and reinstalled Stata, perhaps running Stata's Installation Qualification Tool may provide helpful information.

                            https://www.stata.com/support/instal...qualification/

                            It's straightforward and quick to run, and if all goes well you'll see a dialog box telling you "Qualification completed. All qualification tests passed."

                            The obvious recommendation is to use the trace command to find exactly where in outreg2 the failure occurs, although that produces a painful amount of output in the Results window, although if you're lucky you won't have to scroll back too far from where the command exits to find the error message and the command that produced it. If you try that, be sure to
                            Code:
                            set more off
                            and insert the trace command immediately before the outreg2 command.

                            Comment


                            • #15
                              Thanks, William. Example understood now and thanks for confirmation that my output in post #13 confirmed I do not have a profile.do.

                              In your post #14, you've requested to see the output of:

                              Code:
                              adopath
                              display c(username)
                              That output looks like the below. I closed Stata 16 SE and re-opened it immediately before running this just to be entirely safe.

                              Code:
                              . adopath
                              
                                  [1]  (BASE)      "C:\Program Files\Stata16\ado\base/"
                                  [2]  (SITE)      "C:\Program Files\Stata16\ado\site/"
                                  [3]              "."
                                  [4]  (PERSONAL)  "C:\Users\Emily XPS13\ado\personal/"
                                  [5]  (PLUS)      "C:\Users\Emily XPS13\ado\plus/"
                                  [6]  (OLDPLACE)  "c:\ado/"
                              
                              .
                              . display c(username)
                              Emily XPS13
                              
                              .
                              I have secondly run the Stata Installation Qualification tool, as per your advice. In this process, I confirmed in my Stata 16 SE programme (Windows installation), that I have the Stata 16.1 Dec 15 2020 revision installed, which is as expected, given that I uninstalled and reinstalled the programme after that date and there has been no more recent update available. Following the directions in the Stata IQT programme, I was able to run this successfully and did indeed get a dialogue box at the end telling me: "Qualification completed. All qualification tests passed." So, installation is correct.

                              Lastly, you have recommended that I apply the trace command immediately before my outreg2 command to locate the specific command that is producing the error. I think I have done this and identified a little more but am no closer to understanding how to remedy the problem.


                              I have run the below code:

                              Code:
                                  clear
                                  set more off
                                  clear 
                                  
                                  set mat 11000
                              
                                  *** Emily's Current Laptop Directories ***
                                  
                                  cd "C:\Users\Emily XPS13\Desktop\HESAanalysis"
                                  global maindir "C:\Users\Emily XPS13\Desktop\HESAanalysis"
                                  global outputs "C:\Users\Emily XPS13\Desktop\HESAanalysis\AlmudenaHESAOutputs"
                                  global dofiles "C:\Users\Emily XPS13\Desktop\HESAanalysis\AlmudenaDoFiles"
                                  global statadata "C:\Users\Emily XPS13\Desktop\HESAanalysis\AlmudenaStataData"
                              
                                  
                                  *** Open Dataset *** 
                                  
                                  use "$statadata/HESA_SBM_working.dta", clear
                                      set more off
                                      
                                  *** Regression ***
                              
                                  reg lsalary16 BLACK ASIAN OTHER, robust   
                                  
                                  set more off 
                                  set trace on 
                                  outreg2 using "$outputs/TableEthnic.xls", replace label dec(3) keep(BLACK ASIAN OTHER) addtext(Controls, NO, University Dummies, NO, Year Dummies, NO)
                              In the trace output in the results window, the error I've been getting precisely happens when Stata tried to open a temporary file with I think variable labels that it has created. Specifically, I can see from the below that the temp file it was trying to open was located at: C:\Users\Emily XPS13\AppData\Local\Temp\ST_4330_000003.tmp

                              Interestingly, when I opened this file path on my laptop (less the tmp file name), I could see that there was a temp file there created at the time I ran the Stata code and with a slightly different name: STD4330_000000.tmp (I've flagged the differences to the trace report file name in red here for ease of reference). So, I would guess that *for some reason,* the outreg2 command is creating a temp file with lable info with the file name STD4330_000000.tmp and then trying to open a different one for use with the file name ST_4330_000003.tmp The latter doesn't exist in my Temp file location so of course Stata cannot open it as commanded below, I guess. Although then I'm not sure why Stata sats invalid XPS13 rather than file not found or similar. Or am I interpreting the code incorrectly maybe and the issue is being unable to create a temp file with below name that is generating the error? Probably more confused now than before in a way!


                              Code:
                               ---------------------------------------------------------------------------------------------------------- end outreg2.macroUnique ---
                                  - tempname labelsave
                                  - tempfile label_file
                                  - file open `labelsave' using `label_file', write replace
                                  = file open __00000D using C:\Users\Emily XPS13\AppData\Local\Temp\ST_4330_000003.tmp, write replace
                              invalid 'XPS13'

                              Comment

                              Working...
                              X