Epidemiology & Technology

Easily get gender wise results of binary outcomes after survey data analysis in Stata

Problem Statement: You have a survey data and you wish to summarize your prevalence by gender


  • gender coded as 1 = male. 2 = female
  • Outcome coded as : 1 = outcome, 0 = no outcome
  • You are aware of setting up weights properly using svyset in stata: In my case – svyset distCode, fpc(NumDists) weight(wtDist2India2) vce(linearized) singleunit(missing) || cluster, weight(wtExam2Dist)

Stata Code

cap prog drop mfprevalences
prog define  mfprevalences
args  outcome
qui count if `outcome'==1 & sex==1
local countm = r(N)
qui count if `outcome'==1 & sex==2
local countf = r(N)
qui svy linearized : proportion   `outcome', over(sex) cformat(%9.7f)
	cap mat drop  aa
	mat aa = r(table)
	mat aa = aa'
	qui mat list aa
	local numMt  : di  %4.2f  aa[3,1]*100
	local numMll : di  %4.2f  aa[3,5]*100
	local numMul : di  %4.2f  aa[3,6]*100
	local numFt  : di  %4.2f  aa[4,1]*100
	local numFll : di  %4.2f  aa[4,5]*100
	local numFul : di  %4.2f  aa[4,6]*100
qui count if `outcome'==1 
local countt = r(N)
qui svy linearized : proportion   `outcome',  cformat(%9.7f)
	cap mat drop  aa
	mat aa = r(table)
	mat aa = aa'
	qui mat list aa
	local numAllt  : di  %4.2f  aa[2,1]*100
	local numAllll : di  %4.2f  aa[2,5]*100
	local numAllul : di  %4.2f  aa[2,6]*100
di "`outcome' " _column(18) `countm'  _column(22) `numMt' " (`numMll', `numMul')  "  _column(55) `countf' _column(60) "   `numFt' (`numFll', `numFul')   " _column(85) `countt' _column(90)   "     `numAllt' (`numAllll', `numAllul')"

foreach x in coVi360Ul coVi360Bl coNoVi360 coVi360prev coVi360prevSurg coVi660Ul coVi660Bl coNoVi660 coVi660prev coVi660prevSurg coVi618Ul coVi618Bl coNoVi618 coVi618prev coVi618prevSurg coVi612Ul coVi612Bl coNoVi612 coVi612prev coVi612prevSurg  {
	mfprevalences `x'

Code language: Stata (stata)


You will get the outcome variable name in first column, male total, male %(95% CI), female total, female % (95%CI), total number, total % (95% CI).

This can be copied and pasted in excel, use “Import Text” and use Fixed width import to format the results as table cleanly.

Potential customizations

Change the multiplier from *100 to *1000 or *10000 to get results per thousand, per ten thousand etc

Change the number of decimals

Related posts