Search code examples
appendstata

Stata append datasets using foreach and local


I'm trying to append many files in Stata using loop. I've tried this answer: Stata: looping and appending

And below is my code.

clear
local pathdir "Data\RawData\edd"
local files: dir "`pathdir'" files "test_*.dta"

save "`pathdir'/master.dta", emptyok replace
foreach file in `files' {

    use "`pathdir'/`file'", clear
    append using "`pathdir'/master.dta"
    save "`pathdir'/master.dta", replace

}

It just gives me empty space. I'm not really sure what to do. Why doesn't this work? Thank you for any help.


Solution

  • Let me just add that looping to append files is not necessary.

    clear
    local pathdir "Data\RawData\edd"
    local files: dir "`pathdir'" files "test_*.dta"
    append using `files'
    save "whatever.dta"