Derivation
variable: p3date (table[s]: all_vaccination_records [derived table])
variable: birth_date (table[s]: all_vaccination_records [derived table])
la def vacckeep 1 "vacc=y, date ok, card seen" 2 "vacc=y, date NOT ok (S), card seen" 3 "vacc=y, date NOT ok (B), card seen" 4 "vacc=y, date NOT ok (S&B), card seen" 5 "vacc=y, date ok, card NOT seen" 6 "vacc=y, date NOT ok (S), card NOT seen" 7 "vacc=y, date NOT ok (B), card NOT seen" 8 "vacc=y, date NOT ok (S&B), card NOT seen" 9 "last report of vacc=n"
*1. keep the first record where the vaccine=yes, birth-date problem=no, schedule problem=no and healthcard=seen
gen `vacc'keep=1 if order==1 & `vacc'yn==1 & `vacc'Bprob==0 & `vacc'Sprob==0 & card==1
bys ident: egen kept=max(`vacc'keep)
*2. if no record identified: keep the first record where the vaccine=yes, birth-date problem=no, schedule problem=yes and healthcard=seen
replace `vacc'keep=2 if order==1 & `vacc'yn==1 & `vacc'Bprob==0 & `vacc'Sprob==1 & card==1 & kept==.
drop kept
bys ident: egen kept=max(`vacc'keep)
*3. if no record identified: keep the first record where the vaccine=yes, birth-date problem=yes, schedule problem=no and healthcard=seen
replace `vacc'keep=3 if order==1 & `vacc'yn==1 & `vacc'Bprob==1 & `vacc'Sprob==0 & card==1 & kept==.
drop kept
bys ident: egen kept=max(`vacc'keep)
*4. if no record identified: keep the first record where the vaccine=yes, birth-date problem=yes, schedule problem=yes and healthcard=seen
replace `vacc'keep=4 if order==1 & `vacc'yn==1 & `vacc'Bprob==1 & `vacc'Sprob==1 & card==1 & kept==.
drop kept
bys ident: egen kept=max(`vacc'keep)
*5. if no record identified: keep the first record where the vaccine=yes, birth-date problem=no, schedule problem=no and healthcard=not seen
replace `vacc'keep=5 if order==1 & `vacc'yn==1 & `vacc'Bprob==0 & `vacc'Sprob==0 & card==2 & kept==.
drop kept
bys ident: egen kept=max(`vacc'keep)
*6. if no record identified: keep the first record where the vaccine=yes, birth-date problem=no, schedule problem=yes and healthcard=not seen
replace `vacc'keep=6 if order==1 & `vacc'yn==1 & `vacc'Bprob==0 & `vacc'Sprob==1 & card==2 & kept==.
drop kept
bys ident: egen kept=max(`vacc'keep)
*7. if no record identified: keep the first record where the vaccine=yes, birth-date problem=yes, schedule problem=no and healthcard=not seen
replace `vacc'keep=7 if order==1 & `vacc'yn==1 & `vacc'Bprob==1 & `vacc'Sprob==0 & card==2 & kept==.
drop kept
bys ident: egen kept=max(`vacc'keep)
*8. if no record identified: keep the first record where the vaccine=yes, birth-date problem=yes, schedule problem=yes and healthcard=not seen
replace `vacc'keep=8 if order==1 & `vacc'yn==1 & `vacc'Bprob==1 & `vacc'Sprob==1 & card==2 & kept==.
drop kept
bys ident: egen kept=max(`vacc'keep)
*9. if no record identified: keep the LAST record where the vaccine is recorded as no (regardless whether healthcard seen or not)
replace `vacc'keep=9 if Norder==Ntotal & `vacc'yn==0 & kept==.
la val `vacc'keep vacckeep
drop kept