Announcement

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

  • Appending txt files using a loop

    Hi all,

    I'm using Stata 15 and trying to import and append a bunch of txt files. I obtain a list of txt files using the user written command -filelist-, these are saved in memory. I then try and loop through the list but my loop fails because I have dropped the original list from memory. How can I loop through all of the txt files and append?

    This is what code I have so far:

    Code:
    filelist
    keep if strpos(filename, "Observation") & regexm(filename, "[\.txt]")
    
    local obs = _N
    save "myfilelist.dta", replace
    
     forvalues i = 1/`obs' {
             local f = filename[`i']
                     import delimited "`f'", clear
                      append using "`f'"
           }

  • #2
    You need to store the filenames in local macros before you try to loop over them.
    Code:
    // -filelist- matches filename patterns if asked
    filelist, pattern(*.txt)
    // Store filenames in numbered locals
    local obs = r(N)
    forval i = 1/`obs' {
       local f`i' = filename[`i']
    }
    // Import each file and append it to a running temporary file.
    clear
    tempfile temp
    forval i = 1/`obs' {
       save `temp', emptyok replace
       import delimited "`f`i''", clear
       append using `temp'
    }

    Comment


    • #3
      Thanks Mike Lacy My only problem is that the folder also contains .txt files that I dont want to import. I only want to import where the name of the text file contains 'Observation'

      Comment


      • #4
        Tim Evans you can use the pattern() option to filelist, perhaps something like pattern(*Observation*.txt). See the help for the command.

        For others, filelist is a community-contributed command available via
        Code:
        net install filelist.pkg

        Comment

        Working...
        X