Blog Home!

profile picture
Milestone 1.2 - Data coding and cleaning for ELSA

Project: AgeingTimeUse, Horizon 2020 ERC

For the second strand of my research, I used the English Longitudinal Study of Ageing (ELSA), which I accessed through the UK Data Services. ELSA survey collected data starting in 2002 and interviewed people aged 50 years and older living in England. In this part of my project, I used Wave 3-8 (2006–2017) to include a few years before introducing the National Dementia Strategy in 2009 and a few years after the Dementia Challenge of 2012–2015.


Published Work Using ELSA


I published a paper using this dataset. The analysis in this paper can be replicated following the code below.

You can access it here:


Coding and Merging ELSA Waves 

In this part, I create common variables across the waves and merge all datasets into one. Some variables were not be used in the paper, but I created them nonetheless.


set maxvar 10000
    
* Coding wave 8

use "wave_8_elsa_data_eul_v2.dta", clear
    
*** Female

recode indsex (1=0)(2=1), gen(Female)

*** Education

recode fqendm (-8/-1=.)(2=0)(3=9)(4=10)(5=11)(6=12)(7=13)(8=14), gen(EduInYears)
*** Sports

recode heacta heactb heactc (-9/-8=.)

gen H1 = 5-heacta
gen H2 = 5-heactb
gen H3 = 5-heactc

alpha H1 H2 H3, i

gen Health = (H1+H2+H3)/3 
*** smoking

recode hesmk (-9/-1=.)(2=0), gen(smoked)
*** drinking

recode scal7a (-3/-1=.)(2=0), gen(drinked)

*** age

recode indager (-7=90), gen(Age)

*** married

recode couple (1/2=1)(3=0), gen(Married)

*** year

gen Year=iintdaty

*** gen ID

gen ID =idauniq

*** weight (wave 1)

gen Weight = w8w1lwgt

*** multimorbidity


*** high blood pressure

recode hedawbp (-1=0), gen(HBP1)
recode hediabp (-9/-8 =.), gen(HBP2)

gen HBP = .
replace HBP=0 if HBP1==0 | HBP2==0
replace HBP=1 if HBP1==1 | HBP2==1

*** diabetes

recode hedawdi (-1=0) (7=1), gen(Diabetes1)
recode hediadi (-9/-8=0), gen(Diabetes2)

gen Diabetes = .
replace Diabetes=0 if Diabetes1==0 | Diabetes2==0
replace Diabetes=1 if Diabetes1==1 | Diabetes2==1

*** arthritis (3) (4)


recode hedbwar (-1=0) (3=1), gen(Arthritis1)
recode hedibar (-9/-8 =.), gen(Arthritis2)

gen Arthritis = .
replace Arthritis=0 if Arthritis1==0 | Arthritis2==0
replace Arthritis=1 if Arthritis1==1 | Arthritis2==1


recode hedbwos (-1=0)(4=1), gen(Osteo1)
recode hedibos (-9/-8 =.), gen(Osteo2)

gen Osteo = .
replace Osteo=0 if Osteo1==0 | Osteo2==0
replace Osteo=1 if Osteo1==1 | Osteo2==1

replace Arthritis = 1 if Arthritis ==0 & Osteo==1

*** heart attack

recode hedawmi (-1=0)(3=1), gen(HeartAttack1)
recode hediami (-9/-8 =.), gen(HeartAttack2)

gen HeartAttack = .
replace HeartAttack=0 if HeartAttack1==0 | HeartAttack2==0
replace HeartAttack=1 if HeartAttack1==1 | HeartAttack2==1

*** stroke

recode hedawst (-1=0)(8=1), gen(Stroke1)
recode hediast (-9/-8 =.), gen(Stroke2)

gen Stroke = .
replace Stroke=0 if Stroke1==0 | Stroke2==0
replace Stroke=1 if Stroke1==1 | Stroke2==1

*** cancer

recode hedbwca (-1=0)(5=1), gen(Cancer1)
recode hedibca (-9/-8 =.), gen(Cancer2)

gen Cancer = .
replace Cancer=0 if Cancer1==0 | Cancer2==0
replace Cancer=1 if Cancer1==1 | Cancer2==1

*** lung disease

recode hedbwlu (-1=0), gen(Lung1)
recode hediblu (-9/-8 =.), gen(Lung2)

gen Lung = .
replace Lung=0 if Lung1==0 | Lung2==0
replace Lung=1 if Lung1==1 | Lung2==1


*** Parkinson's

recode hedbwpd (-1=0)(6=1), gen(Parkinson1)
recode hedibpd (-9/-8 =.), gen(Parkinson2)

gen Parkinson = .
replace Parkinson=0 if Parkinson1==0 | Parkinson2==0
replace Parkinson=1 if Parkinson1==1 | Parkinson2==1

*** Alzheimers and dementia

recode hedbwad (-1=0)(8=1), gen(Dementia1)
recode hedibad (-9/-8 =.), gen(Dementia2)

recode hedbwde (-1=0)(9=1), gen(Dementia3)
recode hedibde (-9/-8 =.), gen(Dementia4)

gen Dementia = .
replace Dementia=0 if Dementia1==0 | Dementia2==0 | Dementia3==0 | Dementia4==0
replace Dementia=1 if Dementia1==1 | Dementia2==1 |Dementia3==1 | Dementia4==1

*** affective or emotional disorders

recode hedbwps (-1=0)(7=1), gen(Psychological1)
recode hedibps (-9/-8 =.), gen(Psychological2)

gen Psychological = .
replace Psychological=0 if Psychological1==0 | Psychological2==0
replace Psychological=1 if Psychological1==1 | Psychological2==1


*** hip fracture

recode hefrac (-1/-9 2 =0), gen(Hip1)
recode hejiaa1 (-1=0), gen(Hip2)
recode hejiaa2 (-1=0), gen(Hip3)

gen Hip = .
replace Hip=0 if Hip1==0 | Hip2==0 | Hip3==0
replace Hip=1 if Hip1==1 | Hip2==1 |Hip3==1 


*** gen multimorbidity

gen Morb = HBP +Diabetes +Arthritis +HeartAttack +Stroke +Cancer +Lung +Parkinson +Dementia +Psychological +Hip

recode Morb (0/1=0)(2/max=1), gen(Multimorb)

**** some variables from the paper

gen AG = .
replace AG =1 if Age>=50 & Age<60
replace AG =2 if Age>=60 & Age<70
replace AG =3 if Age>=70 & Age<80
replace AG =4 if Age>=80

gen HouseholdYear = idahhw8
gen Region = gor
gen BirthYear = indobyr

***


keep idauniq Female EduInYears H1 H2 H3 Health smoked drinked Age Married Year ID Weight HBP1 HBP2 HBP ///
    Diabetes1 Diabetes2 Diabetes Arthritis1 Arthritis2 Arthritis Osteo1 Osteo2 Osteo HeartAttack1 ///
    HeartAttack2 HeartAttack Stroke1 Stroke2 Stroke Cancer1 Cancer2 Cancer Lung1 Lung2 Lung Parkinson1 ///
    Parkinson2 Parkinson Dementia1 Dementia2 Dementia3 Dementia4 Dementia Psychological1 Psychological2 ///
    Psychological Hip1 Hip2 Hip3 Hip Morb Multimorb AG HouseholdYear Region BirthYear

save "ELSA8.dta", replace    

    
    
* Coding wave 7

use "wave_7_elsa_data.dta", clear    
*** Female

recode indsex (1=0)(2=1), gen(Female)

*** Education

recode fffqend (-1=.)(1/2=0)(3=9)(4=10)(5=11)(6=12)(7=13)(8=14), gen(EduInYears)


*** Sports

recode HeActa HeActb HeActc (-9/-8=.)

gen H1 = 5-HeActa
gen H2 = 5-HeActb
gen H3 = 5-HeActc

gen Health = (H1+H2+H3)/3 


*** smoking

recode HeSmk (-9/-1=.)(2=0), gen(smoked)


*** drinking

recode scal7a (-9/-1=.)(2=0), gen(drinked)

*** age

recode indager (-7=90), gen(Age)

*** married

recode couple (1/2=1)(3=0), gen(Married)

*** year

gen Year=iintdaty

*** gen ID

gen ID =idauniq

*** weight (wave 1)

gen Weight = w7lwgt

*** multimorbidity


*** high blood pressure

recode hedawbp (-1=0), gen(HBP1)
recode hediabp (-9/-8 =.), gen(HBP2)

gen HBP = .
replace HBP=0 if HBP1==0 | HBP2==0
replace HBP=1 if HBP1==1 | HBP2==1

*** diabetes

recode hedawdi (-1=0) (7=1), gen(Diabetes1)
recode hediadi (-9/-8=0), gen(Diabetes2)

gen Diabetes = .
replace Diabetes=0 if Diabetes1==0 | Diabetes2==0
replace Diabetes=1 if Diabetes1==1 | Diabetes2==1

*** arthritis (3) (4)


recode hedbwar (-1=0) (3=1), gen(Arthritis1)
recode hedibar (-9/-8 =.), gen(Arthritis2)

gen Arthritis = .
replace Arthritis=0 if Arthritis1==0 | Arthritis2==0
replace Arthritis=1 if Arthritis1==1 | Arthritis2==1


recode hedbwos (-1=0)(4=1), gen(Osteo1)
recode hedibos (-9/-8 =.), gen(Osteo2)

gen Osteo = .
replace Osteo=0 if Osteo1==0 | Osteo2==0
replace Osteo=1 if Osteo1==1 | Osteo2==1

replace Arthritis = 1 if Arthritis ==0 & Osteo==1

*** heart attack

recode hedawmi (-1=0)(3=1), gen(HeartAttack1)
recode hediami (-9/-8 =.), gen(HeartAttack2)

gen HeartAttack = .
replace HeartAttack=0 if HeartAttack1==0 | HeartAttack2==0
replace HeartAttack=1 if HeartAttack1==1 | HeartAttack2==1

*** stroke

recode hedawst (-1=0)(8=1), gen(Stroke1)
recode hediast (-9/-8 =.), gen(Stroke2)

gen Stroke = .
replace Stroke=0 if Stroke1==0 | Stroke2==0
replace Stroke=1 if Stroke1==1 | Stroke2==1

*** cancer

recode hedbwca (-1=0)(5=1), gen(Cancer1)
recode hedibca (-9/-8 =.), gen(Cancer2)

gen Cancer = .
replace Cancer=0 if Cancer1==0 | Cancer2==0
replace Cancer=1 if Cancer1==1 | Cancer2==1

*** lung disease

recode hedbwlu (-1=0), gen(Lung1)
recode hediblu (-9/-8 =.), gen(Lung2)

gen Lung = .
replace Lung=0 if Lung1==0 | Lung2==0
replace Lung=1 if Lung1==1 | Lung2==1


*** Parkinson's

recode hedbwpd (-1=0)(6=1), gen(Parkinson1)
recode hedibpd (-9/-8 =.), gen(Parkinson2)

gen Parkinson = .
replace Parkinson=0 if Parkinson1==0 | Parkinson2==0
replace Parkinson=1 if Parkinson1==1 | Parkinson2==1

*** Alzheimers and dementia

recode hedbwad (-1=0)(8=1), gen(Dementia1)
recode hedibad (-9/-8 =.), gen(Dementia2)

recode hedbwde (-1=0)(9=1), gen(Dementia3)
recode hedibde (-9/-8 =.), gen(Dementia4)

gen Dementia = .
replace Dementia=0 if Dementia1==0 | Dementia2==0 | Dementia3==0 | Dementia4==0
replace Dementia=1 if Dementia1==1 | Dementia2==1 |Dementia3==1 | Dementia4==1

*** affective or emotional disorders

recode hedbwps (-1=0)(7=1), gen(Psychological1)
recode hedibps (-9/-8 =.), gen(Psychological2)

gen Psychological = .
replace Psychological=0 if Psychological1==0 | Psychological2==0
replace Psychological=1 if Psychological1==1 | Psychological2==1


*** hip fracture

recode HeFrac (-1/-9 2 =0), gen(Hip1)
recode HeJiaa1 (-1=0), gen(Hip2)
recode HeJiaa2 (-1=0), gen(Hip3)

gen Hip = .
replace Hip=0 if Hip1==0 | Hip2==0 | Hip3==0
replace Hip=1 if Hip1==1 | Hip2==1 |Hip3==1 


*** gen multimorbidity

gen Morb = HBP +Diabetes +Arthritis +HeartAttack +Stroke +Cancer +Lung +Parkinson +Dementia +Psychological +Hip

recode Morb (0/1=0)(2/max=1), gen(Multimorb)

**** some variables from the paper

gen AG = .
replace AG =1 if Age>=50 & Age<60
replace AG =2 if Age>=60 & Age<70
replace AG =3 if Age>=70 & Age<80
replace AG =4 if Age>=80


***

alpha H1 H2 H3, i

gen HouseholdYear = idahhw7
gen Region = gor
gen BirthYear = Indobyr


keep idauniq Female EduInYears H1 H2 H3 Health smoked drinked Age Married Year ID Weight HBP1 HBP2 HBP ///
    Diabetes1 Diabetes2 Diabetes Arthritis1 Arthritis2 Arthritis Osteo1 Osteo2 Osteo HeartAttack1 ///
    HeartAttack2 HeartAttack Stroke1 Stroke2 Stroke Cancer1 Cancer2 Cancer Lung1 Lung2 Lung Parkinson1 ///
    Parkinson2 Parkinson Dementia1 Dementia2 Dementia3 Dementia4 Dementia Psychological1 Psychological2 ///
    Psychological Hip1 Hip2 Hip3 Hip Morb Multimorb AG HouseholdYear Region BirthYear

save "ELSA7.dta", replace    


* Coding wave 6

use "wave_6_elsa_data_v2.dta", clear    
*** Female

recode indsex (1=0)(2=1), gen(Female)

*** Education

recode fffqend (-1=.)(1/2=0)(3=9)(4=10)(5=11)(6=12)(7=13)(8=14), gen(EduInYears)


*** Sports

recode HeActa HeActb HeActc (-9/-8=.)

gen H1 = 5-HeActa
gen H2 = 5-HeActb
gen H3 = 5-HeActc

gen Health = (H1+H2+H3)/3 


*** smoking

recode HeSmk (-9/-1=.)(2=0), gen(smoked)


*** drinking

recode scal7a (-9/-1=.)(2=0), gen(drinked)

*** age

recode indager (-7=90), gen(Age)

*** married

recode couple (1/2=1)(3=0), gen(Married)

*** year

gen Year=iintdaty

*** gen ID

gen ID =idauniq

*** weight (wave 1)

gen Weight = w6lwgt

*** multimorbidity


*** high blood pressure

recode hedawbp (-1=0), gen(HBP1)
recode hediabp (-9/-8 =.), gen(HBP2)

gen HBP = .
replace HBP=0 if HBP1==0 | HBP2==0
replace HBP=1 if HBP1==1 | HBP2==1

*** diabetes

recode hedawdi (-1=0) (7=1), gen(Diabetes1)
recode hediadi (-9/-8=0), gen(Diabetes2)

gen Diabetes = .
replace Diabetes=0 if Diabetes1==0 | Diabetes2==0
replace Diabetes=1 if Diabetes1==1 | Diabetes2==1

*** arthritis (3) (4)


recode hedbwar (-1=0) (3=1), gen(Arthritis1)
recode hedibar (-9/-8 =.), gen(Arthritis2)

gen Arthritis = .
replace Arthritis=0 if Arthritis1==0 | Arthritis2==0
replace Arthritis=1 if Arthritis1==1 | Arthritis2==1


recode hedbwos (-1=0)(4=1), gen(Osteo1)
recode hedibos (-9/-8 =.), gen(Osteo2)

gen Osteo = .
replace Osteo=0 if Osteo1==0 | Osteo2==0
replace Osteo=1 if Osteo1==1 | Osteo2==1

replace Arthritis = 1 if Arthritis ==0 & Osteo==1

*** heart attack

recode hedawmi (-1=0)(3=1), gen(HeartAttack1)
recode hediami (-9/-8 =.), gen(HeartAttack2)

gen HeartAttack = .
replace HeartAttack=0 if HeartAttack1==0 | HeartAttack2==0
replace HeartAttack=1 if HeartAttack1==1 | HeartAttack2==1

*** stroke

recode hedawst (-1=0)(8=1), gen(Stroke1)
recode hediast (-9/-8 =.), gen(Stroke2)

gen Stroke = .
replace Stroke=0 if Stroke1==0 | Stroke2==0
replace Stroke=1 if Stroke1==1 | Stroke2==1

*** cancer

recode hedbwca (-1=0)(5=1), gen(Cancer1)
recode hedibca (-9/-8 =.), gen(Cancer2)

gen Cancer = .
replace Cancer=0 if Cancer1==0 | Cancer2==0
replace Cancer=1 if Cancer1==1 | Cancer2==1

*** lung disease

recode hedbwlu (-1=0), gen(Lung1)
recode hediblu (-9/-8 =.), gen(Lung2)

gen Lung = .
replace Lung=0 if Lung1==0 | Lung2==0
replace Lung=1 if Lung1==1 | Lung2==1


*** Parkinson's

recode hedbwpd (-1=0)(6=1), gen(Parkinson1)
recode hedibpd (-9/-8 =.), gen(Parkinson2)

gen Parkinson = .
replace Parkinson=0 if Parkinson1==0 | Parkinson2==0
replace Parkinson=1 if Parkinson1==1 | Parkinson2==1

*** Alzheimers and dementia

recode hedbwad (-1=0)(8=1), gen(Dementia1)
recode hedibad (-9/-8 =.), gen(Dementia2)

recode hedbwde (-1=0)(9=1), gen(Dementia3)
recode hedibde (-9/-8 =.), gen(Dementia4)

gen Dementia = .
replace Dementia=0 if Dementia1==0 | Dementia2==0 | Dementia3==0 | Dementia4==0
replace Dementia=1 if Dementia1==1 | Dementia2==1 |Dementia3==1 | Dementia4==1

*** affective or emotional disorders

recode hedbwps (-1=0)(7=1), gen(Psychological1)
recode hedibps (-9/-8 =.), gen(Psychological2)

gen Psychological = .
replace Psychological=0 if Psychological1==0 | Psychological2==0
replace Psychological=1 if Psychological1==1 | Psychological2==1


*** hip fracture

recode HeFrac (-1/-9 2 =0), gen(Hip1)
recode HeJiaa1 (-1=0), gen(Hip2)
recode HeJiaa2 (-1=0), gen(Hip3)

gen Hip = .
replace Hip=0 if Hip1==0 | Hip2==0 | Hip3==0
replace Hip=1 if Hip1==1 | Hip2==1 |Hip3==1 


*** gen multimorbidity

gen Morb = HBP +Diabetes +Arthritis +HeartAttack +Stroke +Cancer +Lung +Parkinson +Dementia +Psychological +Hip

recode Morb (0/1=0)(2/max=1), gen(Multimorb)

**** some variables from the paper

gen AG = .
replace AG =1 if Age>=50 & Age<60
replace AG =2 if Age>=60 & Age<70
replace AG =3 if Age>=70 & Age<80
replace AG =4 if Age>=80


***
alpha H1 H2 H3, i

gen HouseholdYear = idahhw6
gen Region = GOR
gen BirthYear = Indobyr


keep idauniq Female EduInYears H1 H2 H3 Health smoked drinked Age Married Year ID Weight HBP1 HBP2 HBP ///
    Diabetes1 Diabetes2 Diabetes Arthritis1 Arthritis2 Arthritis Osteo1 Osteo2 Osteo HeartAttack1 ///
    HeartAttack2 HeartAttack Stroke1 Stroke2 Stroke Cancer1 Cancer2 Cancer Lung1 Lung2 Lung Parkinson1 ///
    Parkinson2 Parkinson Dementia1 Dementia2 Dementia3 Dementia4 Dementia Psychological1 Psychological2 ///
    Psychological Hip1 Hip2 Hip3 Hip Morb Multimorb AG HouseholdYear Region BirthYear

save "ELSA6.dta", replace    

* Coding wave 5

use "wave_5_elsa_data_v4.dta", clear    
*** Female

recode indsex (1=0)(2=1), gen(Female)

*** Education

recode fffqend (-1=.)(1/2=0)(3=9)(4=10)(5=11)(6=12)(7=13)(8=14), gen(EduInYears)

*** Sports

recode heacta heactb heactc (-9/-8=.)

gen H1 = 5-heacta
gen H2 = 5-heactb
gen H3 = 5-heactc

gen Health = (H1+H2+H3)/3 


*** smoking

recode hesmk (-9/-1=.)(2=0), gen(smoked)


*** drinking

recode scal7a (-9/-1=.)(2=0), gen(drinked)

*** age

recode indager (-7=90), gen(Age)

*** married

recode couple (1/2=1)(3=0), gen(Married)

*** year

gen Year=iintdaty

*** gen ID

gen ID =idauniq

*** weight (wave 1)

gen Weight = w5lwgt

*** multimorbidity


*** high blood pressure

recode hedawbp (-1=0), gen(HBP1)
recode hediabp (-9/-8 =.), gen(HBP2)

gen HBP = .
replace HBP=0 if HBP1==0 | HBP2==0
replace HBP=1 if HBP1==1 | HBP2==1

*** diabetes

recode hedawdi (-1=0) (7=1), gen(Diabetes1)
recode hediadi (-9/-8=0), gen(Diabetes2)

gen Diabetes = .
replace Diabetes=0 if Diabetes1==0 | Diabetes2==0
replace Diabetes=1 if Diabetes1==1 | Diabetes2==1

*** arthritis (3) (4)


recode hedbwar (-1=0) (3=1), gen(Arthritis1)
recode hedibar (-9/-8 =.), gen(Arthritis2)

gen Arthritis = .
replace Arthritis=0 if Arthritis1==0 | Arthritis2==0
replace Arthritis=1 if Arthritis1==1 | Arthritis2==1


recode hedbwos (-1=0)(4=1), gen(Osteo1)
recode hedibos (-9/-8 =.), gen(Osteo2)

gen Osteo = .
replace Osteo=0 if Osteo1==0 | Osteo2==0
replace Osteo=1 if Osteo1==1 | Osteo2==1

replace Arthritis = 1 if Arthritis ==0 & Osteo==1

*** heart attack

recode hedawmi (-1=0)(3=1), gen(HeartAttack1)
recode hediami (-9/-8 =.), gen(HeartAttack2)

gen HeartAttack = .
replace HeartAttack=0 if HeartAttack1==0 | HeartAttack2==0
replace HeartAttack=1 if HeartAttack1==1 | HeartAttack2==1

*** stroke

recode hedawst (-1=0)(8=1), gen(Stroke1)
recode hediast (-9/-8 =.), gen(Stroke2)

gen Stroke = .
replace Stroke=0 if Stroke1==0 | Stroke2==0
replace Stroke=1 if Stroke1==1 | Stroke2==1

*** cancer

recode hedbwca (-1=0)(5=1), gen(Cancer1)
recode hedibca (-9/-8 =.), gen(Cancer2)

gen Cancer = .
replace Cancer=0 if Cancer1==0 | Cancer2==0
replace Cancer=1 if Cancer1==1 | Cancer2==1

*** lung disease

recode hedbwlu (-1=0), gen(Lung1)
recode hediblu (-9/-8 =.), gen(Lung2)

gen Lung = .
replace Lung=0 if Lung1==0 | Lung2==0
replace Lung=1 if Lung1==1 | Lung2==1


*** Parkinson's

recode hedbwpd (-1=0)(6=1), gen(Parkinson1)
recode hedibpd (-9/-8 =.), gen(Parkinson2)

gen Parkinson = .
replace Parkinson=0 if Parkinson1==0 | Parkinson2==0
replace Parkinson=1 if Parkinson1==1 | Parkinson2==1

*** Alzheimers and dementia

recode hedbwad (-1=0)(8=1), gen(Dementia1)
recode hedibad (-9/-8 =.), gen(Dementia2)

recode hedbwde (-1=0)(9=1), gen(Dementia3)
recode hedibde (-9/-8 =.), gen(Dementia4)

gen Dementia = .
replace Dementia=0 if Dementia1==0 | Dementia2==0 | Dementia3==0 | Dementia4==0
replace Dementia=1 if Dementia1==1 | Dementia2==1 |Dementia3==1 | Dementia4==1

*** affective or emotional disorders

recode hedbwps (-1=0)(7=1), gen(Psychological1)
recode hedibps (-9/-8 =.), gen(Psychological2)

gen Psychological = .
replace Psychological=0 if Psychological1==0 | Psychological2==0
replace Psychological=1 if Psychological1==1 | Psychological2==1


*** hip fracture

recode hefrac (-1/-9 2 =0), gen(Hip1)
recode hejiaa1 (-1=0), gen(Hip2)
recode hejiaa2 (-1=0), gen(Hip3)

gen Hip = .
replace Hip=0 if Hip1==0 | Hip2==0 | Hip3==0
replace Hip=1 if Hip1==1 | Hip2==1 |Hip3==1 


*** gen multimorbidity

gen Morb = HBP +Diabetes +Arthritis +HeartAttack +Stroke +Cancer +Lung +Parkinson +Dementia +Psychological +Hip

recode Morb (0/1=0)(2/max=1), gen(Multimorb)

**** some variables from the paper

gen AG = .
replace AG =1 if Age>=50 & Age<60
replace AG =2 if Age>=60 & Age<70
replace AG =3 if Age>=70 & Age<80
replace AG =4 if Age>=80


***
alpha H1 H2 H3, i

gen HouseholdYear = idahhw5
gen Region = GOR
gen BirthYear = indobyr

keep idauniq Female EduInYears H1 H2 H3 Health smoked drinked Age Married Year ID Weight HBP1 HBP2 HBP ///
    Diabetes1 Diabetes2 Diabetes Arthritis1 Arthritis2 Arthritis Osteo1 Osteo2 Osteo HeartAttack1 ///
    HeartAttack2 HeartAttack Stroke1 Stroke2 Stroke Cancer1 Cancer2 Cancer Lung1 Lung2 Lung Parkinson1 ///
    Parkinson2 Parkinson Dementia1 Dementia2 Dementia3 Dementia4 Dementia Psychological1 Psychological2 ///
    Psychological Hip1 Hip2 Hip3 Hip Morb Multimorb AG HouseholdYear Region BirthYear

save "ELSA5.dta", replace    


*** Coding wave 4

use "wave_4_elsa_data_v3.dta", clear    
*** Female

recode indsex (1=0)(2=1), gen(Female)

*** Education

recode w4edqual (-8/-1=.)(7=0)(5=9)(4=11)(3=12)(2=13)(1=14)(6=12), gen(EduInYears)

*** Sports

recode heacta heactb heactc (-9/-8=.)

gen H1 = 5-heacta
gen H2 = 5-heactb
gen H3 = 5-heactc

gen Health = (H1+H2+H3)/3 


*** smoking

recode hesmk (-9/-1=.)(2=0), gen(smoked)


*** drinking

recode scal7a (-9/-1=.)(2=0), gen(drinked)

*** age

recode indager (-7=90), gen(Age)

*** married

recode couple (1/2=1)(3=0), gen(Married)

*** year

gen Year=iintdaty

*** gen ID

gen ID =idauniq

*** weight (wave 1)

gen Weight = w4lwgt

*** multimorbidity


*** high blood pressure

recode hedawbp (-1=0), gen(HBP1)
recode hediabp (-9/-8 =.), gen(HBP2)

gen HBP = .
replace HBP=0 if HBP1==0 | HBP2==0
replace HBP=1 if HBP1==1 | HBP2==1

*** diabetes

recode hedawdi (-1=0) (7=1), gen(Diabetes1)
recode hediadi (-9/-8=0), gen(Diabetes2)

gen Diabetes = .
replace Diabetes=0 if Diabetes1==0 | Diabetes2==0
replace Diabetes=1 if Diabetes1==1 | Diabetes2==1

*** arthritis (3) (4)


recode hedbwar (-1=0) (3=1), gen(Arthritis1)
recode hedibar (-9/-8 =.), gen(Arthritis2)

gen Arthritis = .
replace Arthritis=0 if Arthritis1==0 | Arthritis2==0
replace Arthritis=1 if Arthritis1==1 | Arthritis2==1


recode hedbwos (-1=0)(4=1), gen(Osteo1)
recode hedibos (-9/-8 =.), gen(Osteo2)

gen Osteo = .
replace Osteo=0 if Osteo1==0 | Osteo2==0
replace Osteo=1 if Osteo1==1 | Osteo2==1

replace Arthritis = 1 if Arthritis ==0 & Osteo==1

*** heart attack

recode hedawmi (-1=0)(3=1), gen(HeartAttack1)
recode hediami (-9/-8 =.), gen(HeartAttack2)

gen HeartAttack = .
replace HeartAttack=0 if HeartAttack1==0 | HeartAttack2==0
replace HeartAttack=1 if HeartAttack1==1 | HeartAttack2==1

*** stroke

recode hedawst (-1=0)(8=1), gen(Stroke1)
recode hediast (-9/-8 =.), gen(Stroke2)

gen Stroke = .
replace Stroke=0 if Stroke1==0 | Stroke2==0
replace Stroke=1 if Stroke1==1 | Stroke2==1

*** cancer

recode hedbwca (-1=0)(5=1), gen(Cancer1)
recode hedibca (-9/-8 =.), gen(Cancer2)

gen Cancer = .
replace Cancer=0 if Cancer1==0 | Cancer2==0
replace Cancer=1 if Cancer1==1 | Cancer2==1

*** lung disease

recode hedbwlu (-1=0), gen(Lung1)
recode hediblu (-9/-8 =.), gen(Lung2)

gen Lung = .
replace Lung=0 if Lung1==0 | Lung2==0
replace Lung=1 if Lung1==1 | Lung2==1


*** Parkinson's

recode hedbwpd (-1=0)(6=1), gen(Parkinson1)
recode hedibpd (-9/-8 =.), gen(Parkinson2)

gen Parkinson = .
replace Parkinson=0 if Parkinson1==0 | Parkinson2==0
replace Parkinson=1 if Parkinson1==1 | Parkinson2==1

*** Alzheimers and dementia

recode hedbwad (-1=0)(8=1), gen(Dementia1)
recode hedibad (-9/-8 =.), gen(Dementia2)

recode hedbwde (-1=0)(9=1), gen(Dementia3)
recode hedibde (-9/-8 =.), gen(Dementia4)

gen Dementia = .
replace Dementia=0 if Dementia1==0 | Dementia2==0 | Dementia3==0 | Dementia4==0
replace Dementia=1 if Dementia1==1 | Dementia2==1 |Dementia3==1 | Dementia4==1

*** affective or emotional disorders

recode hedbwps (-1=0)(7=1), gen(Psychological1)
recode hedibps (-9/-8 =.), gen(Psychological2)

gen Psychological = .
replace Psychological=0 if Psychological1==0 | Psychological2==0
replace Psychological=1 if Psychological1==1 | Psychological2==1


*** hip fracture

recode hefrac (-1/-9 2 =0), gen(Hip1)
recode hejiaa1 (-1=0), gen(Hip2)
recode hejiaa2 (-1=0), gen(Hip3)

gen Hip = .
replace Hip=0 if Hip1==0 | Hip2==0 | Hip3==0
replace Hip=1 if Hip1==1 | Hip2==1 |Hip3==1 


*** gen multimorbidity

gen Morb = HBP +Diabetes +Arthritis +HeartAttack +Stroke +Cancer +Lung +Parkinson +Dementia +Psychological +Hip

recode Morb (0/1=0)(2/max=1), gen(Multimorb)

**** some variables from the paper

gen AG = .
replace AG =1 if Age>=50 & Age<60
replace AG =2 if Age>=60 & Age<70
replace AG =3 if Age>=70 & Age<80
replace AG =4 if Age>=80


***
alpha H1 H2 H3, i

gen HouseholdYear = idahhw4
gen Region = GOR
gen BirthYear = indobyr

keep idauniq Female EduInYears H1 H2 H3 Health smoked drinked Age Married Year ID Weight HBP1 HBP2 HBP ///
    Diabetes1 Diabetes2 Diabetes Arthritis1 Arthritis2 Arthritis Osteo1 Osteo2 Osteo HeartAttack1 ///
    HeartAttack2 HeartAttack Stroke1 Stroke2 Stroke Cancer1 Cancer2 Cancer Lung1 Lung2 Lung Parkinson1 ///
    Parkinson2 Parkinson Dementia1 Dementia2 Dementia3 Dementia4 Dementia Psychological1 Psychological2 ///
    Psychological Hip1 Hip2 Hip3 Hip Morb Multimorb AG HouseholdYear Region BirthYear

save "ELSA4.dta", replace    


*** Coding wave 3


use "wave_3_elsa_data_v4.dta", clear    
*** Female

recode indsex (1=0)(2=1), gen(Female)

*** Education

recode w3edqual (-8/-1=.)(7=0)(5=9)(4=11)(3=12)(2=13)(1=14)(6=12), gen(EduInYears)

*** Sports

recode heacta heactb heactc (-9/-8=.)

gen H1 = 5-heacta
gen H2 = 5-heactb
gen H3 = 5-heactc

gen Health = (H1+H2+H3)/3 


*** smoking

recode hesmk (-9/-1=.)(2=0), gen(smoked)


*** drinking

recode scal7a (-9/-1=.)(2=0), gen(drinked)

*** age

recode indager (-7=90), gen(Age)

*** married

recode couple (1/2=1)(3=0), gen(Married)

*** year

gen Year=iintdaty

*** gen ID

gen ID =idauniq

*** weight (wave 1)

gen Weight = w3lwgt

*** multimorbidity


*** high blood pressure

recode hedawbp (-1=0), gen(HBP1)
recode hediabp (-9/-8 =.), gen(HBP2)

gen HBP = .
replace HBP=0 if HBP1==0 | HBP2==0
replace HBP=1 if HBP1==1 | HBP2==1

*** diabetes

recode hedawdi (-1=0) (7=1), gen(Diabetes1)
recode hediadi (-9/-8=0), gen(Diabetes2)

gen Diabetes = .
replace Diabetes=0 if Diabetes1==0 | Diabetes2==0
replace Diabetes=1 if Diabetes1==1 | Diabetes2==1

*** arthritis (3) (4)


recode hedbwar (-1=0) (3=1), gen(Arthritis1)
recode hedibar (-9/-8 =.), gen(Arthritis2)

gen Arthritis = .
replace Arthritis=0 if Arthritis1==0 | Arthritis2==0
replace Arthritis=1 if Arthritis1==1 | Arthritis2==1


recode hedbwos (-1=0)(4=1), gen(Osteo1)
recode hedibos (-9/-8 =.), gen(Osteo2)

gen Osteo = .
replace Osteo=0 if Osteo1==0 | Osteo2==0
replace Osteo=1 if Osteo1==1 | Osteo2==1

replace Arthritis = 1 if Arthritis ==0 & Osteo==1

*** heart attack

recode hedawmi (-1=0)(3=1), gen(HeartAttack1)
recode hediami (-9/-8 =.), gen(HeartAttack2)

gen HeartAttack = .
replace HeartAttack=0 if HeartAttack1==0 | HeartAttack2==0
replace HeartAttack=1 if HeartAttack1==1 | HeartAttack2==1

*** stroke

recode hedawst (-1=0)(8=1), gen(Stroke1)
recode hediast (-9/-8 =.), gen(Stroke2)

gen Stroke = .
replace Stroke=0 if Stroke1==0 | Stroke2==0
replace Stroke=1 if Stroke1==1 | Stroke2==1

*** cancer

recode hedbwca (-1=0)(5=1), gen(Cancer1)
recode hedibca (-9/-8 =.), gen(Cancer2)

gen Cancer = .
replace Cancer=0 if Cancer1==0 | Cancer2==0
replace Cancer=1 if Cancer1==1 | Cancer2==1

*** lung disease

recode hedbwlu (-1=0), gen(Lung1)
recode hediblu (-9/-8 =.), gen(Lung2)

gen Lung = .
replace Lung=0 if Lung1==0 | Lung2==0
replace Lung=1 if Lung1==1 | Lung2==1


*** Parkinson's

recode hedbwpd (-1=0)(6=1), gen(Parkinson1)
recode hedibpd (-9/-8 =.), gen(Parkinson2)

gen Parkinson = .
replace Parkinson=0 if Parkinson1==0 | Parkinson2==0
replace Parkinson=1 if Parkinson1==1 | Parkinson2==1

*** Alzheimers and dementia

recode hedbwad (-1=0)(8=1), gen(Dementia1)
recode hedibad (-9/-8 =.), gen(Dementia2)

recode hedbwde (-1=0)(9=1), gen(Dementia3)
recode hedibde (-9/-8 =.), gen(Dementia4)

gen Dementia = .
replace Dementia=0 if Dementia1==0 | Dementia2==0 | Dementia3==0 | Dementia4==0
replace Dementia=1 if Dementia1==1 | Dementia2==1 |Dementia3==1 | Dementia4==1

*** affective or emotional disorders

recode hedbwps (-1=0)(7=1), gen(Psychological1)
recode hedibps (-9/-8 =.), gen(Psychological2)

gen Psychological = .
replace Psychological=0 if Psychological1==0 | Psychological2==0
replace Psychological=1 if Psychological1==1 | Psychological2==1


*** hip fracture

recode hefrac (-1/-9 2 =0), gen(Hip1)
recode hejiaa1 (-1=0), gen(Hip2)
recode hejiaa2 (-1=0), gen(Hip3)

gen Hip = .
replace Hip=0 if Hip1==0 | Hip2==0 | Hip3==0
replace Hip=1 if Hip1==1 | Hip2==1 |Hip3==1 


*** gen multimorbidity

gen Morb = HBP +Diabetes +Arthritis +HeartAttack +Stroke +Cancer +Lung +Parkinson +Dementia +Psychological +Hip

recode Morb (0/1=0)(2/max=1), gen(Multimorb)

**** some variables from the paper

gen AG = .
replace AG =1 if Age>=50 & Age<60
replace AG =2 if Age>=60 & Age<70
replace AG =3 if Age>=70 & Age<80
replace AG =4 if Age>=80


***

alpha H1 H2 H3, i

gen HouseholdYear = idahhw3
gen Region = GOR
gen BirthYear = indobyr

keep idauniq Female EduInYears H1 H2 H3 Health smoked drinked Age Married Year ID Weight HBP1 HBP2 HBP ///
    Diabetes1 Diabetes2 Diabetes Arthritis1 Arthritis2 Arthritis Osteo1 Osteo2 Osteo HeartAttack1 ///
    HeartAttack2 HeartAttack Stroke1 Stroke2 Stroke Cancer1 Cancer2 Cancer Lung1 Lung2 Lung Parkinson1 ///
    Parkinson2 Parkinson Dementia1 Dementia2 Dementia3 Dementia4 Dementia Psychological1 Psychological2 ///
    Psychological Hip1 Hip2 Hip3 Hip Morb Multimorb AG HouseholdYear Region BirthYear

save "ELSA3.dta", replace    

use "ELSA3.dta", clear
append using "ELSA4.dta"
append using "ELSA5.dta"
append using "ELSA6.dta"
append using "ELSA7.dta"
append using "ELSA8.dta"

save "ELSA3to8.dta", replace


Coding merged dataset for the paper

Here, I code and run the analysis for the paper. 


use "ELSA3to8.dta", clear

recode BirthYear (-1 -8=.)(-7=.)
    
recode BirthYear (min/1924=1920) ///
    (1925/1929=1925) ///
    (1930/1934=1930)(1935/1939=1935) ///
    (1940/1944=1940)(1945/1949=1945) ///
    (1950/1954=1950)(1955/1959=1955)(1960/max=1960), gen(Cohort)
    
gen Age1 = Year-BirthYear
    
recode Age1 (min/49=45)(50/54=50)(55/59=55) ///
    (60/64=60)(65/69=65)(70/74=70)(75/79=75) ///
    (80/84=80)(85/89=85)(90/max=90), gen(AgeGroup)
    

gen NewPeriod = Cohort + AgeGroup

*** we have to drop AgeGroup 49 because we are observing only those above 50
drop if AgeGroup < 50

************************************************************
***** fixing the year for those in 2000

replace AgeGroup = AgeGroup+5 if NewPeriod==2000
replace NewPeriod = Cohort + AgeGroup

replace AgeGroup = AgeGroup+5 if NewPeriod==2005 & Year>=2010 & Year <2015
replace NewPeriod = Cohort + AgeGroup

replace AgeGroup = AgeGroup+5 if NewPeriod<2015 & Year>=2015
replace NewPeriod = Cohort + AgeGroup

*************************************************************
*** fixing over 90s

replace Cohort=1920 if AgeGroup==95
replace Cohort=1920 if AgeGroup==100
recode AgeGroup (95/max=90)

*****************************************************************
*** not all cohorts are present in all years so dropping some:

drop if Cohort <1925
drop if Cohort>1955

*** same with Age

drop if AgeGroup <60
drop if AgeGroup>80

*****************************************************************

gen no_miss=!missing(Dementia)

la de AgeGroup ///
    50 "50-54" ///
    55 "55-59" ///
    60 "60-64" ///
    65 "65-69" ///
    70 "70-74" ///
    75 "75-79" ///
    80 "80-84" ///
    85 "85-89" ///
    90 "90 and above"
la val AgeGroup AgeGroup

la de Cohort ///
    1920 "Before and on 1924" 1925 "Before 1930" ///
    1930 "1930-1935" ///
    1935 "1935-1939" 1940 "1940-1944" ///
    1945 "1945-1949" 1950 "1950-1954" ///
    1955 "1955-1959" 1960 "1960 and later"
la val Cohort Cohort

cd "R&R.R1"

apc_ie Dementia ///
    , age(AgeGroup) period(NewPeriod) cohort(Cohort) family(binomial) generate(var1)
eststo m1


esttab m1 using reg1.rtf, ///
    b(3) ci(3) scalars("deviance Deviance" "ll Log-Lik.") title ///
    ("Table 3") ///
    mtitle("Model (1)" "Model (2)") ///
    nonumber coeflabels( ///
    EduInYears "Education" ///
    Female "Women" ///
    Health "Physical Activity" ///
    smoked "Ever Smoked" ///
    drinked "Drinks" ///
    Married "Married" ///
    cohort_1922 "1922" cohort_1927 "1927" cohort_1932 "1932" ///
    cohort_1937 "1937" cohort_1942 "1942" ///
    cohort_1947 "1947" cohort_1952 "1952" ///
    cohort_1957 "1957" cohort_1962 "1962" ///
    cohort_1967 "1967" cohort_1972 "1972"  ///
    age_47 "43-47" age_52 "48-52" age_57 "52-57" ///
    age_62 "58-62" age_67 "62-67" age_72 "68-72" age_77 "72-77" ///
    age_82 "78-82" age_87 "82-87" period_2009 "2009" period_2014 ///
    "2014" period_2019 "2017" _cons "Constant") ///
    collabels(,none) nonumber varlabels(`e(labels)') ///
    modelwidth(10) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
    varwidth(35) unstack wide label nogap replace
    
    
** 

tab Female NewPeriod, col
tab AgeGroup NewPeriod, col
tab Cohort NewPeriod, col

tab AgeGroup NewPeriod, summarize(Dementia) means
tab Cohort NewPeriod, summarize(Dementia) means


*****************************************************************************
**** plot APC effects
*****************************************************************************

est res m1

coefplot, keep(age_*) vert recast(connected) ///
    ciopts(recast(rarea)) ///
    coefl( ///
    age_60 ="60-64" ///
    age_65 ="65-69" ///
    age_70 ="70-74" ///
    age_75 ="75-79" ///
    age_80 ="80-84" ///
    , wrap(6)) title("Age Effects") ///
    ytitle("Log Coefficients") xtitle("Age Group") ///
    yline(0.0, lw(thin) lc(black)) scheme(lean2)
    
    
coefplot, keep(period_*) vert recast(connected) ciopts(recast(rarea)) ///
    coefl(period_2005 = "2006-2009" period_2010 = "2010-2014" period_2015 = "2015-2017" ///
    ) title("Period Effects") ///
    ytitle("Log Coefficients") xtitle("Period") ///
    yline(0.0, lw(thin) lc(black)) scheme(lean2)
    
coefplot, keep(cohort_*) vert recast(connected) ciopts(recast(rarea)) ///
    coefl(cohort_1925 ="1925-1929" ///
    cohort_1930 ="1930-1934" ///
    cohort_1935 ="1935-1939" cohort_1940 ="1940-1944" ///
    cohort_1945 ="1945-1949" cohort_1950 ="1950-1954" ///
    cohort_1955 ="1955-1959", wrap(5)) ///
     title("Cohort Effects") ///
    ytitle("Log Coefficients") xtitle("Cohort") ///
    yline(0.0, lw(thin) lc(black)) scheme(lean2)