Derivation
Variable is created during appending of case/control datasets
<this is the code that put the datasets for the main file together, unless otherwise indicated, the code is working off this dataset>
**TBX (CASES)
use ${newstata}\tbold_tbpart2, clear
rename * *_2
rename ident_2 ident
save ${tempfiles}\tbold_tbpart2, replace
use ${newstata}\tbold_tbpart1, clear /*data prior to 2000*/
merge m:1 ident using ${tempfiles}\tbold_tbpart2
gen intdiff=abs(intdate_2-intdate)
bys ident: egen mindiff=min(intdiff)
gen keep=1 if mindiff==intdiff & _m==3
desc *_2, varlist
foreach var in `r(varlist)' {
cap replace `var'=. if keep~=1
cap replace `var'="" if keep~=1
}
gen case=1
rename occhead occheadold
save ${tempfiles}\tbold_tbpart12, replace
use ${newstata}\tb_tbxto2007, clear /*data from 2000 to 2007*/
gen case=1
save ${tempfiles}\tb_tbxto2007, replace
use ${newstata}\tb_tbx, clear /*data from 2007 onwards*/
gen case=1
save ${tempfiles}\tb_tbx, replace
**TBO (CONTROLS)
use ${newstata}\tbold_control, clear /*data prior to 2000*/
gen case=0
rename occhead occheadold
save ${tempfiles}\tbold_control, replace
use ${newstata}\tb_tboto2007, clear /*data from 2000 to 2007*/
gen case=0
save ${tempfiles}\tb_tboto2007, replace
use ${newstata}\tb_tbo, clear /*data from 2007 onwards*/
gen case=0
save ${tempfiles}\tb_tbo, replace
use ${tempfiles}\tbold_tbpart12, clear
append using ${tempfiles}\tb_tbxto2007
rename smeacult smeacult_old
append using ${tempfiles}\tb_tbx
duplicates drop ident intdate, force
preserve
use ${newstata}\tbcpu, clear
duplicates drop ident intdate, force
keep ident tbtype tbcpu mic_res outcome intdate starteps fineps pos_cult
rename (mic_res outcome) (mic_resH outcome_cpu)
save ${tempfiles}\tbcpu_edit, replace
restore
merge 1:1 ident intdate using ${tempfiles}\tbcpu_edit, keep(match) nogen
append using ${tempfiles}\tbold_control
append using ${tempfiles}\tb_tboto2007
append using ${tempfiles}\tb_tbo
bys ident intdate (mohregno): keep if _n==_N
*add in crshse from residency episodes
preserve
use ident startdate stopdate crshse using ${gendata}\crs_residency_episodes_clean_nopoly, clear
save ${tempfiles}\reseps, replace
restore
drop _m
joinby ident using ${tempfiles}\reseps, unmatched(master)
gen crs=1 if intdate>=startdate & intdate<stopdate
replace crshse="9999999" if crs~=1
bys ident intdate (crshse): keep if _n==1
replace crshse="" if crshse=="9999999"
drop startdate stopdate _m
save ${tempfiles}\comb_tbx_tbo, replace
la def case 0 "Control" 1 "Case"
la val case case