Announcement

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

  • Exclude a single file in each folder from a loop

    I am looping through folders to -precombine- files in every folder to check for incompatibilities before appending them. I want to precombine every file in each folder except one. The files I want to use do not have consistent file names within or across folders, but the file I want to exclude in each folder ends in "_all.dta". The text before "_all.dta" is not consistent so enumerating the files in a loop and excluding based on that isn't possible. I tried using -if strpos(`"`files'"', "_all") == 0- before the precombine but I realized all that was doing was saying, if there is a file that doesn't contain "_all", precombine all the files, which doesn't help. Is there a straightforward way to exclude the file ending in "_all.dta" from the `files' macro? Or another solution?
    Code:
    foreach folder of local folder_list { 
        cd "`dta_path'/`folder'"
        local files: dir "." files "*.dta" 
        precombine `files', uniquevars describe(type ndta)
    }

  • #2
    I'm not sure I have understood how to identify the excluded file. I have interpreted what you said as saying that each folder contains (or may contain) one file whose name ends in _all.dta. No other files end that way. If that is correct, you can do this:
    Code:
    foreach folder of local folder_list {
        cd "`dta_path'/`folder'"
        local files: dir "." files "*.dta"
        local exclude: dir "." files "*_all.dta"
        local files: list files - exclude
        precombine `files', uniquevars describe(type ndta)
    }

    Comment


    • #3
      Thank you Clyde Schechter! Your interpretation of how to identify the excluded file is correct (sorry for not being clearer). I had actually tried a solution very similar to what you suggested but didn't have it quite right (I had -local exclude: dir "." exclude "*_all.dta"- instead of your -local exclude: dir "." files "*_all.dta"-). This small change did the trick.

      Comment

      Working...
      X