1

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.

7
  • What do you mean by "empty space". Is the resulting data empty or do you mean the file path expands to empty such that you get an error? Commented Apr 28, 2021 at 14:32
  • The resulting data is empty. Nothing appears in the dataset.. Commented Apr 28, 2021 at 14:35
  • Are you running the code above as continuous block? That is, if you run the loop separately from the lines defining your local file path and your list of files Stata will see the files list as empty, but won't tell you as much when running your loop. Commented Apr 28, 2021 at 14:42
  • 1
    Up to you! There are no answers so you'll be able to delete it. (stackoverflow.com/help/deleted-questions) Commented Apr 28, 2021 at 15:19
  • 1
    Now an upvoted answer. Commented Apr 28, 2021 at 15:50

1 Answer 1

3

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"
Sign up to request clarification or add additional context in comments.

3 Comments

This is the way.
I usually always append this way, but sometimes it is hard to if you have to filter which files you want and don't want in a folder when there is no straightforward way to do so without looping. Out of curiosity, would anyone know how much slower (assuming it is slower) appending via a for loop is compared to using this local macro method? If it is not substantially slower, I find looping to be more useful at times. If it is a lot slower, then I should stick to finding better ways to compartmentalize .dta's into folders for this method.
Not sure about speed, my guess is that it's not going to matter very much, it tends to make the code more concise though. If wildcards are not enough to specify the files you want to append, you could also consider constructing the list of files in a loop, instead of appending in a loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.