Announcement

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

  • Delete .dta file with Stata command

    Hi,

    I would like to delete a *.dta file in my Stata code. How can I do that? Thanks


  • #2
    Code:
    help erase

    Comment


    • #3
      Thanks Nick. How can I code "erase if the file exists". I'm encountering the following error:

      Code:
      . erase publi_date_A_q_dta.dta
      file publi_date_A_q_dta.dta not found

      Comment


      • #4
        Code:
        help capture

        Comment


        • #5
          I have looked at the capture help file, but I'm not sure how to use it in this context. Can you help Rich?

          Comment


          • #6
            Try this, Francois. Change the folder definition if necessary.

            Code:
            clear
            input  x
            42
            end
            save C:\temp\junk
            
            dir C:\temp\junk.dta
            capture erase C:\temp\junk.dta
            dir C:\temp\junk.dt
            capture erase C:\temp\junk.dta
            dir C:\temp\junk.dta
            erase C:\temp\junk.dta
            --
            Bruce Weaver
            Email: [email protected]
            Version: Stata/MP 18.5 (Windows)

            Comment


            • #7
              You may find capture noisily a useful variant on capture, especially with commands that fail in more interesting ways than does erase.
              Code:
              . erase gnxl.dat
              file gnxl.dat not found
              r(601);
              
              . capture erase gnxl.dat
              
              . capture noisily erase gnxl.dat
              file gnxl.dat not found
              
              .

              Comment


              • #8
                Alternatively, manually program the error handling using capture confirm and if _rc == ... (where _rc is an internal stata macro(?) with the error return code)

                Code:
                clear all
                set obs 10
                gen x = 3
                save junk.dta, replace
                
                di _newline "Test on file that exists"
                
                ** Verify if file exists
                capture confirm file junk.dta
                
                ** .. if it does, delete program
                if _rc == 0 {
                    di "File exists, deleting ..."
                    erase junk.dta
                }
                
                ** .. else, show error code (but continue)
                else {
                    capture noisily error _rc
                    di "File keeps running though..."
                }
                
                
                di _newline "Test on file that doesn't"
                capture confirm file junk.dta
                if _rc == 0 {
                    di "File exists, deleting ..."
                    erase junk.dta
                }
                
                else {
                    capture noisily error _rc
                    di "File keeps running though..."
                }

                Comment


                • #9
                  Just wanted to point out that any command that saves or creates a (new) file has a replace option, so I do not fully get the point of this discussion.

                  Best
                  Daniel

                  Comment


                  • #10
                    From post #1

                    I would like to delete a *.dta file in my Stata code. How can I do that? Thanks
                    does not suggest to me a necessary desire to replace the existing file, only to delete it.

                    Comment


                    • #11
                      Well, given that every single answer that followed the initial one, given by Nick Cox, centers around ways to detect whether a (.dta) file exists and to erase it, if it does, and since I am not creative enough to come up with any reasonable explanation of why one would want to do that, I thought this might well be an example of the xy problem. Obviously, only Francois can really answer this.

                      I do see that people can learn from the posted code anyway, so no offense to anyone who has answered.

                      Edit: By the way, perhaps

                      Code:
                      help tempfile
                      could also be solution to the yet unknown problem of Francois.

                      Best
                      Daniel
                      Last edited by daniel klein; 10 Jul 2017, 11:14.

                      Comment


                      • #12
                        Originally posted by daniel klein View Post
                        Well, given that every single answer that followed the initial one, given by Nick Cox, centers around ways to detect whether a (.dta) file exists and to erase it, if it does, and since I am not creative enough to come up with any reasonable explanation of why one would want to do that, I thought this might well be an example of the xy problem.
                        Nice page on the XY problem, Daniel. Perhaps a link to that should be included somewhere in the FAQ.
                        --
                        Bruce Weaver
                        Email: [email protected]
                        Version: Stata/MP 18.5 (Windows)

                        Comment


                        • #13
                          In production systems that replace the output files, I may delete the output at the start of the run to preclude the old output being mistaken for the updated output if the process fails somewhere before the file gets replaced.

                          Comment


                          • #14
                            Nice page on the XY problem, Daniel. Perhaps a link to that should be included somewhere in the FAQ.
                            Actually, it is. See 9. Where may I look for other advice on posting technical questions?

                            In production systems that replace the output files, I may delete the output at the start of the run to preclude the old output being mistaken for the updated output if the process fails somewhere before the file gets replaced.
                            Makes sense.

                            Best
                            Daniel

                            Comment


                            • #15
                              Dear Daniel
                              Many thanks for your support
                              Unfortunately, I received the following


                              . net install xtdcce221 , from("https://janditzen.github.io/xtdcce2/")
                              checking xtdcce221 consistency and verifying not already installed...

                              the following files already exist and are different:
                              c:\ado\plus\x\xtset2.sthlp
                              c:\ado\plus\x\xtcd2.ado
                              c:\ado\plus\x\xtcd2.sthlp
                              c:\ado\plus\x\xtcse2.ado
                              c:\ado\plus\x\xtcse2.sthlp
                              c:\ado\plus\x\xtdcce2_auxiliary.ado

                              no files installed or copied
                              (no action taken)
                              r(602);
                              What I have to do next? remove the existing files? how?
                              Regards

                              Comment

                              Working...
                              X