Can someone help me create a table in the easiest way? I'm really confused in several places as my total count isn’t showing directly at the top. Also, if I have the count variable at the top and in the car below, what should I do? I’m not sure how to use the correct method. I’ve tried table, collect, dtable, and collapse, but I still don’t understand.
// First One
br f2_q1_1 f2_q1_2 f2_q1_3 f2_q1_5
egen icu= anycount(f2_q1_1 f2_q1_2 f2_q1_3 f2_q1_5) ,v(1)
egen hdu= anycount(f2_q1_4 f2_q1_11 ) ,v(1)
label var icu "Intensive Care Units (combined)"
label var hdu "High Dependency Units (combined)"
gen pulseox_available = 0
foreach var of varlist f2a_q87 f2b_q87 f2c_q87 f2d_q87 f2e_q87 f2f_q87 f2g_q87 f2h_q87 f2i_q87 f2j_q87 f2k_q87 f2l_q87 {
replace pulseox_available = 1 if `var' == 1 | `var' == 2
}
label var pulseox_available "Wards: Availability of Pulse Oximeter"
label val pulseox_available bin_lab
keep if province == "Sindh"
gen total_wards = icu + hdu
label var total_wards "Total Wards (ICU + HDU)"
replace pulseox_available = 1 if missing(pulseox_available)
label define pulseox_lbl 1 "Total" 0 "Pulse No" 1 "Pulse Yes"
label values pulseox_available pulseox_lbl
collapse (sum) icu hdu total_wards , by(pulseox_available
// Second One
* Define your variables
local vars pulseox_available power_supply_available power_supply_stabilizer power_supply_suppressor
* Export using putexcel
putexcel set "multi_table.xlsx", replace
* Headers
putexcel A1 = "Variable"
putexcel B1 = "Category"
putexcel C1 = "ICU"
putexcel D1 = "HDU"
* Start row counter
local row = 2
foreach var of local vars {
* Extract variable label
local varlabel : variable label `var'
if "`varlabel'" == "" local varlabel "`var'"
* Get totals first
tabstat icu hdu, stat(sum) save
local icu_total = r(StatTotal)[1,1]
local hdu_total = r(StatTotal)[1,2]
* Get unique levels of the variable
quietly levelsof `var', local(levels)
local ngroups : word count `levels'
* Get sums by group
tabstat icu hdu, by(`var') stat(sum) save
if `ngroups' == 1 {
local onlylevel : word 1 of `levels'
if `onlylevel' == 0 {
local icu_no = r(Stat1)[1,1]
local hdu_no = r(Stat1)[1,2]
local icu_yes = 0
local hdu_yes = 0
}
else {
local icu_no = 0
local hdu_no = 0
local icu_yes = r(Stat1)[1,1]
local hdu_yes = r(Stat1)[1,2]
}
}
else {
local icu_no = r(Stat1)[1,1]
local hdu_no = r(Stat1)[1,2]
local icu_yes = r(Stat2)[1,1]
local hdu_yes = r(Stat2)[1,2]
}
* Calculate percentages
local pct_icu_no = round((`icu_no' / `icu_total') * 100, 0.1)
local pct_hdu_no = round((`hdu_no' / `hdu_total') * 100, 0.1)
local pct_icu_yes = round((`icu_yes' / `icu_total') * 100, 0.1)
local pct_hdu_yes = round((`hdu_yes' / `hdu_total') * 100, 0.1)
* Format counts - trim spaces
local icu_no_f = strtrim("`: display %9.0f `icu_no''")
local hdu_no_f = strtrim("`: display %9.0f `hdu_no''")
local icu_yes_f = strtrim("`: display %9.0f `icu_yes''")
local hdu_yes_f = strtrim("`: display %9.0f `hdu_yes''")
local icu_tot_f = strtrim("`: display %9.0f `icu_total''")
local hdu_tot_f = strtrim("`: display %9.0f `hdu_total''")
* Format percentages - trim spaces
local pct_icu_no_f = strtrim("`: display %9.1f `pct_icu_no''")
local pct_hdu_no_f = strtrim("`: display %9.1f `pct_hdu_no''")
local pct_icu_yes_f = strtrim("`: display %9.1f `pct_icu_yes''")
local pct_hdu_yes_f = strtrim("`: display %9.1f `pct_hdu_yes''")
* Write variable label
putexcel A`row' = "`varlabel'"
* No row
putexcel B`row' = "No"
putexcel C`row' = "`icu_no_f'(`pct_icu_no_f'%)"
putexcel D`row' = "`hdu_no_f'(`pct_hdu_no_f'%)"
local ++row
* Yes row
putexcel A`row' = ""
putexcel B`row' = "Yes"
putexcel C`row' = "`icu_yes_f'(`pct_icu_yes_f'%)"
putexcel D`row' = "`hdu_yes_f'(`pct_hdu_yes_f'%)"
local ++row
* Total row
putexcel A`row' = ""
putexcel B`row' = "Total"
putexcel C`row' = "`icu_tot_f'(100.0%)"
putexcel D`row' = "`hdu_tot_f'(100.0%)"
local ++row
* Blank row between variables
local ++row
}
My Excel Table
// First One
br f2_q1_1 f2_q1_2 f2_q1_3 f2_q1_5
egen icu= anycount(f2_q1_1 f2_q1_2 f2_q1_3 f2_q1_5) ,v(1)
egen hdu= anycount(f2_q1_4 f2_q1_11 ) ,v(1)
label var icu "Intensive Care Units (combined)"
label var hdu "High Dependency Units (combined)"
gen pulseox_available = 0
foreach var of varlist f2a_q87 f2b_q87 f2c_q87 f2d_q87 f2e_q87 f2f_q87 f2g_q87 f2h_q87 f2i_q87 f2j_q87 f2k_q87 f2l_q87 {
replace pulseox_available = 1 if `var' == 1 | `var' == 2
}
label var pulseox_available "Wards: Availability of Pulse Oximeter"
label val pulseox_available bin_lab
keep if province == "Sindh"
gen total_wards = icu + hdu
label var total_wards "Total Wards (ICU + HDU)"
replace pulseox_available = 1 if missing(pulseox_available)
label define pulseox_lbl 1 "Total" 0 "Pulse No" 1 "Pulse Yes"
label values pulseox_available pulseox_lbl
collapse (sum) icu hdu total_wards , by(pulseox_available
// Second One
* Define your variables
local vars pulseox_available power_supply_available power_supply_stabilizer power_supply_suppressor
* Export using putexcel
putexcel set "multi_table.xlsx", replace
* Headers
putexcel A1 = "Variable"
putexcel B1 = "Category"
putexcel C1 = "ICU"
putexcel D1 = "HDU"
* Start row counter
local row = 2
foreach var of local vars {
* Extract variable label
local varlabel : variable label `var'
if "`varlabel'" == "" local varlabel "`var'"
* Get totals first
tabstat icu hdu, stat(sum) save
local icu_total = r(StatTotal)[1,1]
local hdu_total = r(StatTotal)[1,2]
* Get unique levels of the variable
quietly levelsof `var', local(levels)
local ngroups : word count `levels'
* Get sums by group
tabstat icu hdu, by(`var') stat(sum) save
if `ngroups' == 1 {
local onlylevel : word 1 of `levels'
if `onlylevel' == 0 {
local icu_no = r(Stat1)[1,1]
local hdu_no = r(Stat1)[1,2]
local icu_yes = 0
local hdu_yes = 0
}
else {
local icu_no = 0
local hdu_no = 0
local icu_yes = r(Stat1)[1,1]
local hdu_yes = r(Stat1)[1,2]
}
}
else {
local icu_no = r(Stat1)[1,1]
local hdu_no = r(Stat1)[1,2]
local icu_yes = r(Stat2)[1,1]
local hdu_yes = r(Stat2)[1,2]
}
* Calculate percentages
local pct_icu_no = round((`icu_no' / `icu_total') * 100, 0.1)
local pct_hdu_no = round((`hdu_no' / `hdu_total') * 100, 0.1)
local pct_icu_yes = round((`icu_yes' / `icu_total') * 100, 0.1)
local pct_hdu_yes = round((`hdu_yes' / `hdu_total') * 100, 0.1)
* Format counts - trim spaces
local icu_no_f = strtrim("`: display %9.0f `icu_no''")
local hdu_no_f = strtrim("`: display %9.0f `hdu_no''")
local icu_yes_f = strtrim("`: display %9.0f `icu_yes''")
local hdu_yes_f = strtrim("`: display %9.0f `hdu_yes''")
local icu_tot_f = strtrim("`: display %9.0f `icu_total''")
local hdu_tot_f = strtrim("`: display %9.0f `hdu_total''")
* Format percentages - trim spaces
local pct_icu_no_f = strtrim("`: display %9.1f `pct_icu_no''")
local pct_hdu_no_f = strtrim("`: display %9.1f `pct_hdu_no''")
local pct_icu_yes_f = strtrim("`: display %9.1f `pct_icu_yes''")
local pct_hdu_yes_f = strtrim("`: display %9.1f `pct_hdu_yes''")
* Write variable label
putexcel A`row' = "`varlabel'"
* No row
putexcel B`row' = "No"
putexcel C`row' = "`icu_no_f'(`pct_icu_no_f'%)"
putexcel D`row' = "`hdu_no_f'(`pct_hdu_no_f'%)"
local ++row
* Yes row
putexcel A`row' = ""
putexcel B`row' = "Yes"
putexcel C`row' = "`icu_yes_f'(`pct_icu_yes_f'%)"
putexcel D`row' = "`hdu_yes_f'(`pct_hdu_yes_f'%)"
local ++row
* Total row
putexcel A`row' = ""
putexcel B`row' = "Total"
putexcel C`row' = "`icu_tot_f'(100.0%)"
putexcel D`row' = "`hdu_tot_f'(100.0%)"
local ++row
* Blank row between variables
local ++row
}
My Excel Table
| Variable | Category | ICU | HDU |
| Total | N=1* | N=9 | |
| Wards: Availability of Pulse Oximeter | No | 2(10.5%) | 1(11.1%) |
| Yes | 17(8*.5%) | 8(*8.9%) |

Comment