Hello,
I am trying to create a loop that will export a single excel file with separate tabs. Originally I was exporting each table separately by question, but I would like to create a loop that does some of the manual work so that I do not have to combine the excel files into one single file at the end
Here is the code I was using to create excel exports:
table (Q48_1) (StartDateYr) (cohort Q55), statistic(frequency) statistic(percent, across(Q48_1))
collect export "~/Downloads/Q48_1.xlsx", sheet(Q48_1) replace
And here is the loop:
local first = 1
foreach var of varlist Q5 Q6 Q22 Q23 Q24 {
collect clear
table (`var') (StartDateYr) (cohort Q55), statistic(frequency) statistic(percent, across(`var'))
if `first' {
collect export "YS_TEST_v2.xlsx", sheet("`var'") replace
local first = 0
} else {
collect export "YS_TEST_v2.xlsx", sheet("`var'") modify
}
}
When I use the code above, my export only has a single tab (for Q5) and I get the error message:
program error: code follows on the same line as close brace
r(198);
Is it possible to adjust the loop to create one excel doc that has multiple tabs for each of the tables that I was exporting separately?
Should I be using putexcel or another command?
Thank you!
I am trying to create a loop that will export a single excel file with separate tabs. Originally I was exporting each table separately by question, but I would like to create a loop that does some of the manual work so that I do not have to combine the excel files into one single file at the end
Here is the code I was using to create excel exports:
table (Q48_1) (StartDateYr) (cohort Q55), statistic(frequency) statistic(percent, across(Q48_1))
collect export "~/Downloads/Q48_1.xlsx", sheet(Q48_1) replace
And here is the loop:
local first = 1
foreach var of varlist Q5 Q6 Q22 Q23 Q24 {
collect clear
table (`var') (StartDateYr) (cohort Q55), statistic(frequency) statistic(percent, across(`var'))
if `first' {
collect export "YS_TEST_v2.xlsx", sheet("`var'") replace
local first = 0
} else {
collect export "YS_TEST_v2.xlsx", sheet("`var'") modify
}
}
When I use the code above, my export only has a single tab (for Q5) and I get the error message:
program error: code follows on the same line as close brace
r(198);
Is it possible to adjust the loop to create one excel doc that has multiple tabs for each of the tables that I was exporting separately?
Should I be using putexcel or another command?
Thank you!
Comment