Announcement

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

  • Tempfile.dta halting a program with r(608 error)

    Hello all:

    I wrote this wrapper program to gather death rate from strate. Close to the conclusion of the program, the tempfile.dta is getting saved physically in the working directory and halting my do file from running further. This seems to be sporadic and I can't figure out how to fix the halt. I include my code and a portion where the error seems to be halting in the trace. I see that file in my pwd and deleted it too. But that seems to work only sometimes with clearing that file.If you have ideas for getting this right, I would like to know.

    The program
    Code:
    *! program mrate v1.0.0 GV 9nov2023
    
    cap program drop mrate
    return clear
    
    program define mrate, rclass
        version 18.0
        syntax [varname(default=none)] [, CLUster(string)] 
        cap assert !missing(_st)
        if c(rc){
            di as error: "Data needs to be stset first"
        }
    * tabulate total deaths 
    *----------------------
    qui estpost tab _d 
    return local dead = e(b)[1,2]
        qui strate, per(1000) `clu' output(tempfile, replace)
        tempname tfrm
        mkf `tfrm'
        frame `tfrm' {
            use tempfile
            local r = string(_Rate[1], "%09.1fc")
            ret local drate "death rate of `r' per 1000 patient-years"
            local d
            }
    ret li
    end


    Error with trace
    Code:
              ------------------------------------------------- end st_set.Clear ---
            - exit
            --------------------------------------------------------- end st_set ---
          - exit
          ------------------------------------------------------------ end stset ---
        - qui save `"`outnam'"', `outrepl'
        = qui save `"tempfile"', replace
    file tempfile.dta cannot be modified or erased; likely cause is read-only
        directory or file
          restore
          }
          }
        - if "`jack'" !="" {
        = if "" !="" {
          return hidden scalar flag = `missflg'
          return hidden local method "jackknife"
          }
        - local rc = _rc
        - capture drop _D
        - capture drop _E
        - capture drop _SMR
        - capture drop _Lower
        - capture drop _Upper
        - capture drop _Y
        - capture drop _Rate
        - capture drop _MY
        - capture drop _NY
        - exit `rc'
        = exit 608
        ------------------------------------------------------------- end strate ---
      ---------------------------------------------------------------- end mrate ---
    r(608);
    
    end of do-file
    
    r(608);

  • #2
    Adding a cap erase tempfile.dta seems to fix it, but I would like to know why this occurs with a tempfile. Does it need to enclosed as `tempfile' perhaps?

    Comment


    • #3
      Well, what your code is working with is not a Stata tempfile. It is simply an ordinary .dta file to which you have given the name tempfile.dta. And that's really a terrible name for it given that it is not a tempfile.

      It is hard to say, beyond what the error message itself tells you, why tempfile.dta is a read-only file. You should follow-up on all the possibilities there.

      That said, if your intent is to actually use a Stata tempfile, the code below show you might do that. The first thing you need to do is pick a name for the tempfile. Anything that would serve as a local macro name will do. Since I like to give things names that say what they are or what they do, I'll use strate_output as the name
      Code:
      *! program mrate v1.0.0 GV 9nov2023
      
      cap program drop mrate
      return clear
      
      program define mrate, rclass
          version 18.0
          syntax [varname(default=none)] [, CLUster(string)]
          cap assert !missing(_st)
          if c(rc){
              di as error: "Data needs to be stset first"
          }
      * tabulate total deaths
      *----------------------
      qui estpost tab _d
      return local dead = e(b)[1,2]
          tempfile strate_output
          qui strate, per(1000) `clu' output(`strate_output')
          tempname tfrm
          mkf `tfrm'
          frame `tfrm' {
              use `strate_output'
              local r = string(_Rate[1], "%09.1fc")
              ret local drate "death rate of `r' per 1000 patient-years"
              local d
              }
      ret li
      end
      Note that when you define the tempfile, you do not surround its name in any quotes. When you access the tempfile you do surround it in local-macro quotes (` '). Note also that there is no need for a -replace- suboption in -strate-'s -output()- option. That's because, as a tempfile, its lifespan is limited to the program you create it in. Once you exit program mrate, this file will go out of existence. If you subsequently call -mrate- again, the previous `strate_output' file will no longer exist, so there will be nothing to replace. (Of course, if program -mrate- went on to do other things to `strate_output' and you needed to save it yet again, you would need the -replace- option that time.

      Note that if you do use a -tempfile-, you will not see it in your working directory. For its lifetime, it will exist in a system directory for temporary files, and you probably would not recognize the name it has in that directory either--it will be something like _ST000001.

      Comment


      • #4
        Thanks much for pointing, Clyde Schechter. It seems logical now that you show the process. I will follow your suggestion and modify the program (and another one with similar issues) accordingly.

        Comment

        Working...
        X