Hello
I am using Stata to make tables for a project that has collected Survey data from three cities. For some of the variables I want them displayed categorically, whereas for others I want just the mean value displayed. Like, for instance, for the distribution of age (variable b02age_n) I don't want it shown with multiple rows of the different ages, and then the proportion of participants with each of these ages, but rather one row with the mean age for each of the city*demographic categories.
There are some other important features in the table, for instance, I want the total N to show for each variable
I figured out how to formulate the command for the categorical variables, but I am struggling a bit with formulating the command for mean variables.
Here is the command I use for the categorical variable:
local vars grwloc_c3
foreach var1 of local vars {
local i 0 // Reset counter for each variable
replace `var1' = 888 if `var1' == .a
replace `var1' = 999 if `var1' == .b
foreach var2 of varlist citygnd cityage citywealth {
local ++i
eststo City1`i': estpost tab `var1' `var2' if city == 1 [aw = gweightcorr_n]
eststo City2`i': estpost tab `var1' `var2' if city == 2 [aw = gweightcorr_n]
eststo City3`i': estpost tab `var1' `var2' if city == 3 [aw = gweightcorr_n]
}
local opt = cond("`var1'" == word("`vars'", 1), "replace nonumb", "append eqlab(none) nonumb collab(none)")
esttab City1* City2* City3* using cattables.csv, cells("colpct(fmt(2)) b(label(N) fmt(0))") unstack drop(Total: ) `opt' mtitles(`var1') noobs order(Total) note("Extra line") addnotes("Observations")
estimates drop _all // Clear estimates after output
}
I was able to achieve the correct display of the means by using the estpost summarize instead of the estpost tab function. The following command is the closest I have come to a command for the mean-variables
local vars b02age_n
foreach var1 of local vars {
local i 0 // Reset counter for each variable
replace `var1' = 888 if `var1' == .a
replace `var1' = 999 if `var1' == .b
foreach var2 of varlist citygnd cityage citywealth {
local ++i
// Use estpost summarize to calculate the mean
eststo City1`i': estpost summarize `var1' if city == 1 [aw = gweightcorr_n]
eststo City2`i': estpost summarize `var1' if city == 2 [aw = gweightcorr_n]
eststo City3`i': estpost summarize `var1' if city == 3 [aw = gweightcorr_n]
}
local opt = cond("`var1'" == word("`vars'", 1), "replace nonumb", "append eqlab(none) nonumb collab(none)")
esttab City1* City2* City3* using meantestdescriptives.csv, ///
cells("mean(fmt(2)) b(label(N) fmt(0))") unstack `opt' ///
mtitles(`var1') noobs note("Extra line") addnotes("Observations")
estimates drop _all
}
but this drops the rows with the total, and I cant figure out to how to incorporate this with the estpost summarize
Can you help me with this, please?
Best,
Maria
I am using Stata to make tables for a project that has collected Survey data from three cities. For some of the variables I want them displayed categorically, whereas for others I want just the mean value displayed. Like, for instance, for the distribution of age (variable b02age_n) I don't want it shown with multiple rows of the different ages, and then the proportion of participants with each of these ages, but rather one row with the mean age for each of the city*demographic categories.
There are some other important features in the table, for instance, I want the total N to show for each variable
I figured out how to formulate the command for the categorical variables, but I am struggling a bit with formulating the command for mean variables.
Here is the command I use for the categorical variable:
local vars grwloc_c3
foreach var1 of local vars {
local i 0 // Reset counter for each variable
replace `var1' = 888 if `var1' == .a
replace `var1' = 999 if `var1' == .b
foreach var2 of varlist citygnd cityage citywealth {
local ++i
eststo City1`i': estpost tab `var1' `var2' if city == 1 [aw = gweightcorr_n]
eststo City2`i': estpost tab `var1' `var2' if city == 2 [aw = gweightcorr_n]
eststo City3`i': estpost tab `var1' `var2' if city == 3 [aw = gweightcorr_n]
}
local opt = cond("`var1'" == word("`vars'", 1), "replace nonumb", "append eqlab(none) nonumb collab(none)")
esttab City1* City2* City3* using cattables.csv, cells("colpct(fmt(2)) b(label(N) fmt(0))") unstack drop(Total: ) `opt' mtitles(`var1') noobs order(Total) note("Extra line") addnotes("Observations")
estimates drop _all // Clear estimates after output
}
I was able to achieve the correct display of the means by using the estpost summarize instead of the estpost tab function. The following command is the closest I have come to a command for the mean-variables
local vars b02age_n
foreach var1 of local vars {
local i 0 // Reset counter for each variable
replace `var1' = 888 if `var1' == .a
replace `var1' = 999 if `var1' == .b
foreach var2 of varlist citygnd cityage citywealth {
local ++i
// Use estpost summarize to calculate the mean
eststo City1`i': estpost summarize `var1' if city == 1 [aw = gweightcorr_n]
eststo City2`i': estpost summarize `var1' if city == 2 [aw = gweightcorr_n]
eststo City3`i': estpost summarize `var1' if city == 3 [aw = gweightcorr_n]
}
local opt = cond("`var1'" == word("`vars'", 1), "replace nonumb", "append eqlab(none) nonumb collab(none)")
esttab City1* City2* City3* using meantestdescriptives.csv, ///
cells("mean(fmt(2)) b(label(N) fmt(0))") unstack `opt' ///
mtitles(`var1') noobs note("Extra line") addnotes("Observations")
estimates drop _all
}
but this drops the rows with the total, and I cant figure out to how to incorporate this with the estpost summarize
Can you help me with this, please?
Best,
Maria
Comment