Announcement

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

  • Retaining tempfiles outside of a program.

    Hi Everyone,

    Is there a way of retaining tempfiles for longer than the execution of a program? I understand the purpose of a tempfile is to be deleted at the conclusion of a program, but the tempfile generation is happening as part of a sub-routine and isn't a part of the main do file. I tried using an include command but it didn't work as I had hoped.

    My include.do file is:

    Code:
    local hi "this worked"
    tempfile test1
    And when I want to execute it it would be:

    Code:
    . program drop _all
    
    . program stitch
      1. tempfile test1
      2. clear
      3. set obs 1
      4. gen strL general = "Kenobi"
      5. save `test1', replace
      6. clear
      7. end
    
    . program me
      1. include "C:\Users\dspeed\OneDrive - University of New Brunswick\Desktop\testdoh.do" 
      2. dis "`hi'"
      3. use `test1', clear
      4. end
    
    . 
    end of do-file
    
    . stitch
    number of observations (_N) was 0, now 1
    (note: file C:\Users\dspeed\AppData\Local\Temp\ST_5c_000001.tmp not found)
    file C:\Users\dspeed\AppData\Local\Temp\ST_5c_000001.tmp saved
    
    . me
    
    . local hi "this worked"
    
    . this worked
    invalid file specification
    r(198);
    
    .
    Functionally, I want to be able to define a tempfile in one program and read it in another. Is there a way of doing this?

    Cheers,

    David.

  • #2
    In short, you've captured the transient nature of temporary objects (names and files).

    To retain the contents in a permanent fashion, then you have to explicitly save them with any path and name other than one being monitored as a tempfile.

    An alternative approach using frames is to copy the frame to one named anything but a name tracked as a tempname. For example:

    Code:
    tempname aframe
    cwf `aframe'
    // do something here with your data
    
    frame copy `aframe' keepme
    cwf keepme
    // when your program ends, the frame with tempname -aframe- is destroyed, but the frame -keepme- persists.

    Comment


    • #3
      This example mimics your approach and I think accomplishes what you seek. It demonstrates that local macros (and thus tempfiles), to be shared between called programs, need to be created at a level above the called programs and passed into the programs as arguments.
      Code:
      program drop _all
      
      program stitch
      args test1
      clear
      set obs 1
      gen strL general = "Kenobi"
      save `test1', replace
      clear
      end
      
      program me
      args test1
      dis "`hi'"
      use `test1', clear
      end
      
      tempfile test1
      stitch `test1'
      describe
      me `test1'
      describe
      Code:
      . tempfile test1
      
      . stitch `test1'
      Number of observations (_N) was 0, now 1.
      (file /var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//S_54222.000001 not found)
      file /var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//S_54222.000001 saved as .dta format
      
      . describe
      
      Contains data
       Observations:             0                  
          Variables:             0                  
      Sorted by:
      
      . me `test1'
      
      
      . describe
      
      Contains data from /var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//S_54222.000001
       Observations:             1                  
          Variables:             1                  13 Jan 2023 10:00
      ------------------------------------------------------------------------------------------------
      Variable      Storage   Display    Value
          name         type    format    label      Variable label
      ------------------------------------------------------------------------------------------------
      general         strL    %9s                  
      ------------------------------------------------------------------------------------------------
      Sorted by:
      
      .

      Comment


      • #4
        Leonardo Guizzetti William Lisowski Ah - thanks for the feedback! I'll see if I can get this to work on my end, I think it will do the trick!

        Comment


        • #5
          Let me add that within each of the programs, the argument need not be assigned to a local macro with the same name as the tempfile local macro, nor need it even be referred to by name. The following code works equally well as the code in post #3. When the programs stitch and me are called, the tempfile name is expanded in its full glory and is passed into the programs as a character string, and that string is then referred to using positional notation (`1' for the first argument) or by a local macro name assigned to it (foo in this example).

          Let me also add that all this code would benefit from putting quotation marks around `test1', `foo', and `1' where they appear, in case the full path to the tempfile has an embedded space.
          Code:
          program drop _all
          
          program stitch
          args foo
          clear
          set obs 1
          gen strL general = "Kenobi"
          save `foo', replace
          clear
          end
          
          program me
          dis "`hi'"
          use `1', clear
          end
          
          tempfile test1
          stitch `test1'
          describe
          me `test1'
          describe

          Comment

          Working...
          X