Quantcast
Channel: Statalist
Viewing all articles
Browse latest Browse all 72895

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

$
0
0
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.)

Viewing all articles
Browse latest Browse all 72895

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>