Hi folks,
I am looping over table command to generate tabulation tables in excel.
The key line in my code is:
table `row' `col', stat(percent, across(`row')) notot nformat(%4.1f) missing
Below is my full code:
* Tabulation 1: Demographic Characteristics
// Define all variables
local all_vars "province gender edu age"
// Define output file
local summary_file "Demographics Characteristics.xlsx"
// Loop through column variables
foreach col of local all_vars {
// Define row variables excluding the column variable
local row_vars: list all_vars - col
// Start creating the summary table
local first = 1
foreach row of local row_vars {
if `first' == 1 {
table `row' `col', stat(percent, across(`row')) notot nformat(%4.1f) missing
local first = 0
}
else {
table `row' `col', stat(percent, across(`row')) notot nformat(%4.1f) missing append
}
}
// Set font to "Calibri Light" and decimal format
collect style cell, font("Calibri Light") nformat(%4.1f)
// Export the summary table to "Demographics Characteristics.xlsx"
collect export "`summary_file'", sheet("`sheet_name'") modify
}
Below is my output for the province sheet.
I would appreciate your supporting in formatting my output in the following way:
1. Amend my output to have the values of the row variables in column B, and to shift the rest of columns (tabulation output) one column to the right.
2. Add a Title in BOLD in the first row, and shift the whole table a row below.
3. Make the variable names in column A BOLD.
The output should look like:
Demographics Char. By Province
I am using Stata 18.
Thanks for your support.
I am looping over table command to generate tabulation tables in excel.
The key line in my code is:
table `row' `col', stat(percent, across(`row')) notot nformat(%4.1f) missing
Below is my full code:
* Tabulation 1: Demographic Characteristics
// Define all variables
local all_vars "province gender edu age"
// Define output file
local summary_file "Demographics Characteristics.xlsx"
// Loop through column variables
foreach col of local all_vars {
// Define row variables excluding the column variable
local row_vars: list all_vars - col
// Start creating the summary table
local first = 1
foreach row of local row_vars {
if `first' == 1 {
table `row' `col', stat(percent, across(`row')) notot nformat(%4.1f) missing
local first = 0
}
else {
table `row' `col', stat(percent, across(`row')) notot nformat(%4.1f) missing append
}
}
// Set font to "Calibri Light" and decimal format
collect style cell, font("Calibri Light") nformat(%4.1f)
// Export the summary table to "Demographics Characteristics.xlsx"
collect export "`summary_file'", sheet("`sheet_name'") modify
}
Below is my output for the province sheet.
Province | ||||
1 | 2 | 3 | 4 | |
Gender | ||||
Male | 100.0 | 42.9 | 100.0 | |
Female | 57.1 | 100.0 | ||
Educational Level | ||||
Primary School | 28.6 | 100.0 | ||
High School | 100.0 | 57.1 | 100.0 | |
University Level | 14.3 | |||
Age Group | ||||
0-24 | 100.0 | |||
25-34 | 28.6 | 100.0 | ||
35-44 | 100.0 | 28.6 | ||
45-59 | 28.6 | |||
60+ | 14.3 |
1. Amend my output to have the values of the row variables in column B, and to shift the rest of columns (tabulation output) one column to the right.
2. Add a Title in BOLD in the first row, and shift the whole table a row below.
3. Make the variable names in column A BOLD.
The output should look like:
Demographics Char. By Province
Province | |||||
1 | 2 | 3 | 4 | ||
Gender | |||||
Male | 100.0 | 42.9 | 100.0 | ||
Female | 57.1 | 100.0 | |||
Educational Level | |||||
Primary School | 28.6 | 100.0 | |||
High School | 100.0 | 57.1 | 100.0 | ||
University Level | 14.3 | ||||
Age Group | |||||
0-24 | 100.0 | ||||
25-34 | 28.6 | 100.0 | |||
35-44 | 100.0 | 28.6 | |||
45-59 | 28.6 | ||||
60+ | 14.3 |
Thanks for your support.
Comment