The DHS Program User Forum
Discussions regarding The DHS Program data and results
Home » Data » Merging data files » merging IR file with KR (Csection )
Re: merging IR file with KR [message #16822 is a reply to message #16820] Thu, 07 March 2019 02:54 Go to previous messageGo to previous message
Messages: 87
Registered: February 2017
Location: Cape Town
Senior Member
This one might give you the total for stillbirths = 236

clear all
set matsize 800
set maxvar 10000
set mem 1g
cd "..."
use "MWIR7HFL", clear
set more off

**==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<> **

gen weight = v005/1000000
gen psu =    v021
gen strata = v023

**==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<> **

gen pregs = 0

forvalues i = 1/80 {
  gen cmc`i' = v017 + 80 - `i'
  gen event`i' = substr(vcal_1, `i', 1)
  gen type`i' = .
  replace type`i' = 1 if substr(vcal_1,`i',1) == "B"
  replace type`i' = 3 if substr(vcal_1,`i',1) == "T"
  replace type`i' = 2 if substr(vcal_1,`i',7) == "TPPPPPP" 
  replace pregs = pregs+1 if (substr(vcal_1,`i',1) == "B" | substr(vcal_1,`i',1) == "T")
* Drop cases with no pregnancies
drop if pregs == 0

* Decide what variables you want to keep first before the reshape, modify this list as you need to add extra variables.
keep caseid v001 v002 v003 v005 v008 v011 v013 v017 v018 v019 v021 v022 ///
v023 v024 v190 v025 v024 v106 pregs weight psu strata cmc* event* type* 

* The reshape is really really really slow if you don't select variables and cases first, and will most likely fail otherwise.
reshape long cmc event type, i(caseid) j(ix)

lab def type 1 "Birth" 2 "Stillbirth" 3 "Miscarriage/abortion"
lab val type type
lab var type "Type of pregnancy"
lab var cmc "Century month code of event"
lab var event "Calendar event code"

* Set length of calendar to use
gen callen = v018 + 59
* If calendar is aligned right (as in original dataset), use the following:
gen beg = v018
gen end = callen

* Include only the five year period
keep if ix >= beg & ix <= end

* keep only births and stillbirths
keep if type >= 1 & type <= 3

** =============== **
cap drop stillbirth
recode type (2=1 "Stillbirth") (else=0 "Not stillbirth"), gen(stillbirth)
label var stillbirth "Stillbirths"
label val stillbirth stillbirth

**==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<> **

svyset psu [pw = weight], strata(strata) vce(linearized)

**==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<>==<> **

					(Number of pregnancies of 7+ months duration)

*svy: tab v190 stillbirth, count format(%4.0f) miss
*svy: tab v190 stillbirth, percent format(%4.1f) row miss

svy: tab v190 type, count format(%4.0f) miss
svy: tab v190 type, percent format(%4.1f) row miss


catplot type v190 [iw=weight], percent(v190) stack asyvars ///
		bar(1, bcolor(gold*.6)) bar(2, bcolor(ltblue*.9)) bar(3, bcolor(red*.2)) ///
		blabel(bar, format(%9.1f) pos(center) size(small) orientation(vert)) ///
		bar(4, bcolor(gs14)) ytitle(%) title(Percentage distribution of pregnancy type) ///
		subtitle(Malawi DHS 2015-16)
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Merging all DHS recode files for one country in Stata
Next Topic: Merging IR and PR?
Goto Forum:

Current Time: Tue Jun 2 11:35:45 Eastern Daylight Time 2020