Announcement

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

  • New variables created via do file disappearing?

    I have an issue that I suspect is a hardware or computer configuration problem and NOT Stata. I'm posting it to Statalist to see if others have run into the same problem so I might help IT here figure out a solution.

    I'm working with Add Health data on a secure machine that is accessed via Remote Desktop. Everything was running fine until after the winter break when I started getting errors related to variables created in my do file. The screenshot below shows one such error. In this example, the do file creates a school poverty measure named "spoverty" by taking the average of 10 different estimates (pov1 thru pov10). The do file also reports descriptive statistics for the new variable and ends there. Immediately after running the do file, if I try to use the "spoverty" variable I just created, it's not there. I.e., from the command line I tried to run the same descriptive statistics and Stata reports that it can't find the variable; indeed, the variable is not in the data set though it surely was when running commands in the do file just seconds before.

    Thanks in advance if you've had a similar experience and know the cause, or if you have diagnostic strategies I might try. Part of the challenge is that I can only access the data via Remote Desktop due to data license restrictions.

    Cheers,
    John

    Click image for larger version

Name:	Stata-error-perplexing-me-2.PNG
Views:	1
Size:	41.3 KB
ID:	1700561

  • #2
    HI John,

    Can you add
    Code:
    desc
    at the end of your do-file and rerun
    Code:
    desc
    ?

    Comment


    • #3
      Forgot to add, the path structure seems rather complicated. I wonder if you have the same issue if you ran the code on under "C:\AddHealth\add_health_data".

      Comment


      • #4
        One possibliity: somewhere earlier in your do-file you used a preserve command which was not followed by a later restore command. Exiting the do-file will first restore the data (from before you created spoverty).
        Code:
        . sysuse auto
        (1978 automobile data)
        
        . preserve
        
        . // do a lot of stuff and lose track of having done the preserve
        . generate lprice = price
        
        . summarize lprice
        
            Variable |        Obs        Mean    Std. dev.       Min        Max
        -------------+---------------------------------------------------------
              lprice |         74    6165.257    2949.496       3291      15906
        
        . 
        end of do-file
        
        . summarize lprice
        variable lprice not found
        r(111);
        Last edited by William Lisowski; 06 Feb 2023, 12:59.

        Comment


        • #5
          Originally posted by John Reynolds View Post
          the do file creates a school poverty measure named "spoverty" by taking the average of 10 different estimates (pov1 thru pov10). The do file also reports descriptive statistics for the new variable and ends there. Immediately after running the do file, if I try to use the "spoverty" variable I just created, it's not there.
          [ATTACH=CONFIG]n1700561[/ATTACH]
          This can happen if you have a preserve statement somewhere in the do-file, in which case Stata restores the original dataset once it is done executing the do-file. Perhaps you can show the entire do-file commands.

          Comment


          • #6
            Ah, I thought preserve would show restore at the end of a do-file by default if it wasn't explicit. Learned something new today!

            Comment


            • #7
              Thanks, William, Andrew, and Daniel. You were spot on. I had a preserve command buried in the middle of the very long do file from when I was debugging part of it. I commented that out and, bingo, everything is back to normal. Gotta love Statalist!
              Cheers and thanks again.
              John

              Comment

              Working...
              X