I have the following code in Stata that I am trying to translate to python
foreach var in income_pc_r income_village_pc_r income_migration exp_total_pc_r sav_cash12_any_r sav_cash12_amtcon_pc_r anygorl_r_ffv gorl_r_ffv_condamt anygorl_g_ffv gorl_g_ffv_condamt hhsize_r migrant_r { if inlist("`var'","income_pc_r","income_village_pc_r","income_migration","exp_total_pc_r","savelast12mo_condamt","gorl_r_ffv_condamt","gorl_g_ffv_condamt") {
qui sum `var', det
local mean=string(r(mean),".0f")
local sd =string(r(sd), ".0f")
qui sum `var' if treat==0, det
local meanc=string(r(mean),".0f")
local sdc =string(r(sd), ".0f")
qui sum `var' if treat==1, det
local meant=string(r(mean),".0f")
local sdt =string(r(sd), ".0f")
file write ofile " `label`var'' & `mean' & `meanc' & `meant' \\" _n ///
" & (`sd') & (`sdc') & (`sdt') \\" _n } else {
qui sum `var', det
local mean=string(r(mean),".2f")
local sd =string(r(sd), ".2f")
qui sum `var' if treat==0, det
local meanc=string(r(mean),".2f")
local sdc =string(r(sd), ".2f")
qui sum `var' if treat==1, det
local meant=string(r(mean),".2f")
local sdt =string(r(sd), ".2f")
file write ofile " `label`var'' & `mean' & `meanc' & `meant' \\" _n ///
" & (`sd') & (`sdc') & (`sdt') \\" _n } }
Does Stata only take those observations in calculation of mean and SD for each variable, for which all the variables have a non null value? Or does it treat each variable individually?
CodePudding user response:
In this code, each variable is treated separately, so a non-missing value will be included in a calculation regardless of whether missing values exist in the same observation for other variables.
It's not your purpose, but it can be simplified.
quietly
can be said once, not repeatedly.summarize
by itself yields mean and SD; thedetail
option is overkill.Only one
file write
statement is needed.
Necessarily I can't test this rewrite.
quietly foreach var in income_pc_r income_village_pc_r income_migration exp_total_pc_r sav_cash12_any_r sav_cash12_amtcon_pc_r anygorl_r_ffv gorl_r_ffv_condamt anygorl_g_ffv gorl_g_ffv_condamt hhsize_r migrant_r {
if inlist("`var'","income_pc_r","income_village_pc_r","income_migration","exp_total_pc_r","savelast12mo_condamt","gorl_r_ffv_condamt","gorl_g_ffv_condamt") {
sum `var'
local mean=string(r(mean),".0f")
local sd =string(r(sd), ".0f")
sum `var' if treat==0
local meanc=string(r(mean),".0f")
local sdc =string(r(sd), ".0f")
sum `var' if treat==1
local meant=string(r(mean),".0f")
local sdt =string(r(sd), ".0f")
}
else {
sum `var'
local mean=string(r(mean),".2f")
local sd =string(r(sd), ".2f")
sum `var' if treat==0
local meanc=string(r(mean),".2f")
local sdc =string(r(sd), ".2f")
sum `var' if treat==1
local meant=string(r(mean),".2f")
local sdt =string(r(sd), ".2f")
}
file write ofile " `label`var'' & `mean' & `meanc' & `meant' \\" _n ///
" & (`sd') & (`sdc') & (`sdt') \\" _n
}