Announcement

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

  • Simple help with global macros

    Hello,

    I believe I incorrectly posted this to the Statalist earlier today. I recognize this question is simple but I need help. I try to debug my .do files by examining datasets created within them. So, I want to understand how to create a temporary dataset that exists after completion of a do file but its erased upon Stata exit. I have been unsuccessful at accomplishing this.

    I wrote the following simple program as a way to test possibilities.

    input firm year
    10 2019
    10 2018
    11 2017
    11 2016
    end

    describe
    tempfile nextcit
    save "`nextcit'"

    global macrot "`nextcit'"
    save $macrot, replace

    describe

    use $macrot

    list

    [output after describe command]
    describe

    Contains data from C:\Users\Owner\AppData\Local\Temp\ST_459c_000001.t mp
    obs: 4
    vars: 2 31 Jan 2019 12:45
    size: 32
    ----------------------------------------------------------------------------------------------------------------
    storage display value
    variable name type format label variable label
    ----------------------------------------------------------------------------------------------------------------
    firm float %9.0g
    year float %9.0g
    ----------------------------------------------------------------------------------------------------------------
    Sorted by:

    .
    . use $macrot

    .
    . list


    +-------------+
    | firm year |
    |-------------|
    1. | 10 2019 |
    2. | 10 2018 |
    3. | 11 2017 |
    4. | 11 2016 |
    +-------------+

    -----
    However, after my .do file terminates and I type in the command window :

    . use $macrot

    and receive the following error:


    file C:\Users\Owner\AppData\Local\Temp\ST_459c_000001.t mp not found
    r(601);

    How can I access the data that I intended to store within $macrot?

    Thanks in advance for your time and consideration,

    Ed

  • #2
    Files created using -tempfile- don't exist after completion of the do-file within which they were created. The fact that you saved the name of your tempfile in a global macro doesn't change the temporary status of the file. However, if you save your dataset in a file created outside the tempfile system, then it would exist for you to examine and erase later. You can obtain a "safe" file name from the -tempname- command, stick it in a global, and then use that global to examine the file later. That file is a permanent file; the fact that its name was obtained as a tempname doesn't matter. I know that's confusing: The status of the file name, and the status of the file itself are different things.

    One potential source of confusion here is that Stata will automatically assign the file extension "dta" when a file without an extension is saved, at least under Windows. That extension won't be in the tempname itself unless we put it there. So, I'd tack the dta extension onto the tempname that you save in the global. You could also use something other than dta if you like. So, here's my suggestion, which seems a bit clunky; perhaps someone else has a nicer way to do this.

    Code:
    sysuse auto
    local MyTempDir = "c:/temp"  // this is the temp folder I use
    tempname temp1  //obtain a tempname
    global KeepName = "`MyTempDir'/`temp1'.dta"  // make the name available later. Note the .dta
    save $KeepName 
    clear
    //
    // Some time later, do something with the file. 
    use $KeepName
    desc
    erase $KeepName    // OR shell out, e.g. del *__00*.*  will delete all files with typical tempname names.


    Comment


    • #3
      ok, thank you very much for the help

      Comment

      Working...
      X