Following is a response from DHS staff member, Tom Pullum:
There are various ways to do this merge. The following lines will work. You need to specify a workspace and your path to the data.
* merge_AR_IR_MR_PR_do_15May2024
* South Africa 2016 DHS, ZA71
* specify workspace
cd e:\DHS\DHS_data\scratch
* specify path to the data with a scalar
scalar spath="C:\Users\26216\ICF\Analysis - Shared Resources\Data\DHSdata"
local lpath=spath
* Prepare the four data files
use "`lpath'\ZAAR71FL.DTA", clear
gen cluster=hivclust
gen hh=hivnumb
gen line=hivline
gen wt=hiv05/1000000
gen in_AR=1
tab in_AR
save ARtemp.dta, replace
use "`lpath'\ZAPR71FL.DTA", clear
gen cluster=hv001
gen hh=hv002
gen line=hvidx
keep if hv117==1 | hv118==1
save PRtemp.dta, replace
use "`lpath'\ZAIR71FL.DTA", clear
gen cluster=v001
gen hh=v002
gen line=v003
save IRtemp.dta, replace
use "`lpath'\ZAMR71FL.DTA", clear
rename mv* v*
gen cluster=v001
gen hh=v002
gen line=v003
save MRtemp.dta, replace
* Append IR and MR
use IRtemp.dta, clear
append using MRtemp.dta
save IRMRtemp.dta, replace
* Merge AR with PR
use ARtemp.dta, clear
merge 1:1 cluster hh line using PRtemp.dta
keep if in_AR==1
drop _merge
* Merge with IR+MR
merge 1:1 cluster hh line using IRMRtemp.dta
keep if in_AR==1
drop _merge
* Confirm that we have all the cases in the AR file
tab in_AR,m