I am following instructions from this link on how to append Stata files via a foreach
loop. I think that it's pretty straightforward.
However, when I try to refer to each f
in datafiles
in my foreach
loop, I receive the error:
invalid `
I've set my working directory and the data is in a subfolder called csvfiles
. I am trying to call each file f
in the csvfiles
subfolder using my local macro datafiles
and then append each file to an aggregate Stata dataset called data.dta
.
I've included the code from my do
file below:
clear
local datafiles: dir "csvfiles" files "*.csv"
foreach f of local datafiles {
preserve
insheet using “csvfiles\`f'”, clear
** add syntax here to run on each file**
save temp, replace
restore
append using temp
}
rm temp
save data.dta, replace