Hello. I would like to repeat do-files for several datasets in a directory. I like to include such a loop in my master do file. I have different merge-datasets, but would likt to perform the same cleaning operations on all of them. At the moment I always copy/comment "use data1.dta" at the begin and "save data1_done.dta" at the end of every do file.

I have some problems with loading all .dta files in a local and running a loop over that local variable, finally saving new files with appended names.


Code:
******************************************************
* Generating two test data files in current directory
******************************************************
clear

* Nick Data 2005
set seed 123
set obs 100
forval j = 1/10 {
    gen v`j' = uniform()
}
save "data2005.dta", replace
clear

* Nick Data 2015
set seed 123
set obs 6
egen year = seq(), from(1991) to(1993) block(2)
gen country = cond(mod(_n, 2), "UK", "US")
gen growth = 1 + rnormal()

save "data2015.dta", replace
clear

****************************************************************
* Creating a local list of all .dta files in directory
* Doing a loop over all files, e.g. dropping first obs
* Saving each dataset in a new file, append "done" in file name
****************************************************************

local files : dir . files ".dta"

 foreach aaa of local files {
     use `aaa'
     drop if _n == 1
 save `aaa'+"_done.dta", replace
 }
PS: The test data files are unrelated to my loop problem. You can take any other two files and perform any other operation on them.