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.
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.)
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
}
}
(Note: I have already used -precombine- to check that these files are compatible for appending so that should not present any issues.)

Comment