diff --git a/pr_cvd_female_raw_bmirep_qrisk2.do b/pr_cvd_female_raw_bmirep_qrisk2.do new file mode 100644 index 0000000..cfe0147 --- /dev/null +++ b/pr_cvd_female_raw_bmirep_qrisk2.do @@ -0,0 +1,172 @@ +*use a dataset including +cap prog drop cvd_female_raw_qrisk2 +program define cvd_female_raw_qrisk2 + +noi di "Variables you need:" +noi di "age -- age as integer" +noi di "b_AF -- atrial fibrilation as binary 01" +noi di "b_ra -- rheumatoid arthritis as binary 01" +noi di "b_renal -- rename disease as binary 01" +noi di "b_treatedhyp -- hypertension treatment as binary 01" +noi di "b_type1 -- type 1 diabetes as binary 01" +noi di "b_type2 -- type 2 diabetes as binary 01" +noi di "bmi -- bmi as continuous numeric, replaced with 20 if below 20" +noi di "ethrisk -- ethnicity as categorical integer (0-9)" +noi di "fh_cvd -- family history as binary 01" +noi di "rati -- HDL LDL cholesterol ratio as continuous numeric" +noi di "sbp -- systolic blood pressure as continuous numeric" +noi di "smoke_cat -- smoking as integer category (0-4)" +noi di "sex -- sex as 1=male 2=female" + + + +cap gen score=. +gen double rec_num = _n +preserve + +keep if sex==2 +*replace bmi=20 if bmi<20 + + +local surv=10 +forvalues i = 0/15 { + gen survivor`i'=0 +} +replace survivor10=0.989747583866119 +*this part is added in incase they want to do risk scores over a different number of years but they haven't actually done the groundwork yet. + +gen Iethrisk0 = 0 +gen Iethrisk1 = 0 /*5*/ +gen Iethrisk2 = 0.2574099349831925900000000 /*3*/ +gen Iethrisk3 = 0.6129795430571779400000000 /*1*/ +gen Iethrisk4 = 0.3362159841669621300000000 /*2*/ +gen Iethrisk5 = 0.1512517303224336400000000 /*4*/ +gen Iethrisk6 = -0.1794156259657768100000000 /*7*/ +gen Iethrisk7 = -0.3503423610057745400000000 /*9*/ +gen Iethrisk8 = -0.2778372483233216800000000 /*8*/ +gen Iethrisk9 = -0.1592734122665366000000000 /*6*/ + +gen Ismoke0=0 +gen Ismoke1=0.2119377108760385200000000 +gen Ismoke2=0.6618634379685941500000000 +gen Ismoke3=0.7570714587132305600000000 +gen Ismoke4=0.9496298251457036000000000 + +gen double dage = age +replace dage=dage/10 +gen double age_2 = dage +gen double age_1 = dage^0.5 +gen double dbmi = bmi +replace dbmi=dbmi/10 +gen double bmi_2 = ln(dbmi)*dbmi^(-2) +gen double bmi_1 = dbmi^(-2) + +replace age_1 = age_1 - 2.086397409439087 +replace age_2 = age_2 - 4.353054523468018 +replace bmi_1 = bmi_1 - 0.152244374155998 +replace bmi_2 = bmi_2 - 0.143282383680344 +replace rati = rati - 3.506655454635620 +replace sbp = sbp - 125.040039062500000 +replace town = town - 0.416743695735931 + +gen double a=0 + + + + + + +/* The conditional sums */ +forvalues i=1/9 { +replace a = a+ Iethrisk`i' if ethrisk==`i' +} +forvalues i=0/4{ +replace a = a+ Ismoke`i' if smoke_cat==`i' +} + + /* Sum from continuous values */ + + + replace a = a+ age_1 * 4.4417863976316578000000000 + replace a = a+ age_2 * 0.0281637210672999180000000 + replace a = a+ bmi_1 * 0.8942365304710663300000000 + replace a = a+ bmi_2 * -6.5748047596104335000000000 + replace a = a+ rati * 0.1433900561621420900000000 + replace a = a+ sbp * 0.0128971795843613720000000 + replace a = a+ town * 0.0664772630011438850000000 + + + + /* Sum from boolean values */ + + replace a = a+ b_AF * 1.6284780236484424000000000 + replace a = a+ b_ra * 0.2901233104088770700000000 + replace a = a+ b_renal * 1.0043796680368302000000000 + replace a = a+ b_treatedhyp * 0.6180430562788129500000000 + replace a = a+ b_type1 * 1.8400348250874599000000000 + replace a = a+ b_type2 * 1.1711626412196512000000000 + replace a = a+ fh_cvd * 0.5147261203665195500000000 + + /* Sum from interaction terms */ + + replace a = a+ age_1 * 1 * 0.7464406144391666500000000 if smoke_cat==1 + replace a = a+ age_1 * 1 * 0.2568541711879666600000000 if smoke_cat==2 + replace a = a+ age_1 * 1 * -1.5452226707866523000000000 if smoke_cat==3 + replace a = a+ age_1 * 1 * -1.7113013709043405000000000 if smoke_cat==4 + replace a = a+ age_1 * b_AF * -7.0177986441269269000000000 + replace a = a+ age_1 * b_renal * -2.9684019256454390000000000 + replace a = a+ age_1 * b_treatedhyp * -4.2219906452967848000000000 + replace a = a+ age_1 * b_type1 * 1.6835769546040080000000000 + replace a = a+ age_1 * b_type2 * -2.9371798540034648000000000 + replace a = a+ age_1 * bmi_1 * 0.1797196207044682300000000 + replace a = a+ age_1 * bmi_2 * 40.2428166760658140000000000 + replace a = a+ age_1 * fh_cvd * 0.1439979240753906700000000 + replace a = a+ age_1 * sbp * -0.0362575233899774460000000 + replace a = a+ age_1 * town * 0.3735138031433442600000000 + replace a = a+ age_2 * 1 * -0.1927057741748231000000000 if smoke_cat==1 + replace a = a+ age_2 * 1 * -0.1526965063458932700000000 if smoke_cat==2 + replace a = a+ age_2 * 1 * 0.2313563976521429400000000 if smoke_cat==3 + replace a = a+ age_2 * 1 * 0.2307165013868296700000000 if smoke_cat==4 + replace a = a+ age_2 * b_AF * 1.1395776028337732000000000 + replace a = a+ age_2 * b_renal * 0.4356963208330940600000000 + replace a = a+ age_2 * b_treatedhyp * 0.7265947108887239600000000 + replace a = a+ age_2 * b_type1 * -0.6320977766275653900000000 + replace a = a+ age_2 * b_type2 * 0.4023270434871086800000000 + replace a = a+ age_2 * bmi_1 * 0.1319276622711877700000000 + replace a = a+ age_2 * bmi_2 * -7.3211322435546409000000000 + replace a = a+ age_2 * fh_cvd * -0.1330260018273720400000000 + replace a = a+ age_2 * sbp * 0.0045842850495397955000000 + replace a = a+ age_2 * town * -0.0952370300845990780000000 + + + gen double scoref = exp(a) + replace scoref=survivor`surv'^scoref + replace scoref = 1-scoref + replace scoref=100*scoref + *gen double scoref = 100.0 * (1 - survivor`surv'^(exp(a))) + +keep scoref patid rec_num + +tempfile temper +save `temper' , replace +restore +merge 1:1 patid rec_num using `temper' , nogen +drop rec_num + +replace score=scoref if scoref!=. +drop scoref + +end + + + + + + + + + + + + + diff --git a/pr_cvd_female_raw_bmirep_qrisk3.do b/pr_cvd_female_raw_bmirep_qrisk3.do new file mode 100644 index 0000000..9ae7daf --- /dev/null +++ b/pr_cvd_female_raw_bmirep_qrisk3.do @@ -0,0 +1,188 @@ +*use a dataset including +cap prog drop cvd_female_raw_qrisk3 +program define cvd_female_raw_qrisk3 + +noi di "Variables you need:" +noi di "age -- age as integer" +noi di "b_AF -- atrial fibrilation as binary 01" +noi di "b_ra -- rheumatoid arthritis as binary 01" +noi di "b_renal -- rename disease as binary 01" +noi di "b_treatedhyp -- hypertension treatment as binary 01" +noi di "b_type1 -- type 1 diabetes as binary 01" +noi di "b_type2 -- type 2 diabetes as binary 01" +noi di "bmi -- bmi as continuous numeric, replaced with 20 if below 20" +noi di "ethrisk -- ethnicity as categorical integer (0-9)" +noi di "fh_cvd -- family history as binary 01" +noi di "rati -- HDL LDL cholesterol ratio as continuous numeric" +noi di "sbp -- systolic blood pressure as continuous numeric" +noi di "smoke_cat -- smoking as integer category (0-4)" +noi di "sex -- sex as 1=male 2=female" + +noi di "b_atypicalantipsy -- atypical antipsychotic prescribing as binary 01" +noi di "b_semi - severe mental illness as binary 01" +noi di "sbp_s5 - systolic blood pressure variability as continuous numeric" +noi di "b_impotence2 - erectile dysfunction as binary 01" +noi di "b_sle - systemic lupus erythematosus as binary 01" +noi di "b_corticosteroids - corticosteroid prescribing as binary 01" + + +cap gen score=. +gen double rec_num = _n +preserve + +keep if sex==2 +*replace bmi=20 if bmi<20 + + +local surv=10 +forvalues i = 0/15 { + gen survivor`i'=0 +} +replace survivor10=0.988876402378082 +*this part is added in incase they want to do risk scores over a different number of years but they haven't actually done the groundwork yet. + +gen Iethrisk0 = 0 +gen Iethrisk1 = 0 +gen Iethrisk2 = 0.2804031433299542500000000 +gen Iethrisk3 = 0.5629899414207539800000000 +gen Iethrisk4 = 0.2959000085111651600000000 +gen Iethrisk5 = 0.0727853798779825450000000 +gen Iethrisk6 = -0.1707213550885731700000000 +gen Iethrisk7 = -0.3937104331487497100000000 +gen Iethrisk8 = -0.3263249528353027200000000 +gen Iethrisk9 = -0.1712705688324178400000000 + +gen Ismoke0=0 +gen Ismoke1=0.1338683378654626200000000 +gen Ismoke2=0.5620085801243853700000000 +gen Ismoke3=0.6674959337750254700000000 +gen Ismoke4=0.8494817764483084700000000 + +gen double dage = age +replace dage=dage/10 +gen double age_1 = dage^(-2) +gen double age_2 = dage +gen double dbmi = bmi +replace dbmi=dbmi/10 +gen double bmi_1 = dbmi^(-2) +gen double bmi_2 = ln(dbmi)*dbmi^(-2) + +replace age_1 = age_1 - 0.053274843841791 +replace age_2 = age_2 - 4.332503318786621 +replace bmi_1 = bmi_1 - 0.154946178197861 +replace bmi_2 = bmi_2 - 0.144462317228317 +replace rati = rati - 3.476326465606690 +replace sbp = sbp - 123.130012512207030 +replace sbps5 = sbps5 - 9.002537727355957 +replace town = town - 0.392308831214905 + +/* Start of Sum */ +gen double a=0 + + +/* The conditional sums */ +forvalues i=1/9 { +replace a = a+ Iethrisk`i' if ethrisk==`i' +} +forvalues i=0/4{ +replace a = a+ Ismoke`i' if smoke_cat==`i' +} + + /* Sum from continuous values */ + + + replace a = a+ age_1 * -8.1388109247726188000000000 + replace a = a+ age_2 * 0.7973337668969909800000000 + replace a = a+ bmi_1 * 0.2923609227546005200000000 + replace a = a+ bmi_2 * -4.1513300213837665000000000 + replace a = a+ rati * 0.1533803582080255400000000 + replace a = a+ sbp * 0.0131314884071034240000000 + replace a = a+ sbps5 * 0.0078894541014586095000000 if sbps5 != . + replace a = a+ town * 0.0772237905885901080000000 + + + + /* Sum from boolean values */ + + replace a = a+ b_AF * 1.5923354969269663000000000 + replace a = a+ b_atypicalantipsy * 0.2523764207011555700000000 + replace a = a+ b_corticosteroids * 0.5952072530460185100000000 + replace a = a+ b_migraine * 0.3012672608703450000000000 + replace a = a+ b_ra * 0.2136480343518194200000000 + replace a = a+ b_renal * 0.6519456949384583300000000 + replace a = a+ b_semi * 0.1255530805882017800000000 + replace a = a+ b_sle * 0.7588093865426769300000000 + replace a = a+ b_treatedhyp * 0.5093159368342300400000000 + replace a = a+ b_type1 * 1.7267977510537347000000000 + replace a = a+ b_type2 * 1.0688773244615468000000000 + replace a = a+ fh_cvd * 0.4544531902089621300000000 + + /* Sum from interaction terms */ + + replace a = a+ age_1 * -4.7057161785851891000000000 if smoke_cat==1 + replace a = a+ age_1 * -2.7430383403573337000000000 if smoke_cat==2 + replace a = a+ age_1 * -0.8660808882939218200000000 if smoke_cat==3 + replace a = a+ age_1 * 0.9024156236971064800000000 if smoke_cat==4 + replace a = a+ age_1 * b_AF * 19.9380348895465610000000000 + replace a = a+ age_1 * b_corticosteroids * -0.9840804523593628100000000 + replace a = a+ age_1 * b_migraine * 1.7634979587872999000000000 + replace a = a+ age_1 * b_renal * -3.5874047731694114000000000 + replace a = a+ age_1 * b_sle * 19.6903037386382920000000000 + replace a = a+ age_1 * b_treatedhyp * 11.8728097339218120000000000 + replace a = a+ age_1 * b_type1 * -1.2444332714320747000000000 + replace a = a+ age_1 * b_type2 * 6.8652342000009599000000000 + replace a = a+ age_1 * bmi_1 * 23.8026234121417420000000000 + replace a = a+ age_1 * bmi_2 * -71.1849476920870070000000000 + replace a = a+ age_1 * fh_cvd * 0.9946780794043512700000000 + replace a = a+ age_1 * sbp * 0.0341318423386154850000000 + replace a = a+ age_1 * town * -1.0301180802035639000000000 + replace a = a+ age_2 * -0.0755892446431930260000000 if smoke_cat==1 + replace a = a+ age_2 * -0.1195119287486707400000000 if smoke_cat==2 + replace a = a+ age_2 * -0.1036630639757192300000000 if smoke_cat==3 + replace a = a+ age_2 * -0.1399185359171838900000000 if smoke_cat==4 + replace a = a+ age_2 * b_AF * -0.0761826510111625050000000 + replace a = a+ age_2 * b_corticosteroids * -0.1200536494674247200000000 + replace a = a+ age_2 * b_migraine * -0.0655869178986998590000000 + replace a = a+ age_2 * b_renal * -0.2268887308644250700000000 + replace a = a+ age_2 * b_sle * 0.0773479496790162730000000 + replace a = a+ age_2 * b_treatedhyp * 0.0009685782358817443600000 + replace a = a+ age_2 * b_type1 * -0.2872406462448894900000000 + replace a = a+ age_2 * b_type2 * -0.0971122525906954890000000 + replace a = a+ age_2 * bmi_1 * 0.5236995893366442900000000 + replace a = a+ age_2 * bmi_2 * 0.0457441901223237590000000 + replace a = a+ age_2 * fh_cvd * -0.0768850516984230380000000 + replace a = a+ age_2 * sbp * -0.0015082501423272358000000 + replace a = a+ age_2 * town * -0.0315934146749623290000000 + + + gen double scoref = exp(a) + replace scoref=survivor`surv'^scoref + replace scoref = 1-scoref + replace scoref=100*scoref + *gen double scoref = 100.0 * (1 - survivor`surv'^(exp(a))) + +keep scoref patid rec_num + +tempfile temper +save `temper' , replace +restore +merge 1:1 patid rec_num using `temper' , nogen +drop rec_num + +replace score=scoref if scoref!=. +drop scoref + +end + + + + + + + + + + + + + diff --git a/pr_cvd_male_raw_bmirep_qrisk2.do b/pr_cvd_male_raw_bmirep_qrisk2.do new file mode 100644 index 0000000..f115033 --- /dev/null +++ b/pr_cvd_male_raw_bmirep_qrisk2.do @@ -0,0 +1,175 @@ +*use a dataset including +cap prog drop cvd_male_raw_qrisk2 +program define cvd_male_raw_qrisk2 + +noi di "Variables you need:" +noi di "age -- age as integer" +noi di "b_AF -- atrial fibrilation as binary 01" +noi di "b_ra -- rheumatoid arthritis as binary 01" +noi di "b_renal -- rename disease as binary 01" +noi di "b_treatedhyp -- hypertension treatment as binary 01" +noi di "b_type1 -- type 1 diabetes as binary 01" +noi di "b_type2 -- type 2 diabetes as binary 01" +noi di "bmi -- bmi as continuous numeric" +noi di "ethrisk -- ethnicity as categorical integer (0-9)" +noi di "fh_cvd -- family history as binary 01" +noi di "rati -- HDL LDL cholesterol ratio as continuous numeric" +noi di "sbp -- systolic blood pressure as continuous numeric" +noi di "smoke_cat -- smoking as integer category (0-4)" +noi di "town -- townsend deprivation score as continuous numeric -- not clear how this is provided, as score or quintile" +noi di "sex -- sex as 1=male 2=female" + +cap gen score=. +gen double rec_num=_n +preserve +keep if sex==1 +*replace bmi=20 if bmi<20 + + +local surv=10 +forvalues i = 0/15 { + gen survivor`i'=0 +} +replace survivor10=0.978794217109680 +*this part is added in incase they want to do risk scores over a different number of years but they haven't actually done the groundwork yet. + +gen Iethrisk0 = 0 +gen Iethrisk1 = 0 /*5*/ +gen Iethrisk2 = 0.3173321430481919100000000 /*3*/ +gen Iethrisk3 = 0.4738590786081115500000000 /*2*/ +gen Iethrisk4 = 0.5171314655968145500000000 /*1*/ +gen Iethrisk5 = 0.1370301157366419200000000 /*4*/ +gen Iethrisk6 = -0.3885522304972663900000000 /*8*/ +gen Iethrisk7 = -0.3812495485312194500000000 /*7*/ +gen Iethrisk8 = -0.4064461381650994500000000 /*9*/ +gen Iethrisk9 = -0.2285715521377336100000000 /*6*/ + +gen Ismoke0=0 +gen Ismoke1=0.2684479158158020200000000 +gen Ismoke2=0.6307674973877591700000000 +gen Ismoke3=0.7178078883378695700000000 +gen Ismoke4=0.8704172533465485100000000 + +gen double dage = age +replace dage=dage/10 +gen double age_1 = dage^(-1) +gen double age_2 = dage^2 +gen double dbmi = bmi +replace dbmi=dbmi/10 +gen double bmi_2 = ln(dbmi)*dbmi^(-2) +gen double bmi_1 = dbmi^(-2) + +replace age_1 = age_1 - 0.233734160661697 +replace age_2 = age_2 - 18.304403305053711 +replace bmi_1 = bmi_1 - 0.146269768476486 +replace bmi_2 = bmi_2 - 0.140587374567986 +replace rati = rati - 4.321151256561279 +replace sbp = sbp - 130.589752197265620 +replace town = town - 0.551009356975555 + + + +gen double a=0 + +/* The conditional sums */ +forvalues i=1/9 { +replace a = a+ Iethrisk`i' if ethrisk==`i' +} +forvalues i=0/4 { +replace a = a+ Ismoke`i' if smoke_cat==`i' +} + + + replace a = a+ age_1 * -18.0437312550377270000000000 + replace a = a+ age_2 * 0.0236486454254306940000000 + replace a = a+ bmi_1 * 2.5388084343581578000000000 + replace a = a+ bmi_2 * -9.1034725871528597000000000 + replace a = a+ rati * 0.1684397636136909500000000 + replace a = a+ sbp * 0.0105003089380754820000000 + replace a = a+ town * 0.0323801637634487590000000 + + + + /* Sum from boolean values */ + + + replace a = a+ b_AF * 1.0363048000259454000000000 + replace a = a+ b_ra * 0.2519953134791012600000000 + replace a = a+ b_renal * 0.8359352886995286000000000 + replace a = a+ b_treatedhyp * 0.6603459695917862600000000 + replace a = a+ b_type1 * 1.3309170433446138000000000 + replace a = a+ b_type2 * 0.9454348892774417900000000 + replace a = a+ fh_cvd * 0.5986037897136281500000000 + + + + /* Sum from interaction terms */ + + + replace a = a+ age_1 * 1 * 0.6186864699379683900000000 if smoke_cat==1 + replace a = a+ age_1 * 1 * 1.5522017055600055000000000 if smoke_cat==2 + replace a = a+ age_1 * 1 * 2.4407210657517648000000000 if smoke_cat==3 + replace a = a+ age_1 * 1 * 3.5140494491884624000000000 if smoke_cat==4 + replace a = a+ age_1 * b_AF * 8.0382925558108482000000000 + replace a = a+ age_1 * b_renal * -1.6389521229064483000000000 + replace a = a+ age_1 * b_treatedhyp * 8.4621771382346651000000000 + replace a = a+ age_1 * b_type1 * 5.4977016563835504000000000 + replace a = a+ age_1 * b_type2 * 3.3974747488766690000000000 + replace a = a+ age_1 * bmi_1 * 33.8489881012767600000000000 + replace a = a+ age_1 * bmi_2 * -140.6707025404897100000000000 + replace a = a+ age_1 * fh_cvd * 2.0858333154353321000000000 + replace a = a+ age_1 * sbp * 0.0501283668830720540000000 + replace a = a+ age_1 * town * -0.1988268217186850700000000 + replace a = a+ age_2 * 1 * -0.0040893975066796338000000 if smoke_cat==1 + replace a = a+ age_2 * 1 * -0.0056065852346001768000000 if smoke_cat==2 + replace a = a+ age_2 * 1 * -0.0018261006189440492000000 if smoke_cat==3 + replace a = a+ age_2 * 1 * -0.0014997157296173290000000 if smoke_cat==4 + replace a = a+ age_2 * b_AF * 0.0052471594895864343000000 + replace a = a+ age_2 * b_renal * -0.0179663586193546390000000 + replace a = a+ age_2 * b_treatedhyp * 0.0092088445323379176000000 + replace a = a+ age_2 * b_type1 * 0.0047493510223424558000000 + replace a = a+ age_2 * b_type2 * -0.0048113775783491563000000 + replace a = a+ age_2 * bmi_1 * 0.0627410757513945650000000 + replace a = a+ age_2 * bmi_2 * -0.2382914909385732100000000 + replace a = a+ age_2 * fh_cvd * -0.0049971149213281010000000 + replace a = a+ age_2 * sbp * -0.0000523700987951435090000 + replace a = a+ age_2 * town * -0.0012518116569283104000000 + + + + + + /* Sum from continuous values */ + + + gen double scorem = exp(a) + replace scorem=survivor`surv'^scorem + replace scorem = 1-scorem + replace scorem=100*scorem + *gen double scorem = 100.0 * (1 - survivor`surv'^(exp(a))) + +keep scorem patid rec_num + +tempfile temper +save `temper' , replace +restore +merge 1:1 patid rec_num using `temper' , nogen +drop rec_num + +replace score=scorem if scorem!=. +drop scorem + +end + + + + + + + + + + + + + diff --git a/pr_cvd_male_raw_bmirep_qrisk3.do b/pr_cvd_male_raw_bmirep_qrisk3.do new file mode 100644 index 0000000..dc623f5 --- /dev/null +++ b/pr_cvd_male_raw_bmirep_qrisk3.do @@ -0,0 +1,192 @@ +*use a dataset including +cap prog drop cvd_male_raw_qrisk3 +program define cvd_male_raw_qrisk3 + +noi di "age -- age as integer" +noi di "b_AF -- atrial fibrilation as binary 01" +noi di "b_ra -- rheumatoid arthritis as binary 01" +noi di "b_renal -- rename disease as binary 01" +noi di "b_treatedhyp -- hypertension treatment as binary 01" +noi di "b_type1 -- type 1 diabetes as binary 01" +noi di "b_type2 -- type 2 diabetes as binary 01" +noi di "bmi -- bmi as continuous numeric, replaced with 20 if below 20" +noi di "ethrisk -- ethnicity as categorical integer (0-9)" +noi di "fh_cvd -- family history as binary 01" +noi di "rati -- HDL LDL cholesterol ratio as continuous numeric" +noi di "sbp -- systolic blood pressure as continuous numeric" +noi di "smoke_cat -- smoking as integer category (0-4)" +noi di "sex -- sex as 1=male 2=female" + +noi di "b_atypicalantipsy -- atypical antipsychotic prescribing as binary 01" +noi di "b_semi - severe mental illness as binary 01" +noi di "sbp_s5 - systolic blood pressure variability as continuous numeric" +noi di "b_impotence2 - erectile dysfunction as binary 01" +noi di "b_sle - systemic lupus erythematosus as binary 01" +noi di "b_corticosteroids - corticosteroid prescribing as binary 01" + +cap gen score=. +gen double rec_num=_n +preserve +keep if sex==1 +*replace bmi=20 if bmi<20 + + +local surv=10 +forvalues i = 0/15 { + gen survivor`i'=0 +} +replace survivor10=0.977268040180206 +*this part is added in incase they want to do risk scores over a different number of years but they haven't actually done the groundwork yet. + +gen Iethrisk0 = 0 +gen Iethrisk1 = 0 /*5*/ +gen Iethrisk2 = 0.2771924876030827900000000 /*3*/ +gen Iethrisk3 = 0.4744636071493126800000000 /*2*/ +gen Iethrisk4 = 0.5296172991968937100000000 /*1*/ +gen Iethrisk5 = 0.0351001591862990170000000 /*4*/ +gen Iethrisk6 = -0.3580789966932791900000000 /*8*/ +gen Iethrisk7 = -0.4005648523216514000000000 /*7*/ +gen Iethrisk8 = -0.4152279288983017300000000 /*9*/ +gen Iethrisk9 = -0.2632134813474996700000000 /*6*/ + +gen Ismoke0=0 +gen Ismoke1=0.1912822286338898300000000 +gen Ismoke2=0.5524158819264555200000000 +gen Ismoke3=0.6383505302750607200000000 +gen Ismoke4=0.7898381988185801900000000 + +gen double dage = age +replace dage=dage/10 +gen double age_1 = dage^(-1) +gen double age_2 = dage^3 +gen double dbmi = bmi +replace dbmi=dbmi/10 +gen double bmi_2 = ln(dbmi)*dbmi^(-2) +gen double bmi_1 = dbmi^(-2) + +replace age_1 = age_1 - 0.234766781330109 +replace age_2 = age_2 - 77.284080505371094 +replace bmi_1 = bmi_1 - 0.149176135659218 +replace bmi_2 = bmi_2 - 0.141913309693336 +replace rati = rati - 4.300998687744141 +replace sbp = sbp - 128.571578979492190 +replace sbps5 = sbps5 - 8.756621360778809 +replace town = town - 0.526304900646210 + + + +gen double a=0 + +/* The conditional sums */ +forvalues i=1/9 { +replace a = a+ Iethrisk`i' if ethrisk==`i' +} +forvalues i=0/4 { +replace a = a+ Ismoke`i' if smoke_cat==`i' +} + + + replace a = a+ age_1 * -17.8397816660055750000000000 + replace a = a+ age_2 * 0.0022964880605765492000000 + replace a = a+ bmi_1 * 2.4562776660536358000000000 + replace a = a+ bmi_2 * -8.3011122314711354000000000 + replace a = a+ rati * 0.1734019685632711100000000 + replace a = a+ sbp * 0.0129101265425533050000000 + replace a = a+ sbps5 * 0.0102519142912904560000000 if sbps5 != . + replace a = a+ town * 0.0332682012772872950000000 + + /* Sum from boolean values */ + + + replace a = a+ b_AF * 0.8820923692805465700000000 + replace a = a+ b_atypicalantipsy * 0.1304687985517351300000000 + replace a = a+ b_corticosteroids * 0.4548539975044554300000000 + replace a = a+ b_impotence2 * 0.2225185908670538300000000 + replace a = a+ b_migraine * 0.2558417807415991300000000 + replace a = a+ b_ra * 0.2097065801395656700000000 + replace a = a+ b_renal * 0.7185326128827438400000000 + replace a = a+ b_semi * 0.1213303988204716400000000 + replace a = a+ b_sle * 0.4401572174457522000000000 + replace a = a+ b_treatedhyp * 0.5165987108269547400000000 + replace a = a+ b_type1 * 1.2343425521675175000000000 + replace a = a+ b_type2 * 0.8594207143093222100000000 + replace a = a+ fh_cvd * 0.5405546900939015600000000 + + + + /* Sum from interaction terms */ + + + replace a = a+ age_1 * 1 * -0.2101113393351634600000000 if smoke_cat==1 + replace a = a+ age_1 * 1 * 0.7526867644750319100000000 if smoke_cat==2 + replace a = a+ age_1 * 1 * 0.9931588755640579100000000 if smoke_cat==3 + replace a = a+ age_1 * 1 * 2.1331163414389076000000000 if smoke_cat==4 + replace a = a+ age_1 * b_AF * 3.4896675530623207000000000 + replace a = a+ age_1 * b_corticosteroids * 1.1708133653489108000000000 + replace a = a+ age_1 * b_impotence2 * -1.5064009857454310000000000 + replace a = a+ age_1 * b_migraine * 2.3491159871402441000000000 + replace a = a+ age_1 * b_renal * -0.5065671632722369400000000 + replace a = a+ age_1 * b_treatedhyp * 6.5114581098532671000000000 + replace a = a+ age_1 * b_type1 * 5.3379864878006531000000000 + replace a = a+ age_1 * b_type2 * 3.6461817406221311000000000 + replace a = a+ age_1 * bmi_1 * 31.0049529560338860000000000 + replace a = a+ age_1 * bmi_2 * -111.2915718439164300000000000 + replace a = a+ age_1 * fh_cvd * 2.7808628508531887000000000 + replace a = a+ age_1 * sbp * 0.0188585244698658530000000 + replace a = a+ age_1 * town * -0.1007554870063731000000000 + replace a = a+ age_2 * -0.0004985487027532612100000 if smoke_cat==1 + replace a = a+ age_2 * -0.0007987563331738541400000 if smoke_cat==2 + replace a = a+ age_2 * -0.0008370618426625129600000 if smoke_cat==3 + replace a = a+ age_2 * -0.0007840031915563728900000 if smoke_cat==4 + replace a = a+ age_2 * b_AF * -0.0003499560834063604900000 + replace a = a+ age_2 * b_corticosteroids * -0.0002496045095297166000000 + replace a = a+ age_2 * b_impotence2 * -0.0011058218441227373000000 + replace a = a+ age_2 * b_migraine * 0.0001989644604147863100000 + replace a = a+ age_2 * b_renal * -0.0018325930166498813000000 + replace a = a+ age_2 * b_treatedhyp * 0.0006383805310416501300000 + replace a = a+ age_2 * b_type1 * 0.0006409780808752897000000 + replace a = a+ age_2 * b_type2 * -0.0002469569558886831500000 + replace a = a+ age_2 * bmi_1 * 0.0050380102356322029000000 + replace a = a+ age_2 * bmi_2 * -0.0130744830025243190000000 + replace a = a+ age_2 * fh_cvd * -0.0002479180990739603700000 + replace a = a+ age_2 * sbp * -0.0000127187419158845700000 + replace a = a+ age_2 * town * -0.0000932996423232728880000 + + + + + + /* Sum from continuous values */ + + + gen double scorem = exp(a) + replace scorem=survivor`surv'^scorem + replace scorem = 1-scorem + replace scorem=100*scorem + *gen double scorem = 100.0 * (1 - survivor`surv'^(exp(a))) + +keep scorem patid rec_num + +tempfile temper +save `temper' , replace +restore +merge 1:1 patid rec_num using `temper' , nogen +drop rec_num + +replace score=scorem if scorem!=. +drop scorem + +end + + + + + + + + + + + + + diff --git a/pr_get_ED.do b/pr_get_ED.do new file mode 100644 index 0000000..2fed121 --- /dev/null +++ b/pr_get_ED.do @@ -0,0 +1,29 @@ +cap prog drop pr_get_ED + +prog define pr_get_ED +syntax , clinicalfile(string) referralfile(string) therapyfile(string) begin(string) end(string) runin(string) [wide] + +preserve +*first ever diagnosis code +pr_get_bclin, variable(ED) qof(notqof) clinicalfile(`clinicalfile') referralfile(`referralfile') begin(`begin') end(`end') runin(`runin') + +tempfile b_ED +save `b_ED', replace + +restore +*first ever treatment +pr_get_btherapy, variable(ED_drugs) therapyfile(`therapyfile') begin(`begin') end(`end') runin(`runin') time(ever) + +append using `b_ED' + +*first of either diagnosis or treatment +replace EDdate = ED_drugsdate if EDdate == . +assert EDdate !=. + +replace b_ED = b_ED_drugs if b_ED == . +assert b_ED !=. + +drop ED_drugs b_ED_drugs +bysort patid EDdate: keep if _n==1 + +end diff --git a/pr_get_bclin.do b/pr_get_bclin.do new file mode 100644 index 0000000..1175191 --- /dev/null +++ b/pr_get_bclin.do @@ -0,0 +1,74 @@ +cap prog drop pr_get_bclin + +capture program drop pr_get_bclin +prog define pr_get_bclin +syntax , variable(string) qof(string) clinicalfile(string) referralfile(string) begin(string) end(string) runin(string) [wide] + + +preserve + +*get all clinical and referral events matching code list +if "`qof'" == "qof" { + use "$QRiskCodelistdir\cr_codelist_qof_cod.dta", clear + keep if variable=="b_`variable'" + } + else { + use "$QRiskCodelistdir\cr_codelist_`variable'.dta", clear + } + +merge 1:m medcode using `clinicalfile' , nogen keep(match) + +tempfile temp +save `temp', replace + +if "`qof'" == "qof" { + use "$QRiskCodelistdir\cr_codelist_qof_cod.dta", clear + keep if variable=="b_`variable'" + } + else { + use "$QRiskCodelistdir\cr_codelist_`variable'.dta", clear + } + +merge 1:m medcode using `referralfile' , nogen keep(match) +append using `temp' + +gen b_`variable'=1 +keep patid eventdate b_`variable' +tempfile bclin +save `bclin' , replace + +*merge with patient file +restore +merge 1:m patid using `bclin', keep(match master) + +*keep first event within specified time table +drop if _merge==3 & eventdate>`end' +drop if _merge==3 & eventdate<`begin'-365.25*`runin' +gsort patid -eventdate +duplicates drop patid if eventdate<`begin' & _merge==3 , force + +sort patid eventdate +by patid: keep if _n==1 + +replace b_`variable'=0 if b_`variable'==. + +*add record for unexposed time between start of follow-up and first event +expand 2 if eventdate>`begin' & eventdate!=. & patid[_n]!=patid[_n-1] +sort patid eventdate +by patid: replace b_`variable'=0 if _n==1 & eventdate>`begin' +by patid: replace eventdate = `begin' if _n==1 & eventdate>`begin' + +drop _merge +rename eventdate `variable'date + +*HS - I'm not sure what this loop is meant to do but it doesn't run in this do file +if "`wide'"=="wide" { +by patid: gen b_clin_num=_n + +reshape wide b_`variable' `variable'date, i(patid) j(b_clin_num) +} + +end + + + diff --git a/pr_get_fh_cvd_60.do b/pr_get_fh_cvd_60.do new file mode 100644 index 0000000..7e9dc61 --- /dev/null +++ b/pr_get_fh_cvd_60.do @@ -0,0 +1,37 @@ +cap prog drop pr_get_fh_cvd +program define pr_get_fh_cvd +syntax, clinicalfile(string) begin(string) end(string) runin(integer) + +preserve + +use `clinicalfile' , clear +merge m:1 medcode using "$QRiskCodelistdir\cr_codelist_fh_cvd_60" , nogen keep(match) +rename eventdate fh_date +keep fh_date patid + +tempfile tempura +save `tempura' , replace + +restore +merge 1:m patid using `tempura', nogen keep(match master) + +drop if fh_date<`begin'-`runin'*365.25 | fh_date>`end' & fh_date!=. +sort patid fh_date +by patid: keep if _n==1 +*keep first in study period* +gen fh_cvd=1 if fh_date<=`begin' +*set record to 1 if fh recorded before begin of follow up + +expand 2 if fh_cvd!=1 & fh_date!=. +*generate another record if it has been recorded later in fup +sort patid +by patid: replace fh_date=`begin' if _N>1 & _n==1 +*first record keep fh=0 & set date to start of follow up +by patid: replace fh_cvd=1 if _N>1 & _n==2 +*second record keep record date and set fh=1 + +*set remaining missing records to zero +replace fh_cvd=0 if fh_cvd==. +replace fh_date=`begin' if fh_date==. + +end diff --git a/pr_get_tchdl_medcoded.do b/pr_get_tchdl_medcoded.do new file mode 100644 index 0000000..71540b1 --- /dev/null +++ b/pr_get_tchdl_medcoded.do @@ -0,0 +1,52 @@ +cap prog drop pr_get_tchdl_updates +program define pr_get_tchdl_updates +syntax, testfile(string) begin(string) end(string) runin(integer) [wide cutoff(string)] + +if "`cutoff'"=="" { + local cutoff "." +} + +preserve + +use `testfile' , clear + +keep if medcode==14371 | medcode==14372 | medcode==14108 +keep if data3==0 | data3==1 | data3==151 | data3==161 +drop if data2>`cutoff' +drop if data2==0 +rename data2 TC_HDLratio +rename eventdate TC_HDLdate +drop if TC_HDLdate == . + +*HDL:LDL ratios recorded on same date +keep patid TC_HDLratio TC_HDLdate +duplicates drop +duplicates tag patid TC_HDLdate, gen(dup) +drop if dup > 0 /*Different lab tests on same day indicative of error?*/ + +keep patid TC_HDLratio TC_HDLdate +tempfile tempura +save `tempura' , replace +restore +merge 1:m patid using `tempura' , nogen keep(match master) + +drop if (TC_HDLdate<`begin'-365.25*`runin' | TC_HDLdate>`end') & TC_HDLdate!=. +gsort patid -TC_HDLdate +duplicates drop patid if TC_HDLdate<`begin', force + +sort patid TC_HDLdate TC_HDLratio +expand 2 if TC_HDLdate>`begin' & TC_HDLdate!=. & patid[_n]!=patid[_n-1] +sort patid TC_HDLdate +by patid: replace TC_HDLratio=. if _n==1 & TC_HDLdate>`begin' +by patid: replace TC_HDLdate=`begin' if _n==1 & TC_HDLdate>`begin' + + +if "`wide'"=="wide" { +by patid: gen TC_HDLnum=_n +reshape wide TC_HDLratio TC_HDLdate, i(patid) j(TC_HDLnum) +} + + +end + + diff --git a/pr_getdiabetes.do b/pr_getdiabetes.do new file mode 100644 index 0000000..9ea1630 --- /dev/null +++ b/pr_getdiabetes.do @@ -0,0 +1,80 @@ +cap prog drop pr_get_diabetes + +capture program drop pr_get_diabetes +prog define pr_get_diabetes +syntax , clinicalfile(string) referralfile(string) begin(string) end(string) runin(string) [wide] + + +preserve + +*get all clinical and referral events matching code list +use "$QRiskCodelistdir\cr_codelist_qof_cod.dta", clear +keep if variable == "b_type1" | variable == "b_type2" + +merge 1:m medcode using `clinicalfile' , nogen keep(match) + +tempfile temp +save `temp', replace + +use "$QRiskCodelistdir\cr_codelist_qof_cod.dta", clear +keep if variable == "b_type1" | variable == "b_type2" + +merge 1:m medcode using `referralfile' , nogen keep(match) +append using `temp' + +gen diabetes = 1 if variable == "b_type1" +replace diabetes = 2 if variable == "b_type2" +keep patid eventdate diabetes + +tempfile bdiab +save `bdiab' , replace + +*merge with patient file +restore +merge 1:m patid using `bdiab', keep(match master) + +*keep records within specified timetable +drop if _merge==3 & eventdate>`end' +drop if _merge==3 & eventdate<`begin'-365.25*`runin' +gsort patid -eventdate +duplicates drop patid if eventdate<`begin' & _merge==3 , force + +sort patid eventdate +*by patid: keep if _n==1 + +replace diabetes=0 if diab==. + +*if sequential records are for the same diabetes type, keep the first record +by patid: gen drop = 1 if patid[_n]==patid[_n-1] & diab[_n] == diab[_n-1] +drop if drop == 1 +drop drop + +*add record for unexposed time between start of follow-up and first event +expand 2 if eventdate>`begin' & eventdate!=. & patid[_n]!=patid[_n-1] +sort patid eventdate +by patid: replace diab=0 if _n==1 & eventdate>`begin' +by patid: replace eventdate = `begin' if _n==1 & eventdate>`begin' + +/*at each record, set the recorded type to 1 and the other to 0 +i.e. diabetes is defined by the latest record and patients cannot have +type 1 and type 2 diabetes at the same time*/ +gen b_type1 = 0 +gen b_type2 = 0 +replace b_type1 = 1 if diab == 1 +replace b_type2 = 1 if diab == 2 + +gen type1date = eventdate +gen type2date = eventdate +format type1date type2date %dD/N/CY +drop eventdate diabetes + +if "`wide'"=="wide" { +by patid: gen b_clin_num=_n + +reshape wide b_`variable' `variable'date, i(patid) j(b_clin_num) +} + +end + + + diff --git a/pr_getlastsbpvariability.do b/pr_getlastsbpvariability.do new file mode 100644 index 0000000..b6303a5 --- /dev/null +++ b/pr_getlastsbpvariability.do @@ -0,0 +1,60 @@ +cap prog drop pr_getlastsbpvariability + +program define pr_getlastsbpvariability +syntax , clinicalfile(string) additionalfile(string) index(string) runin(integer) [limitlow(string) limithigh(string)] + +if "`limitlow'"=="" { + local limitlow "0" +} + +if "`limithigh'"=="" { + local limithigh "." +} +preserve + +use "$QRiskCodelistdir\cr_codelist_qof_cod.dta", clear +keep if variable=="sbp" +merge 1:m medcode using `clinicalfile' , keep(match) nogen + +replace adid = -_n if adid==0 +rename value sbp +merge 1:1 patid adid using `additionalfile', keep(match master) + +replace sbp=data2 if data2!=. + +drop if eventdate == . +drop if sbp==. +drop if sbp==1 +drop if sbp >`limithigh' +drop if sbp <`limitlow' + +*keep lowest sbp if more than one measurement on same day +keep patid eventdate sbp +duplicates tag patid eventdate, gen(dup) +bysort patid eventdate (sbp): keep if _n==1 + +keep patid eventdate sbp + +tempfile sbprecs +save `sbprecs' , replace + + +restore +merge 1:m patid using `sbprecs' , keep(match master) +sort patid eventdate + + +*identify all sbp measurements recorded in the five years before study entry +gen _diff = `index' - eventdate +gen _sbp5yr = 1 if _diff < (365.25*5) & _diff >= 0 +by patid: egen _countsbp5yr = count(_sbp5yr) + +*calculate standard deviation where there are two or more recorded values +by patid: egen sbp_sd = sd(sbp) if _sbp5yr == 1 & _countsbp5yr >= 2 +keep patid sbp_sd +keep if sbp_sd !=. +duplicates drop +isid patid + +end + diff --git a/pr_getsbpupdates.do b/pr_getsbpupdates.do new file mode 100644 index 0000000..0c6a64c --- /dev/null +++ b/pr_getsbpupdates.do @@ -0,0 +1,67 @@ +cap prog drop pr_getsbpupdates + +program define pr_getsbpupdates +syntax , clinicalfile(string) additionalfile(string) begin(string) end(string) runin(integer) [limitlow(string) limithigh(string)] [wide] + +if "`limitlow'"=="" { + local limitlow "0" +} + +if "`limithigh'"=="" { + local limithigh "." +} + +preserve + +use "$QRiskCodelistdir\cr_codelist_qof_cod", clear +keep if variable=="sbp" +merge 1:m medcode using `clinicalfile' , keep(match) nogen + +replace adid = -_n if adid==0 +rename value sbp +merge 1:1 patid adid using `additionalfile', keep(match master) +replace sbp=data2 if data2!=. + +drop if eventdate == . +drop if sbp==. +drop if sbp==1 +drop if sbp >`limithigh' +drop if sbp <`limitlow' + +keep patid eventdate sbp + +*keep lowest sbp if more than one measurement on same day +keep patid eventdate sbp +duplicates tag patid eventdate, gen(dup) +bysort patid eventdate (sbp): keep if _n==1 + +tempfile sbprecs +save `sbprecs' , replace + +restore +merge 1:m patid using `sbprecs' , keep(match master) + +rename eventdate bpdate +drop if (bpdate<`begin'-365.25*`runin' | bpdate>`end') & _merge==3 +gsort patid -bpdate +duplicates drop patid if bpdate<`begin', force + +sort patid bpdate +by patid: drop if sbp[_n-1]==sbp & _n>1 + +expand 2 if bpdate>`begin' & bpdate!=. & patid[_n]!=patid[_n-1] +sort patid bpdate +by patid: replace sbp=. if _n==1 & bpdate>`begin' +by patid: replace bpdate=`begin' if _n==1 & bpdate>`begin' + + + +drop _merge +if "`wide'"=="wide" { +by patid: gen sbp_rec_num=_n + +reshape wide sbp bpdate, i(patid) j(sbp_rec_num) +} + +end + diff --git a/pr_getsmokingupdates.do b/pr_getsmokingupdates.do new file mode 100644 index 0000000..aaa9cb5 --- /dev/null +++ b/pr_getsmokingupdates.do @@ -0,0 +1,103 @@ +cap prog drop pr_getsmokingupdates + +program define pr_getsmokingupdates +syntax , clinicalfile(string) additionalfile(string) smokingstatusvar(string) begin(string) end(string) runin(integer) [wide] + +preserve + +use "$QRiskCodelistdir\cr_codelist_qof_cod", clear +keep if variable== "smoke_cat" +*NOTE: EH AMENDED 21/09/2018 - MEDCODE 54, Read code 137..00 "Tobacco consumption" is being removed from the code list as it does not indicate smoking status. +drop if medcode==54 +merge 1:m medcode using `clinicalfile' , keep(match) nogen + +rename value clinstatus +label define clinstatuslab 0 "non-smoker" 1 "ex-smoker" 2 "light smoker" 3 "moderate smoker" 4 "heavy smoker" 234 "smoker amount unknown", replace +label values clinstatus clinstatuslab + +*merge with additional file +*main medcodes associated with enttype that are not in the smoking code list = 54 tobacco consumption, 60 current non-smoker, 2111 health education - smoking, 7622 smoking cessation advice +replace adid = -_n if adid==0 +merge 1:1 patid adid using `additionalfile' +drop if eventdate == . + +drop if _merge == 2 & enttype !=4 /*drop additional files not found using codelist and not in the smoking entity type*/ +replace clinstatus = . if _merge == 2 +tab medcode _merge, m + +*smoking status using additional clinical details file +gen adstatus = . +replace adstatus = 0 if data1 == 2 +replace adstatus = 1 if data1 == 3 +replace adstatus = 234 if data1 == 1 +label values adstatus clinstatuslab + +*update clinical and additional details status using cigarettes per day +rename data2 cigsperday +replace cigsperday = 0 if cigsperday==0 +replace cigsperday = 2 if cigsperday<10 +replace cigsperday = 3 if cigsperday<20 & cigsperday>9 +replace cigsperday = 4 if cigsperday>19 & cigsperday!=. +label values cigsperday clinstatuslab + +foreach file in ad clin { + replace `file'status=2 if inlist(`file'status,234,2,3,4) & cigsperday==2 + replace `file'status=3 if inlist(`file'status,234,2,3,4) & cigsperday==3 + replace `file'status=4 if inlist(`file'status,234,2,3,4) & cigsperday==4 + gen `file's234 = 1 if `file'status==234 + replace `file'status=3 if `file'status==234 /*assume moderate smoker if no info*/ + *replace status=2 if status==1234 + } + +*prioritise additional clinical details status unless missing or information about amount smoked more complete in additional clinical details file +tab clinstatus adstatus, m +gen status = clinstatus +replace status = adstatus if clinstatus == . +label values status clinstatuslab +gen s234 = 1 if clins234 == 1 +replace s234 = 1 if clinstatus == . + +*duplicates +duplicates drop patid eventdate status, force +duplicates tag patid eventdate, gen(dup) +sort patid eventdate +by patid eventdate: egen counts234 = count(s234) +by patid eventdate: egen minstatus = min(status) +by patid eventdate: egen maxstatus = max(status) +drop if s234 == 1 & counts234 <= dup & dup> 0 & maxstatus > 2 & minstatus > 1 /*keep specified amount if imputed amount on same day*/ +drop dup +duplicates tag patid eventdate, gen(dup) +drop if dup > 0 /*drop all remaining records with at least two different smoking status records on the same day*/ +assert status !=. + +keep patid eventdate status + +tempfile smokrecs +save `smokrecs' , replace + +restore +merge 1:m patid using `smokrecs' , keep(match master) + +rename eventdate smoke_update +drop if (smoke_update<`begin'-365.25*`runin' | smoke_update>`end') & _merge==3 +gsort patid -smoke_update +duplicates drop patid if smoke_update<`begin', force + +sort patid smoke_update +by patid: drop if status[_n-1]==status & _n>1 + +expand 2 if smoke_update>`begin' & smoke_update!=. & patid[_n]!=patid[_n-1] +sort patid smoke_update +by patid: replace status=. if _n==1 & smoke_update>`begin' +by patid: replace smoke_update=`begin' if _n==1 & smoke_update>`begin' + +rename status `smokingstatusvar' +drop _merge +if "`wide'"=="wide" { +by patid: gen smok_rec_num=_n + +reshape wide `smokingstatusvar' smoke_update, i(patid) j(smok_rec_num) +} + +end + diff --git a/pr_getsmokingupdates_qrisk2.do b/pr_getsmokingupdates_qrisk2.do new file mode 100644 index 0000000..17bae82 --- /dev/null +++ b/pr_getsmokingupdates_qrisk2.do @@ -0,0 +1,55 @@ +cap prog drop pr_getsmokingupdates + +program define pr_getsmokingupdates +syntax , clinicalfile(string) additionalfile(string) smokingstatusvar(string) begin(string) end(string) runin(integer) [wide] + +preserve + +use "$QRiskCodelistdir\cr_codelist_qof_cod", clear +keep if variable== "smoke_cat" +*NOTE: EH AMENDED 21/09/2018 - MEDCODE 54, Read code 137..00 "Tobacco consumption" is being removed from the code list as it does not indicate smoking status. +drop if medcode==54 +merge 1:m medcode using `clinicalfile' , keep(match) nogen + +replace adid = -_n if adid==0 +rename value status +merge 1:1 patid adid using `additionalfile', keep(match master) +rename data2 cigsperday +replace status=2 if status==2 & cigsperday<10 +replace status=3 if status==2 & cigsperday<20 & cigsperday>9 +replace status=4 if status==2 & cigsperday>19 & cigsperday!=. + +replace status=3 if status==234 +replace status=2 if status==1234 + +keep patid eventdate status + +tempfile smokrecs +save `smokrecs' , replace + +restore +merge 1:m patid using `smokrecs' , keep(match master) + +rename eventdate smoke_update +drop if (smoke_update<`begin'-365.25*`runin' | smoke_update>`end') & _merge==3 +gsort patid -smoke_update +duplicates drop patid if smoke_update<`begin', force + +sort patid smoke_update +by patid: drop if status[_n-1]==status & _n>1 + +expand 2 if smoke_update>`begin' & smoke_update!=. & patid[_n]!=patid[_n-1] +sort patid smoke_update +by patid: replace status=. if _n==1 & smoke_update>`begin' +by patid: replace smoke_update=`begin' if _n==1 & smoke_update>`begin' + +rename status `smokingstatusvar' +drop _merge +if "`wide'"=="wide" { +by patid: gen smok_rec_num=_n + +reshape wide `smokingstatusvar' smoke_update, i(patid) j(smok_rec_num) +} + +end +