Hi, I'm using Stata 14 on Windows. I am trying to export results of analysis that I save into Stata data files into Excel. When I originally started the analysis I broke the analysis into incidence ("inc") and prevalence ("pr") .dta files. This meant that I could set up a local macro to look for files beginning with "inc" or "pr". This is the code I used successfully for this part of the export routine:
I originally did this for one disease area - stroke, but now I have included a category of "TIA" and "STIA". Each file is saved with "pr" or "inc" in the filename along with "stroke", "TIA" or "STIA". I am trying to amend my code so that not only do I search for files with the "pr" or "inc" prefix, but also to loop through "stroke", "STIA" and "TIA", so that I can have an excel file each for "pr_stroke", "pr_STIA", "pr_TIA", "inc_stroke", "inc_TIA" and "inc_TIA".
I have amended my code to this:
At the moment I run the code and no output is produced or an error message for me to chase down.
Any help appreciated.
Code:
* export_to_excel.do * * TE 20170425 * * projectname * * Export .dta files into .xlsx worksheet, import .png files to worksheet cd "${outdir}" local outputtypes "inc pr" foreach outputtype of local outputtypes { local files : dir "${outdir}" files "`outputtype'*.dta" foreach f of local files { use `f', replace local tabname = regexr("`f'","\.dta","") di "`tabname'" export excel using "`outputtype'.xlsx", sheet("`tabname'") sheetreplace firstrow(variables) } }
I originally did this for one disease area - stroke, but now I have included a category of "TIA" and "STIA". Each file is saved with "pr" or "inc" in the filename along with "stroke", "TIA" or "STIA". I am trying to amend my code so that not only do I search for files with the "pr" or "inc" prefix, but also to loop through "stroke", "STIA" and "TIA", so that I can have an excel file each for "pr_stroke", "pr_STIA", "pr_TIA", "inc_stroke", "inc_TIA" and "inc_TIA".
I have amended my code to this:
Code:
cd "${outdir}" local outputtypes "inc pr" foreach outputtype of local outputtypes { local datafiles: dir "${outdir}" files "*stroke*.dta *TIA*.dta *STIA*.dta" foreach datafile of local datafiles { local files : dir "${outdir}" files "`outputtype'`datafile'" use `f', replace local tabname = regexr("`f'","\.dta","") di "`tabname'" export excel using "`outputtype'.xlsx", sheet("`tabname'") sheetreplace firstrow(variables) } }
Any help appreciated.
Comment