Project

General

Profile

Support #2312 » hrp_dv_mycode.txt

 
/* THIS PROGRAMME DERIVES THE HOUSEHOLD REFERENCE PERSON IDENTIFIER, W_HRPID & W_HRPNO,
IN AS CLOSE AS POSSIBLE APPROXIMATION TO THE HRP IDENTIFIER IN THE BHPS.
THE DEFINITION IS AS FOLLOWS:
"the person who owns or rents the accommodation, if greater than one person, the eldest."
there are some households in which are rented/owned by somebody not listed in the household grid.
in this case, if no other person in the household is also mentioned as owner or renter, the oldest
person in the household becomes the reference person. Age is determined based on birthy, birthm
and imputed birthd; if birthm is missing, June is assumed; if birthy is missing, survey year 1 -dvage is
assumed.
*/

// INPUT FILE - w_indall.dta & w_hhresp.dta
// OUTPUT FILE - w_hrp_dv.dta

// WRITE FILEPATH WHERE YOU HAVE DOWNLOADED THE DATA
global in "\current_release"

// WRITE FILEPATH WHERE YOU WOULD LIKE TO SAVE THE OUTPUT FILE
global outpath "\hrp_dv"

// 1 is for BHPS, 2 is for UKHLS
global cohort=2

// INCLUDE WAVE LETTERS FOR THE WAVES NEEDED
global waves a b c d e f g h i j k l m n





***** Modified hrp_dv.do file to produce w_hrpno & w_hrpid that matches the definition above

***** No change after this point *****
qui {
foreach w of global waves {
global wave "`w'_"
global num=strpos("abcdefghijklmnopqrstuvwxyz","`w'")
global dta ""
global waveno=$num
global inpath "${in}/bhps_w${num}"
if $cohort==2 {
global waveno=$num+18
*global inpath "${in}/ukhls"
global inpath "${in}/"
}



if ${cohort}==1 {
bys ${wave}hidp: gen hrpno_=${wave}pno if ${wave}hoh==1
bys ${wave}hidp: egen ${wave}hrpno=min(hrpno_)
bys ${wave}hidp: gen long hrpid_=pidp if ${wave}hoh==1
bys ${wave}hidp: egen long ${wave}hrpid=min(hrpid_)
}

if $cohort==2 {
use "${inpath}/${wave}hhresp${dta}", clear
keep ${wave}hidp ${wave}hsownd ${wave}hsowr1* ${wave}rentp*
merge 1:m ${wave}hidp using "${inpath}/${wave}indall${dta}", ///
keepusing(pidp ${wave}pno ${wave}age_dv ${wave}doby_dv ${wave}dobm_dv ///
${wave}dvage ${wave}birthy ${wave}birthm) keep(2 3) // drop hhresp but not indall
replace ${wave}dobm_dv=${wave}birthm if ${wave}dobm_dv<0 & ${wave}birthm>0
replace ${wave}doby_dv=${wave}birthy if ${wave}doby_dv<0 & ${wave}birthy>0
recode ${wave}dobm_dv -9=6
recode ${wave}doby_dv -9=.
cap gen dobd=1
gen dob=mdy(${wave}dobm_dv,dobd,${wave}doby_dv) if ${wave}dobm_dv<. & ${wave}doby_dv<.
// Identifying the renters and owners
generat renter_owner_inhh=.
generat x=.
forvalues x=1/16 {
replace renter_owner_inhh=1 if ${wave}pno==`x' & ${wave}rentp`x'==1
replace x=${wave}age_dv if ${wave}pno==`x' & ${wave}rentp`x'==1
replace renter_owner_inhh=1 if ${wave}pno==`x' & ${wave}hsowr1`x'==1
replace x=${wave}age_dv if ${wave}pno==`x' & ${wave}hsowr1`x'==1
}
// Is the renter/owner older than 16?
bys ${wave}hidp: egen renter_owner_inhh_age=max(x)
drop x
bys ${wave}hidp: egen renter_owner_inhh_num=sum(renter_owner_inhh)

// Keep the renters and owners, if they are a hh member
preserve
keep if renter_owner_inhh==1
bys ${wave}hidp (dob ${wave}pno): g x=${wave}pno if _n==1
bys ${wave}hidp (dob ${wave}pno): g long y=pidp if _n==1
keep if x<. & y<.
isid ${wave}hidp
keep ${wave}hidp x y
tempfile t1
save `t1', replace
restore

merge m:1 ${wave}hidp using `t1', nogen
bys ${wave}hidp: egen ${wave}hrpno1 =mean(x)
bys ${wave}hidp: egen long ${wave}hrpid1=mean(y)
drop x y

// Where none of the hh members are owners or renters
bys ${wave}hidp: egen ttl_renter_owner_inhh=sum(renter_owner_inhh==.)
bys ${wave}hidp: g hhsize=_N
preserve
keep if ttl_renter_owner_inhh<. & ttl_renter_owner_inhh==hhsize
bys ${wave}hidp (dob ${wave}pno): g x=${wave}pno if _n==1
bys ${wave}hidp (dob ${wave}pno): g long y=pidp if _n==1
keep if x<. & y<.
isid ${wave}hidp
keep ${wave}hidp x y
tempfile t2
save `t2', replace
restore

merge m:1 ${wave}hidp using `t2', nogen

bys ${wave}hidp: egen ${wave}hrpno2 =mean(x)
bys ${wave}hidp: egen long ${wave}hrpid2=mean(y)
drop x y


foreach var in hrpno hrpid {
assert (${wave}`var'1<. & ${wave}`var'2==.)|(${wave}`var'1==. & ${wave}`var'2<.)
rename ${wave}`var'1 ${wave}`var'
replace ${wave}`var'=${wave}`var'2 if ${wave}`var'==. & ${wave}`var'2<.
drop ${wave}`var'2
}
lab var ${wave}hrpno "Household reference person: PNO"
lab var ${wave}hrpid "Household reference person: PIDP"

rename renter_owner_inhh_num ${wave}renter_owner_inhh_num
rename renter_owner_inhh_age ${wave}renter_owner_inhh_age
keep pidp ${wave}hidp ${wave}pno ${wave}hrpno ${wave}hrpid ${wave}renter_owner_inhh_num ${wave}renter_owner_inhh_age

lab dat "Household reference person identifiers"
save "${outpath}/${wave}hrp_dv${dta}_mycode", replace

}
}
}

exit
(2-2/2)