I've been using Stata for a few years now, but am fairly new to macros and loops.
I am creating individual Excel reports for 420 different geographical areas. I need a loop that will create an Excel file using the area name, and then use putexcel to insert all of the statistics for that country. For instance: I need create an Excel file named "ArgentinaBuenosAiresEast.xlsx", then save the statistics of that area under that file name, then do the same on a new Excel file for Argentina Buenos Aires North. I have created a loop using if statements that give me the statistics I need for each area, I'm just not sure how to simultaneously set a new putexcel file with the names I need each time.
I have both a string and a categorical version of the area variable, and after reading up, it seemed like "tokenize" may give me what I'm looking for. I've recreated an abbreviated version of my code below, along with the output. It seems to run, but gives me an "invalid file specification" error:
I am creating individual Excel reports for 420 different geographical areas. I need a loop that will create an Excel file using the area name, and then use putexcel to insert all of the statistics for that country. For instance: I need create an Excel file named "ArgentinaBuenosAiresEast.xlsx", then save the statistics of that area under that file name, then do the same on a new Excel file for Argentina Buenos Aires North. I have created a loop using if statements that give me the statistics I need for each area, I'm just not sure how to simultaneously set a new putexcel file with the names I need each time.
I have both a string and a categorical version of the area variable, and after reading up, it seemed like "tokenize" may give me what I'm looking for. I've recreated an abbreviated version of my code below, along with the output. It seems to run, but gives me an "invalid file specification" error:
Code:
sysuse auto levelsof make, local(make) tokenize "`make'" foreach num of numlist 1/5 { putexcel set "`1'".xlsx, sheet(Sheet1) modify putexcel A1:D3, merge putexcel E1:F1 = "Foreign:", merge bold tab foreign if rep78==`num', matcell(cellcounts) putexcel g1 = `r(N)', hcenter bold } OUTPUT . foreach num of numlist 1/5 { 2. putexcel set "`1'".xlsx, sheet(Sheet1) modify 3. putexcel A1:D3, merge 4. putexcel E1:F1 = "Foreign:", merge bold 5. tab foreign if rep78==`num', matcell(cellcounts) 6. putexcel g1 = `r(N)', hcenter bold 7. } invalid file specification
Comment