local output = "AllTables_" + string(d(`c(current_date)'), "%dCY-N-D" ) + ".xlsx" qui { clear local filesDTA : dir "`c(pwd)'" files "*.dta" local filesXLS : dir "`c(pwd)'" files "*.xls*" noi di _newline "Working..." foreach file in `filesDTA' `filesXLS' { if substr("`file'",length("`file'")-3,length("`file'"))==".dta" use "`file'", clear if substr("`file'",length("`file'")-3,length("`file'"))==".xls" | substr("`file'",length("`file'")-4,length("`file'"))==".xlsx" import excel "`file'", firstrow clear noi di "Adding `file'" local filename = substr(subinstr(subinstr(subinstr("`file'",".dta","",.),".xlsx","",.),".xls","",.),1,31) // 31 characters is the limit for Excel sheet names export excel using `output', firstrow(variables) sheet("`filename'") sheetmodify } noi di "Saved as `output'" }