I am trying to import multiple Excel files (from a few different stations for the year 2013) and merge them into one temp .dta file (which then contains all stations in the year 2013) without creating and saving all separate .dta files first. So I would like to use tempfile to save all the separate Excel files as dta file, and subsequently merge these tempfiles into one temporaty dta file for the year 2013. After this I would like to repeat this process for the other folders containing data from 2014 and 2015 and in the end append all temporary year files into one saved dta file. I have researched as found code for a loop to do this however I end up with a .dta file containing only the last of the imported 2013 Excel files.
I am using Stata/IC version 12.1 on Windows 7
This is the code I used for the 2013 folder which contains "HLW_13.xls", "JVG_13.xls", "SHK_13.xls" and "VDS_13.xls":
Could you tell me what is wrong in my code?
I am using Stata/IC version 12.1 on Windows 7
This is the code I used for the 2013 folder which contains "HLW_13.xls", "JVG_13.xls", "SHK_13.xls" and "VDS_13.xls":
Code:
clear local workdir "...\MyWorkDir2013" cd `workdir' local files: dir "`workdir'" files "*.xls" foreach x in `files' { import excel using `x', firstrow clear format datum %td rename *, lower local noextension=subinstr("`x'",".xls","",.) tempfile `noextension' save "``noextension''", replace } fs *_13.dta local getfile "use" foreach file in `r(files)' { `getfile' `file' local getfile "merge 1:1 datum uur using " drop _merge } tempfile TrafficAms2013 save "`TrafficAms2013'", replace
Comment