Skip to content

Commit 294b6df

Browse files
committed
Create mergeWI.R
1 parent a75675b commit 294b6df

File tree

1 file changed

+51
-0
lines changed
  • Intro_DHSdata_Analysis/4_Using_Multiple_Files

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# *****************************************************************************************************
2+
# Program: mergeWI.do
3+
# Purpose: example shown to merge WR to IR file. Notes given on how to merge with other files.
4+
# Author: Shireen Assaf
5+
# Date last modified: July 31, 2023 by Shireen Assaf
6+
#
7+
# Notes: Older datasets sometimes do not contain the wealth index.
8+
# To include the wealth index, a WI file was created for these surveys.
9+
# This code will use the Bangladesh 1999-2000 survey as an example since model datasets do not have an WI file.
10+
#
11+
# Notes are given at the end of the file on how to perform other merges of WI file with other file types.
12+
# *****************************************************************************************************
13+
14+
library(haven)
15+
library(dplyr)
16+
library(here)
17+
here() # check your path, this is where you should have your saved datafiles
18+
19+
# open IR file
20+
IRdata <- read_dta(here("Intro_DHSdata_Analysis","BDIR41FL.dta"))
21+
22+
# generate the ID variable with the same name as in the WI file
23+
IRdata <- IRdata %>%
24+
mutate(whhid=substr(caseid,1,12))
25+
26+
# open WI file
27+
WIdata <- read_dta(here("Intro_DHSdata_Analysis","BDWI41FL.dta"))
28+
29+
# perform the merge
30+
IRdata <- merge(WIdata,IRdata,by=c("whhid"))
31+
32+
# gen the wealth variable name v190 as in other surveys
33+
IRdata <- IRdata %>%
34+
mutate(v190=wlthind5)
35+
36+
#optional: remove WI dataset after merge
37+
rm(WIdata)
38+
39+
# ******************************************************************************
40+
41+
# Merging WI file to other file types
42+
43+
# Same code above can be used to merge to KR file
44+
45+
# To merge to MR file you only need to change how the whhid and the v190 variables are generated as follows:
46+
# mutate(whhid=substr(mcaseid,1,12))
47+
# mutate(mv190=wlthind5)
48+
49+
# To merge to PR or HR files you only need to change how the whhid and the v190 variables are generated as follows:
50+
# mutate(whhid=substr(hhid,1,12))
51+
# mutate(hv270=wlthind5)

0 commit comments

Comments
 (0)