Announcement

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

  • Using a loop to append files from multiple folders into one dataset

    I am using a loop to go through multiple folders (in a single parent directory) and append one file from each folder into a single combined dataset. I have looked at several past posts on here and my code seems to match suggested code for the same goal in those previous posts, but I cannot figure out why mine is not working.
    Code:
    * path for dta files
    local dta_path "C:/Users/.../Data/Working Data/Attendance"
    
    * Create macro with the list of folders 
    local folder_list `" "School 1" "School 2" "School3" "'
    
    set tracedepth 1
    set trace on
    foreach folder of local folder_list {
        clear
        tempfile building
        save `building', emptyok
        cd "`dta_path'/`folder'"
        local files: dir "." files "*all.dta"
        foreach f of local files {
            append using `building'
            save `"`building'"', replace
        }
    }
    I get a "no variables defined" error when I get to the save step. What have I done wrong here?

    (Note: I have already used -precombine- to check that these files are compatible for appending so that should not present any issues.)

  • #2
    Your code at no points actually reads any files into memory. So when it comes time to -save-, there is nothing to -save-. I suspect what you want is:

    Code:
    * path for dta files
    local dta_path "C:/Users/.../Data/Working Data/Attendance"
    
    * Create macro with the list of folders
    local folder_list `" "School 1" "School 2" "School3" "'
    
    // set tracedepth 1
    // set trace on
    foreach folder of local folder_list {
        clear
        tempfile building
        save `building', emptyok
        cd "`dta_path'/`folder'"
        local files: dir "." files "*all.dta"
        foreach f of local files {
            use "`f'", clear
            append using `building'
            save `"`building'"', replace
        }
    }

    Comment


    • #3
      Not reading any files into memory would certainly be a problem! Adding -use "`f'", clear- did the trick. (I also needed to remove the clear in the first loop, because with that in there it only pulled in the data from one file.) Thank you Clyde!

      Comment

      Working...
      X