Announcement

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

  • How to erase a series of datasets in one go?

    Hi,

    how can I delete many Stata datasets whose name follow a pattern (e.g., *tmp*.dta) in a Stata code?

  • #2
    I would get a list of file on the lines of the example here. As a second step I would attempt to loop over this list and delete them. I don't know whether that's possible but if it is, it would be nice to tokenize the list on the line break and then just use erase command on each file path.
    Kind regards,
    Konrad
    Version: Stata/IC 13.1

    Comment


    • #3
      Here's an example of how to erase such files in the current directory. See help extended_fcn.

      Code:
      sysuse auto, clear
      save tmp1.dta
      save tmp2.dta
      save 1tmp.dta
      save 2tmp.dta
      
      local list : dir . files "*tmp*.dta"
      foreach f of local list {
          erase "`f'"
      }
      Last edited by Robert Picard; 16 Aug 2014, 08:44.

      Comment


      • #4
        Hi, thanks so much for your prompt suggestions!
        I'm trying both.

        Comment


        • #5
          And if you are running on a Windows platform you can do it with
          Code:
          !del *tmp*.dta

          Comment


          • #6
            Good point Clyde. And perhaps it would be safer to see what would be deleted before going ahead. On a Mac:

            Code:
            shell ls *tmp*.dta
            which can then be followed by

            Code:
            shell rm *tmp*.dta

            Comment


            • #7
              Great and really helpful.
              Thanks a lot for your effective advice, Clyde.
              Appreciate your considerate information on Mac system, Robert.

              Comment


              • #8
                Originally posted by Robert Picard View Post
                Here's an example of how to erase such files in the current directory. See help extended_fcn.

                Code:
                sysuse auto, clear
                save tmp1.dta
                save tmp2.dta
                save 1tmp.dta
                save 2tmp.dta
                
                local list : dir . files "*tmp*.dta"
                foreach f of local list {
                erase "`f'"
                }
                More question: what if their names are unrelated?

                Comment


                • #9

                  I need to download .data files from zipped files. These files end in "500.dta" Some zipped files have "´" in their names, which does not allow the process to be carried out.
                  Code:
                  clear all
                  set more off
                  
                  global base ""
                  cd $base
                  
                  **
                  local cod_encuesta "687 634 603 546 498 440 404 324 291 279 285 284 283 282 281 280"
                  foreach x of local cod_encuesta {
                  copy http://iinei.inei.gob.pe/iinei/srienaho/descarga/STATA/`x'-Modulo05.zip `x'-Modulo05.zip,replace  
                          }
                          
                  *ssc install filelist /*Instalar por unica vez */
                  local cod_encuesta " 687 634 603 546 440 404 291 279 285 284 283 282 281 280"
                  foreach x of local cod_encuesta {
                  ******descomprimiendo**************
                  
                  version 14: unzipfile "`x'-Modulo05.zip", replace
                  
                  filelist , dir("$temp2\`x'-Modulo05") pattern(*.dta) norecur
                  tempfile file500
                  save "`file500'"
                  
                  cap mkdir MOD500_stata
                  
                  use "`file500'",clear
                  gen nombre=substr(filename,1,17) if regexm(filename, "500.dta")
                  drop if nombre==""
                  cap mkdir MOD500_stata
                  
                   local source = nombre
                   local f = dirname + "\" + filename
                   clear
                   use  "`f'"
                   save "MOD500_stata/`source'.dta", replace
                   
                   erase "`x'-Modulo05.zip"  /*elimina zipeado*/
                   shell rd "`x'-Modulo05" /s /q /*elimina carpeta*/
                   
                   }
                  
                  exit
                  
                  *********************************************************************
                  Click image for larger version

Name:	Captura.PNG
Views:	1
Size:	38.9 KB
ID:	1584955

                  Last edited by Andrés Lahur Talavera Cuya; 06 Dec 2020, 22:54.

                  Comment

                  Working...
                  X