* do e:\DHS\Kenya\Kenya2022_table_5pt10_age_at_first_birth_do_18Dec2023.txt * Program to calculate median age at first birth, applied to the Kenya 2022 survey * The procedure can be modified for age at first marriage, etc. ******************************************************* program define calc_percentages_by_age gen cmc_first_birth=v211 gen age_first_birth=int((cmc_first_birth-v011)/12) replace age_first_birth=99 if cmc_first_birth==. gen no_births=0 replace no_births=100 if cmc_first_birth==. local lcutoffs 15 18 20 22 25 foreach lc of local lcutoffs { gen first_birth_by_`lc'=0 replace first_birth_by_`lc'=100 if age_first_birth<`lc' } replace first_birth_by_18=. if v013==1 replace first_birth_by_20=. if v013==1 replace first_birth_by_22=. if v013<=2 replace first_birth_by_25=. if v013<=2 * Ignore the totals row of the following table tabstat first_birth* no_births [fweight=v005], statistics(mean) by(v013) format(%6.1f) tabstat first_birth* no_births if v013>1 [fweight=v005], statistics(mean) format(%6.1f) tabstat first_birth* no_births if v013>2 [fweight=v005], statistics(mean) format(%6.1f) * Frequencies tab v013 [iweight=wt] total wt if v013>1 total wt if v013>2 end ******************************************************* program define calc_median_age_inner * This version is for all-women surveys scalar smedian=. * The median is r(p50) from summarize, detail summarize age_first_birth [fweight=v005] if v012>=sL & v012<=sU, detail scalar sp50=r(p50) if sp50<99 { * interpolation procedure gen dummy=. replace dummy=0 if v012>=sL & v012<=sU replace dummy=1 if v012>=sL & v012<=sU & age_first_birth=sL & v012<=sU replace dummy=1 if v012>=sL & v012<=sU & age_first_birth<=sp50 summarize dummy [fweight=v005] scalar sUU=r(mean) drop dummy scalar smedian=round(sp50+(.5-sLL)/(sUU-sLL),.1) } end ******************************************************* program define calc_median_age foreach ln in 15 20 25 30 35 40 45 { scalar sL=`ln' scalar sU=sL+4 quietly calc_median_age_inner scalar list sL sU smedian } foreach ln in 20 25 { scalar sL=`ln' scalar sU=49 quietly calc_median_age_inner scalar list sL sU smedian } end ******************************************************* ******************************************************* ******************************************************* ******************************************************* ******************************************************* * Execuation begins here use "C:\Users\26216\ICF\Analysis - Shared Resources\Data\DHSdata\KEIR8BFL.dta" , clear gen wt=v005/1000000 calc_percentages_by_age calc_median_age