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 }
0 Response to Loop over several files in a directory and save each dataset in a new .dta
Post a Comment