For each variable, I'd like to show dis-aggregators side by side, and the loop over to show the next variable/indicator with the same dis-aggregators right displayed in the rows below. Im able to accomplish one part of the whole objective. Unfortunately not the next - at least not yet.
This is what I'd like to have:
Visit Doctor | Wealth Tertile-Lower | Wealth Tertile- Middle | Wealth Tertile-Higher | Visit Doctor | Wealth Quintile-Lowest | Wealth Quintile-Lower | Wealth Quintile-Middle | Wealth Quintile- Higher | Wealth Quintile- Highest | |
Yes | Yes | |||||||||
No | No | |||||||||
Took Medication | Wealth Tertile-Lower | Wealth Tertile- Middle | Wealth Tertile-Highest | Took Medication | Wealth Quintile-Lowest | Wealth Quintile-Lower | Wealth Quintile-Middle | Wealth Quintile-Higher | Wealth Quintile-Highest | |
Yes | Yes | |||||||||
No | No | |||||||||
To this end, I have pasted the code I am using. Note, I need weighted % and unweighted Ns. With the code below, I am able to export the first variable - visit_doc with the two wealth dis-aggregators as expected.I cant seem to get the code correct to move vertically. I've tried looping over using forvalues 1/`Rowcount' and use the correct character functions to place the output in the correct locations, but I've been unsuccessful in so far. Any thoughts would be much appreciated. Many thanks in advance!
---
Code:
local section2variables visit_doc med_consump
local disaggregator wquint wtert
local datarow=9
local Colnum = char(64 + 2) +string(`datarow')
local char = 2
foreach var in `section2variables' {
foreach disaggregator in `disaggregator' {
* Set survey weights
svyset FC [pweight=weight], strata(strata) singleunit(scaled)
estpost svy: tab `var' `disaggregator', col percent
unstack
local TotalN=e(N)
local RowCount = e(r)
matrix pc_`var'_`disaggregator'=col
local totalrow = `datarow'+`RowCount'+3
local totalcolnum = char(64+`char') + string(`totalrow')
local labelcolnum = char(64+`char') + string(`datarow')
* Table, and Variable label
putexcel `Colnum'=matrix(pc_`var'_`disaggregator'),nformat( #.00) names font(Arial,12, black) overwritefmt
putexcel `totalcolnum'="N",font(Arial,12, black) italic bold overwritefmt
putexcel `labelcolnum' = "`e(rowvlab)'"
tabulate `var' if !missing(`disaggregator'), matcell(rowtotals)
tabulate `disaggregator' if !missing(`var'), matcell(coltotals)
tabulate `var' `disaggregator', matcell(cellcounts)
local ColCount = r(c)
forvalues col = 1/`ColCount' {
* Column Ns
local Cell = char(64 + `char' + `col') + string(`totalrow')
local CellContents=coltotals[`col',1]
putexcel `Cell' = `CellContents', font(Arial,12, black) italic bold overwritefmt
}
* Total N
local TotalCell =char(64+`ColCount'+`char'+1) + string(`totalrow')
putexcel `TotalCell' = `TotalN', font(Arial,12, black) italic bold overwritefmt
local Colnum = char(64+11) +string(`datarow')
local char = `char' + 9
}
}
0 Response to Putexcel Loop over several cross tabulations
Post a Comment