Announcement

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

  • Temp Variable Staying in Data Set

    Hello all,

    On a data set that I have been working on, I've been running into a problem with what appears to be a temp variable being saved somehow. FYI, I'm on Stata 14.2.

    Specifically, when I load the .dta file, I do not see any temp variables (e.g., __000004) in the variable manager, but the first time I try to create a tempvar, use egen, etc. after loading the data set, Stata responds with an error like the following:
    Code:
    variable __000004 already defined
    r(110);
    It seems as if somehow some temp variable is being saved in a hidden manner in the data set. This issue occurs when I use the data set, even after I restart Stata.

    Unfortunately, I am not able to share the data set (confidential information). I also was handed this data set to work with, so the history of the coding to create the data set is not in my reach.

    Has anybody run into this issue before? If so, do you know what is happening, and the best way to fix it?

    Thanks,
    Roger

  • #2
    I've come across this before, although off the top of my head I can't remember the circumstances.

    If you can recognise that it is something interesting and useful, you can rename it; otherwise drop it.

    Comment


    • #3
      Nick Cox thanks for the reply. I resaved the data while dropping the temp var that is error-ing out. It seems to have fixed the issue for now.

      Of interest may be that the temp variable name sometimes changes (unsure why). For example, in a current instance of Stata that I have open, the issue is var __000004. But when I reopen the data set in a fresh instance of Stata, the issue is __000000. Both times, I cannot see the tempvar in the variable manager. Weird...

      Comment


      • #4
        Hello all,
        I have experienced the same. I am using a data set in which I have created some variables using tempvars. Those tempvars were not displayed in the variable list in that data set or not even displayed when I describe the variables. But when I use that dataset in another do file, all those tempvar s are in the dataset. As Nick Cox suggested, I use drop *0000* (since I do not need any of the temporary variables) before saving the data set and it worked!

        Comment


        • #5
          Hi all,

          I'm facing the same problem, but when I try to drop the temporary variables (i.e., drop __000*) Stata doesn't find it:

          drop __000*
          variable __000* not found
          r(111);
          Any tips?

          Thanks!

          Comment


          • #6
            How many underscores do you have there? Temporary variable names start with two underscores.

            Comment


            • #7
              Thanks for the quick response Nick. There are 2 underscores there.

              The variable creating conflict is named "__00002J_lci"

              Even if I paste the name of the variable and I write drop, "drop __00002J_lci", I get the same error.

              Comment


              • #8
                What is the result of

                Code:
                ds
                ?

                Comment


                • #9
                  I get a list of all the variables in my database, but that specific temporary variable, or other __000*, are not listed.


                  Seems like it is created when I run one part of the code and eliminated as soon as the code breaks because of the error.


                  It happens while I'm using "predict" to estimate hazard ratios, after fitting a flexible parametric survival model (using STPM2 package). This same procedure works for the first model, but as soon as I run another stpm2 model + the predictions, then the error appears.

                  Comment


                  • #10
                    I don't know. Perhaps a small bug.

                    Comment


                    • #11
                      Thanks for trying to help Nick. I will try to get a better understanding of the problem and post here if I can fix it.

                      Comment


                      • #12
                        Error solved.

                        In my case, I was predicting within a database with multiple imputations, so the "ci" option after "predict" needed to be written in a different way. This error has been mentioned and solved in this other post.

                        Here is the wrong syntax:

                        Code:
                        mi predictnl hr_bmi4 = predict (hrnumerator (bmi4 1) hrdenominator (bmi1 1) timevar(t2) ci) using mi_stpm2_bmi
                        And the correct one:

                        Code:
                        mi predictnl hr_bmi4 = predict (hrnumerator (bmi4 1) hrdenominator (bmi1 1) timevar(t2)) using mi_stpm2_bmi,ci(lci uci) force

                        Thank you again Nick for trying to help!

                        Comment


                        • #13
                          [Using Stata 17] Here's a simple reproducible example in which temporary variables are saved. They don't appear in the variable list when the saved file is loaded, but they appear when the dataset is described or summarized.

                          Code:
                          clear
                          sysuse auto.dta
                          
                          /*count distinct manufacturers*/
                          gen mfr=substr(make, 1, strpos(make, " ") - 1)
                          
                          tempvar tag
                          tempvar distinct
                          egen `tag'=tag(mfr)
                          egen `distinct'=total(`tag')
                          summ `distinct'
                          
                          describe
                          
                          tempfile reuse1
                          save `reuse1'
                          
                          clear
                          use `reuse1'
                          summ
                          describe
                          
                          tab __*
                          Devra Golbe
                          Professor Emerita, Dept. of Economics
                          Hunter College, CUNY

                          Comment


                          • #14
                            In case others find it useful, I had a similar issue and I noticed that if I run a do file, or a portion thereof, that uses temporary variables and saves the data all in one go, the temporary variables are saved. If I run it without saving, and then save it (after the do file returned control to the main window), the temporary variables are not saved. So temporary variables are only temporary when control is returned to the main window. Here is a reproducible example in Stata 17.0:
                            Code:
                            clear *
                            sysuse auto
                            tempvar one
                            generate byte `one' = 1
                            save myauto.dta, replace
                            clear *
                            use myauto.dta
                            describe
                            versus
                            Code:
                            clear *
                            sysuse auto
                            tempvar one
                            generate byte `one' = 1
                            followed by
                            Code:
                            save myauto.dta, replace
                            clear *
                            use myauto.dta
                            describe
                            I opted for adding
                            Code:
                            drop __00*
                            before
                            Code:
                            save
                            and running the do file in one go again.

                            Comment


                            • #15
                              A solution is also posted here now, with a bug report: PSA: temporary variables are saved with datasets - Statalist

                              Comment

                              Working...
                              X