Hi. I'm using a fully updated version of Stata 18 on Windows 10 to analyse survey data, which I have declared as such using svyset.
I've been trying and failing to produce a table of mean values of a binary outcome variable (binvar) for a 12-level categorical/factor variable (catvar) and Wald tests of the differences between each pair of catvar level mean values after (or as part of) running the following loop:
I think the very talented Andrew Musau comes close to what I’m trying to achieve in his reply to Statalist post 1737126-exporting-svy-mean-and-test-results, except that solution is for a table of mean values of three different variables for two groups (0.black and 1.black) and Wald test results for the differences between each of the group mean values for the three variables. Andrew's code follows:
Using the table produced by the code above as an example of what might be possible, I would like to produce and export a table (to Word or Excel) featuring the mean values, standard errors, and 95% confidence intervals for each of the 66 pairs of catvar levels and the p value for each of the 66 Wald tests of the differences between these pairs of mean values. Something like this:
I would also like to export the resulting table to Word (or Excel if that’s easier) and, ideally, fix my loop, above, so it doesn’t duplicate all the Wald tests I need and include invalid Wald tests of the difference between the mean value for the same level of catvar, e.g. test c.binvar@2.catvar = c.binvar@2.catvar.
However, fixing the loop is secondary to producing and exporting the table, because I can probably cope with deleting rows of a table exported into Word or Excel, if necessary.
Please let me know if I need to provide more information to help inform a solution.
Thank you very much for considering this post.
I've been trying and failing to produce a table of mean values of a binary outcome variable (binvar) for a 12-level categorical/factor variable (catvar) and Wald tests of the differences between each pair of catvar level mean values after (or as part of) running the following loop:
Code:
svy: mean binvar , over(catvar)
levelsof catvar, local(levels)
foreach l of local levels{
local lev1 : label (catvar) `l'
levelsof catvar, local(levels)
foreach m of local levels{
local lev2 : label (catvar) `m'
di "T tests for: `lev1' vs `lev2' "
test c.binvar@`l'.catvar = c.binvar@`m'.catvar
}
}
Code:
webuse nhanes2f, clear
svyset psuid [pweight=finalwgt], strata(stratid)
estimates clear
eststo m1: svy: mean zinc age weight, over(black)
local i 0
foreach var in zinc age weight{
local ++i
test _b[c.`var'@0.black] = _b[c.`var'@1.black]
mat F`i'= `:di %3.2f `r(F)''
mat colname F`i'= "c.`var'@1.black"
mat pval`i'= `r(p)'
mat colname pval`i'= "c.`var'@1.black"
}
mat F= F1,F2,F3
mat pval= pval1,pval2,pval3
estadd mat F: m1
estadd mat pval: m1
esttab m1, cells("b F(star pval(pval))") starl(* 0.1 ** 0.05 *** 0.01) nonumb mlab(none) collab(Coef. F-stat.) label
Using the table produced by the code above as an example of what might be possible, I would like to produce and export a table (to Word or Excel) featuring the mean values, standard errors, and 95% confidence intervals for each of the 66 pairs of catvar levels and the p value for each of the 66 Wald tests of the differences between these pairs of mean values. Something like this:
| Mean | SE | 95% CI | p value (Prob > F) | |
| c.binvar@1.catvar | [blank] | |||
| c.binvar@2.catvar | ||||
| c.binvar@1.catvar | [blank] | |||
| c.binvar@3.catvar | ||||
| c.binvar@1.catvar | [blank] | |||
| c.binvar@4.catvar | ||||
| c.binvar@1.catvar | [blank] | |||
| ... [many missing rows] | ||||
| c.binvar@11.catvar | [blank] | |||
| c.binvar@12.catvar |
However, fixing the loop is secondary to producing and exporting the table, because I can probably cope with deleting rows of a table exported into Word or Excel, if necessary.
Please let me know if I need to provide more information to help inform a solution.
Thank you very much for considering this post.

Comment