Announcement

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

  • Multiple tempfile construction using a loop hangs

    Hello all:

    I was trying to clean my code generating 4 temp files to hold 4 sheets of an imported excel file. While executing this outside a loop works perfectly, looping the process hang after the first sheet. -set trace on- gives me a r(602) error which indicates "master.dta already exists".
    I was playing around with all possible double quotes in the local and realized I had no method to my trouble shooting after a point.

    Code:
    // Generate 1 master and 3 other temporary files to hold each of the  sheets
    *---------------------------------------------------------------------------
    
    clear
    frames reset
    
    
    local sheets "master ngs clinical transplant"
    set trace on
    foreach sheet of local sheets {
        tempfile `sheet'
        save "`sheet'", emptyok
    }

    The usual way outside the loop works perfectly as below

    Code:
    * Unlooped version works
    *------------------------
    tempfile master                // pathology data sheet--all identifier sheet 
    save "`master'" , emptyok
    
    tempfile ngs                // ngs datasheet
    tempfile clinical            // clinical datasheet
    tempfile transplant            // transplant datasheet
    save "`ngs'" , emptyok
    save "`clinical'" , emptyok
    save "`transplant'", emptyok

  • #2
    No need for double quotes.

    Code:
    local tempfiles one two three
    foreach temp in `tempfiles'{
        tempfile `temp'
        save ``temp'', emptyok
    }

    Comment


    • #3
      In your loop, you named the tempfile `sheet', which is correct. But when you try to -save "`sheet'"-, `sheet' is interpreted as master, not as `master', so that you are not actually saving into tempfile `master', you are saving into a permanent file master.dta. (Analogously for ngs, clinical, and transplant with subsequent iterations of the loop.) You might get away with this the first time you run this code, if master.dta does not exist. But once you have been through it once, master.dta, the permanent file, is there, and since your -save- command does not authorize replacing an existing file, Stata refuses.

      The solution is
      Code:
      local sheets "master ngs clinical transplant"
      set trace on
      foreach sheet of local sheets {
          tempfile `sheet'
          save "``sheet''", emptyok
      }
      When this runs, ``sheet'' parses as `master' (or `ngs', etc. in later iterations), and Stata will correctly save into that tempfile.

      Added: Crossed with #2.

      Comment


      • #4
        Originally posted by Clyde Schechter View Post
        In your loop, you named the tempfile `sheet', which is correct. But when you try to -save "`sheet'"-, `sheet' is interpreted as master, not as `master', so that you are not actually saving into tempfile `master', you are saving into a permanent file master.dta. (Analogously for ngs, clinical, and transplant with subsequent iterations of the loop.) You might get away with this the first time you run this code, if master.dta does not exist. But once you have been through it once, master.dta, the permanent file, is there, and since your -save- command does not authorize replacing an existing file, Stata refuses.

        The solution is
        Code:
        local sheets "master ngs clinical transplant"
        set trace on
        foreach sheet of local sheets {
        tempfile `sheet'
        save "``sheet''", emptyok
        }
        When this runs, ``sheet'' parses as `master' (or `ngs', etc. in later iterations), and Stata will correctly save into that tempfile.

        Added: Crossed with #2.
        The succinct explanation clears my confusion and furthers my understanding of the logic. Thanks again for taking the time explain why my code failed, Clyde Schechter

        Comment

        Working...
        X