DM 'LOG; CLEAR; ;OUT; CLEAR; '; %LET program = M:\p1074-renateh\2023_DayInLifeGP\Work\Scripts\01_Prevalences_2024Apr03.sas; FOOTNOTE "&program on &sysdate"; ***************************************************************************************************; * For: Norway * Paper: Day in the Life of a Norway GP * Programmer: Renate Houts * File: M:\p1074-renateh\2023_DayInLifeGP\Work\Scripts\01_Prevalences_2024Apr03.sas * Modification Hx: 27-Oct-2022 Prepare Data for Mental Health prevalence * 06-Apr-2023 Add education, location in 2006 * 08-Jun-2023 Re-do to select full sample of medical records, * To save space, remove education & location from demograhics * 22-Sep-2023 Prepare prevalence files * 03-Apr-2024 Check/update documentation/comments * ***************************************************************************************************; libname mhph 'N:\durable\Data22\processed_data\ForRenate'; proc format; value SEX 1 = "Male" 2 = "Female"; value NOYES 0 = "No" 1 = "Yes"; value MHDX -1 = "MHDX: P-code with no number" 1 = "Acute stress reaction" 2 = "ADHD" 3 = "Anxiety" 4 = "Dementia/Memory problems" 5 = "Depression" 6 = "Developmental delay/Learning problems" 7 = "Eating disorder" 8 = "Phobia/Compulsive disorder" 9 = "Psychosis" 10 = "PTSD" 11 = "Sexual concern" 12 = "Sleep disturbance" 13 = "Somatization" 14 = "Substance abuse" 15 = "Suicide/Suicide attempt" 16 = "Child/Adolescent behavior symptom/complaint" 17 = "Continence issues" 18 = "Personality disorder" 19 = "Neuresthenia/surmenage (chronic fatigue)" 20 = "Phase of life problem adult" 21 = "Stammering/stuttering/tic" 22 = "Fear of mental disorder" 23 = "Feeling/behaving irritable/angry" 24 = "Other psychological symptom/disease"; value PHCAT -1 = "PHCAT: Non-category code" 1 = "Circulatory System" 2 = "Endocrine System" 3 = "Pulmonary System and Allergy" 4 = "Gastrointestinal System" 5 = "Urogenital System" 6 = "Musculoskeletal System" 7 = "Hematological System" 8 = "Cancers" 9 = "Neurological System"; value CHAPTER 1 = "General & Unspecified" 2 = "Blood, Blood Forming Organs & Immune Mechanism" 3 = "Digestive" 4 = "Eye" 5 = "Ear" 6 = "Cardiovascular" 7 = "Musculoskeletal" 8 = "Neurological" 9 = "Psychological" 10 = "Respiratory" 11 = "Skin" 12 = "Endocrine/Metabolic & Nutritional" 13 = "Urological" 14 = "Pregnancy, Childbearing, Family planning" 15 = "Female genital" 16 = "Male genital" 17 = "Social Problems"; run; * Read in medical records and bring down to ages 0-100; * Delete "A97", "A98" and "Z" codes; data medrec; set MHPH.MedRec_21Sept2023; if age_dx >= 0 & age_dx < 101; run; * Delete codes for (A97, A98 and Z); proc freq data = medrec; table DIAG; where DIAG in ("A97", "A98", "Z"); run; data medrec; set medrec; if DIAG in ("A97", "A98", "Z") then delete; run; * Code diagnoses into categories we're using; data medrec1; set medrec; * Code mental health into diagnoses; if DIAG in ("P02") then MHdx = 1; else if DIAG in ("P81") then MHdx = 2; else if DIAG in ("P01", "P74") then MHdx = 3; else if DIAG in ("P05", "P20", "P70") then MHdx = 4; else if DIAG in ("P03", "P76") then MHdx = 5; else if DIAG in ("P24", "P28", "P85") then MHdx = 6; else if DIAG in ("P11", "P86") then MHdx = 7; else if DIAG in ("P79") then MHdx = 8; else if DIAG in ("P71", "P72", "P73", "P98") then MHdx = 9; else if DIAG in ("P82") then MHdx = 10; else if DIAG in ("P07", "P08", "P09") then MHdx = 11; else if DIAG in ("P06") then MHdx = 12; else if DIAG in ("P75") then MHdx = 13; else if DIAG in ("P15", "P16", "P17", "P18", "P19") then MHdx = 14; else if DIAG in ("P77") then MHdx = 15; else if DIAG in ("P22", "P23") then MHdx = 16; else if DIAG in ("P12", "P13") then MHdx = 17; else if DIAG in ("P80") then MHdx = 18; else if DIAG in ("P78") then MHdx = 19; else if DIAG in ("P25") then MHdx = 20; else if DIAG in ("P10") then MHdx = 21; else if DIAG in ("P27") then MHdx = 22; else if DIAG in ("P04") then MHdx = 23; else if DIAG in ("P29", "P99") then MHdx = 24; * Chapter P codes that are diagnoses or substance abuse (no acute alcohol abuse); if DIAG in ("P15", "P17", "P18", "P19", "P70", "P71", "P72", "P73", "P74", "P75", "P76", "P77", "P78", "P79", "P80", "P81", "P82", "P85", "P86", "P98", "P99") then Pdx = 1; * Code 9 physical health categories; if DIAG in ("K86", "K87", "T93", "K74", "K75", "K76", "K78", "K77", "K92", "K90", "K91", "K83", "K82", "K80") then PHcat = 1; *14; else if DIAG in ("T89", "T90", "T80", "T86", "T81", "T85", "T92") then PHcat = 2; * 7; else if DIAG in ("R78", "R79", "R95", "R96", "R97", "S88", "A92") then PHcat = 3; * 7; else if DIAG in ("D86", "D85", "D87", "D72", "D97", "D94", "D92") then PHcat = 4; * 7; else if DIAG in ("U88", "U70", "Y85") then PHcat = 5; * 3; else if DIAG in ("L88", "L95") then PHcat = 6; * 2; else if DIAG in ("B90", "B80", "B81", "B82", "B78", "B83") then PHcat = 7; * 6; else if DIAG in ("D77", "R85", "D74", "D75", "D76", "B74", "H75", "R84", "A79", "K72", "L71", "S77", "N74", "X76", "Y78", "X77", "X75", "W72", "Y77", "U75", "U77", "U76", "F74", "T71", "T73", "B72", "B73") then PHcat = 8; *27; else if DIAG in ("N89", "N88", "N87", "N86", "N92", "N91", "N94", "L86", "N93", "N18") then PHcat = 9; *10; * Code infection, pain, injuries; if DIAG in ("A70", "A71", "A72", "A73", "A74", "A75", "A76", "A77", "A78", "B70", "B71", "D70", "D71", "D72", "D73", "F70", "F71", "F72", "F73", "H70", "H71", "H72", "H73", "H74", "K70", "K71", "L70", "N70", "N71", "N72", "N73", "R71", "R72", "R73", "R74", "R75", "R76", "R77", "R78", "R79", "R80", "R81", "R82", "R83", "S03", "S09", "S10", "S11", "S70", "S71", "S72", "S73", "S74", "S75", "S76", "S84", "S95", "T70", "U70", "U71", "U72", "W70", "W71", "X70", "X71", "X72", "X73", "X74", "X90", "X91", "X92", "Y70", "Y71", "Y72", "Y73", "Y74", "Y75", "Y76") then infect = 1; if DIAG in ("A01", "A11", "B02", "D01", "D02", "D04", "D06", "F01", "H01", "K01", "K03", "L18", "L86", "N03", "R01", "S01", "U01", "X01", "X02", "X03", "X04", "X18", "Y01", "Y02") then pain = 1; if codetype = "inf" then infect1 = 1; if codetype = "inj" then injury = 1; * Combine male/female genital; if diag_chapter = "Y" then diag_chapter = "X"; format MHdx MHDX. PHcat PHCAT.; run; * Bring analysis sample down to ages 0-100 on 01-Jan-2006; data demo; set MHPH.Demog_21Sept2023; if age_end >= 0 & age_start < 101; run; proc contents data = medrec1; run; proc freq data = demo; table DOB; run; * Males vs females in sample; proc freq data = demo; table sex; run; * Number of encounters/visit and visits/person ("Methods & Procedures: Ascertainment of primary care encounters"); proc freq data = medrec1 noprint; table w19_1011_lnr_k2_*DATO / out = code_per_visit; run; proc means data = code_per_visit; var COUNT; run; proc freq data = code_per_visit; table COUNT; run; proc freq data = code_per_visit noprint; table w19_1011_lnr_k2_ / out = visit_per_person; run; proc means data = visit_per_person n min p25 median p75 max mean std; var COUNT; run; proc freq data = visit_per_person; table COUNT; run; proc freq data = medrec1; table diag_chapter PHcat MHdx Pdx; run; **************************************************************************************************; * Encounter code-level analysis (everyone and by sex); **************************************************************************************************; * Chapter codes; proc freq data = medrec1 noprint; table diag_chapter / out = chapter_a; run; proc freq data = medrec1 noprint; table diag_chapter / out = chapter_m; where sex = 1; run; proc freq data = medrec1 noprint; table diag_chapter / out = chapter_f; where sex = 2; run; data chapter_a; set chapter_a; all_n = COUNT; all_p = PERCENT/100; drop COUNT PERCENT; run; data chapter_m; set chapter_m; male_n = COUNT; male_p = PERCENT/100; drop COUNT PERCENT; run; data chapter_f; set chapter_f; female_n = COUNT; female_p = PERCENT/100; drop COUNT PERCENT; run; data chapter; merge chapter_a chapter_f chapter_m; by diag_chapter; code = VVALUE(diag_chapter); drop diag_chapter; run; * Set number of encounter codes for all, men, women; %LET nenc = 354516291; %LET menc = 146432336; %LET fenc = 208083955; * Physical Health categories; proc freq data = medrec1 noprint; table PHcat / out = PHcat_a; run; proc freq data = medrec1 noprint; table PHcat / out = PHcat_m; where sex = 1; run; proc freq data = medrec1 noprint; table PHcat / out = PHcat_f; where sex = 2; run; data PHcat_a; set PHcat_a; all_n = COUNT; all_p = all_n / &nenc; drop COUNT PERCENT; run; data PHcat_m; set PHcat_m; male_n = COUNT; male_p = male_n / &menc; drop COUNT PERCENT; run; data PHcat_f; set PHcat_f; female_n = COUNT; female_p = female_n / &fenc; drop COUNT PERCENT; run; data PHcat; merge PHcat_a PHcat_f PHcat_m; by Phcat; if PHCat = . then PHCat = -1; code = VVALUE(PHcat); drop PHcat; run; * Mental Health categories; proc freq data = medrec1 noprint; table MHdx / list missing out = mhdx_a; where diag_chapter = "P"; run; proc freq data = medrec1 noprint; table MHdx / list missing out = mhdx_m; where diag_chapter = "P" and sex = 1; run; proc freq data = medrec1 noprint; table MHdx / list missing out = mhdx_f; where diag_chapter = "P" and sex = 2; run; data mhdx_a; set mhdx_a; all_n = COUNT; all_p = PERCENT/100; drop COUNT PERCENT; run; data mhdx_m; set mhdx_m; male_n = COUNT; male_p = PERCENT/100; drop COUNT PERCENT; run; data mhdx_f; set mhdx_f; female_n = COUNT; female_p = PERCENT/100; drop COUNT PERCENT; run; data mhdx; merge mhdx_a mhdx_f mhdx_m; by mhdx; if mhdx = . then mhdx = -1; code = VVALUE(mhdx); drop mhdx; run; * Mental Health categories & diagnoses; proc freq data = medrec1 noprint; table DIAG / list missing out = mhdiag_a; where diag_chapter = "P"; run; proc freq data = medrec1 noprint; table DIAG / list missing out = mhdiag_m; where diag_chapter = "P" and sex = 1; run; proc freq data = medrec1 noprint; table DIAG / list missing out = mhdiag_f; where diag_chapter = "P" and sex = 2; run; data mhdiag_a; set mhdiag_a; all_n = COUNT; all_p = PERCENT/100; drop COUNT PERCENT; run; data mhdiag_m; set mhdiag_m; male_n = COUNT; male_p = PERCENT/100; drop COUNT PERCENT; run; data mhdiag_f; set mhdiag_f; female_n = COUNT; female_p = PERCENT/100; drop COUNT PERCENT; run; data mhdiag; merge mhdiag_a mhdiag_f mhdiag_m; by DIAG; if DIAG = "" then code = "P-codes: Non-p code"; if DIAG = "P" then code = "P-codes: No number"; if code = "" then code = DIAG; drop DIAG; run; * P diagnosis codes; proc freq data = medrec1 noprint; table Pdx / missing out = Pdx_a; run; proc freq data = medrec1 noprint; table Pdx / missing out = Pdx_m; where sex = 1; run; proc freq data = medrec1 noprint; table Pdx / missing out = Pdx_f; where sex = 2; run; data Pdx_a; set Pdx_a; all_n = COUNT; all_p = all_n / &nenc; drop COUNT PERCENT; run; data Pdx_m; set Pdx_m; male_n = COUNT; male_p = male_n / &menc; drop COUNT PERCENT; run; data Pdx_f; set Pdx_f; female_n = COUNT; female_p = female_n / &fenc; drop COUNT PERCENT; run; data Pdx; merge Pdx_a Pdx_f Pdx_m; by Pdx; if Pdx = 1; code = "P-dx"; drop Pdx; run; * Infections; proc freq data = medrec1 noprint; table infect / missing out = infect_a; proc freq data = medrec1 noprint; table infect / missing out = infect_m; where sex = 1; run; proc freq data = medrec1 noprint; table infect / missing out = infect_f; where sex = 2; run; data infect_a; set infect_a; all_n = COUNT; all_p = all_n / &nenc; drop COUNT PERCENT; run; data infect_m; set infect_m; male_n = COUNT; male_p = male_n / &menc; drop COUNT PERCENT; run; data infect_f; set infect_f; female_n = COUNT; female_p = female_n / &fenc; drop COUNT PERCENT; run; data infect; merge infect_a infect_f infect_m; by infect; if infect = 1; code = "Infection"; drop infect; run; * Pain; proc freq data = medrec1 noprint; table pain / missing out = pain_a; proc freq data = medrec1 noprint; table pain / missing out = pain_m; where sex = 1; run; proc freq data = medrec1 noprint; table pain / missing out = pain_f; where sex = 2; run; data pain_a; set pain_a; all_n = COUNT; all_p = all_n / &nenc; drop COUNT PERCENT; run; data pain_m; set pain_m; male_n = COUNT; male_p = male_n / &menc; drop COUNT PERCENT; run; data pain_f; set pain_f; female_n = COUNT; female_p = female_n / &fenc; drop COUNT PERCENT; run; data pain; merge pain_a pain_f pain_m; by pain; if pain = 1; code = "Pain"; drop pain; run; * Injuries; proc freq data = medrec1 noprint; table injury / missing out = injury_a; proc freq data = medrec1 noprint; table injury / missing out = injury_m; where sex = 1; run; proc freq data = medrec1 noprint; table injury / missing out = injury_f; where sex = 2; run; data injury_a; set injury_a; all_n = COUNT; all_p = all_n / &nenc; drop COUNT PERCENT; run; data injury_m; set injury_m; male_n = COUNT; male_p = male_n / &menc; drop COUNT PERCENT; run; data injury_f; set injury_f; female_n = COUNT; female_p = female_n / &fenc; drop COUNT PERCENT; run; data injury; merge injury_a injury_f injury_m; by injury; if injury = 1; code = "Injury"; drop injury; run; * Combine; data encounters; set chapter phcat mhdx mhdiag Pdx infect pain injury; run; proc datasets; delete chapter_a chapter_m chapter_f phcat_a phcat_m phcat_f mhdx_a mhdx_m mhdx_f mhdiag_a mhdiag_f mhdiag_m pdx_a pdx_m pdx_f infect_a infect_m infect_f pain_a pain_m pain_f injury_a injury_m injury_f chapter phcat mhdx mhdiag pdx infect pain injury; run; quit; **************************************************************************************************; * Person-level analysis (everyone and by sex); **************************************************************************************************; * Chapter codes; proc freq data = medrec1 noprint; table w19_1011_lnr_k2_*diag_chapter / out = chapter_long; run; proc sort data = chapter_long; by w19_1011_lnr_k2_; run; data chapter_wide; array n [15] A_n B_n D_n F_n H_n K_n L_n N_n P_n R_n S_n T_n U_n W_n X_n; do i = 1 to 15 until (last.w19_1011_lnr_k2_); set chapter_long; by w19_1011_lnr_k2_; if diag_chapter = "A" then A_n = COUNT; if diag_chapter = "B" then B_n = COUNT; if diag_chapter = "D" then D_n = COUNT; if diag_chapter = "F" then F_n = COUNT; if diag_chapter = "H" then H_n = COUNT; if diag_chapter = "K" then K_n = COUNT; if diag_chapter = "L" then L_n = COUNT; if diag_chapter = "N" then N_n = COUNT; if diag_chapter = "P" then P_n = COUNT; if diag_chapter = "R" then R_n = COUNT; if diag_chapter = "S" then S_n = COUNT; if diag_chapter = "T" then T_n = COUNT; if diag_chapter = "U" then U_n = COUNT; if diag_chapter = "W" then W_n = COUNT; if diag_chapter = "X" then X_n = COUNT; end; drop i diag_chapter COUNT PERCENT; run; data chapter_wide; merge chapter_wide demo (in = indem); by w19_1011_lnr_k2_; indemo = indem; array n [15] A_n B_n D_n F_n H_n K_n L_n N_n P_n R_n S_n T_n U_n W_n X_n; array a [15] A_a B_a D_a F_a H_a K_a L_a N_a P_a R_a S_a T_a U_a W_a X_a; do i = 1 to 15; if n[i] > 0 then a[i] = 1; else a[i] = 0; end; * Delete n = 2 whose age_dx > age_end (age_end disqualifies them, age_dx doesn't); if DOB = . then delete; drop i; run; ods output OneWayFreqs = chapter_a; proc freq data = chapter_wide; table A_a B_a D_a F_a H_a K_a L_a N_a P_a R_a S_a T_a U_a W_a X_a; run; ods output close; data chapter_a; set chapter_a; if MOD(_N_,2) = 0 then value = 1; else value = 0; if value = 1; code = SUBSTR(Table, 7, 1); all_n = frequency; all_p = percent/100; keep code all_n all_p; run; ods output OneWayFreqs = chapter_m; proc freq data = chapter_wide; table A_a B_a D_a F_a H_a K_a L_a N_a P_a R_a S_a T_a U_a W_a X_a; where sex = 1; run; ods output close; data chapter_m; set chapter_m; if MOD(_N_,2) = 0 then value = 1; else value = 0; if value = 1; code = SUBSTR(Table, 7, 1); male_n = frequency; male_p = percent/100; keep code male_n male_p; run; ods output OneWayFreqs = chapter_f; proc freq data = chapter_wide; table A_a B_a D_a F_a H_a K_a L_a N_a P_a R_a S_a T_a U_a W_a X_a; where sex = 2; run; ods output close; data chapter_f; set chapter_f; if MOD(_N_,2) = 0 then value = 1; else value = 0; if value = 1; code = SUBSTR(Table, 7, 1); female_n = frequency; female_p = percent/100; keep code female_n female_p; run; data chapter; merge chapter_a chapter_f chapter_m; by code; run; * Physical Health categories; proc freq data = medrec1 noprint; table w19_1011_lnr_k2_*PHcat / out = category_long; run; proc sort data = category_long; by w19_1011_lnr_k2_; run; data category_wide; array n [9] circ_n endo_n pulm_n gast_n urog_n musc_n hema_n canc_n neur_n; do i = 1 to 9 until (last.w19_1011_lnr_k2_); set category_long; by w19_1011_lnr_k2_; if PHcat = 1 then circ_n = COUNT; if PHcat = 2 then endo_n = COUNT; if PHcat = 3 then pulm_n = COUNT; if PHcat = 4 then gast_n = COUNT; if PHcat = 5 then urog_n = COUNT; if PHcat = 6 then musc_n = COUNT; if PHcat = 7 then hema_n = COUNT; if PHcat = 8 then canc_n = COUNT; if PHcat = 9 then neur_n = COUNT; end; drop i PHcat COUNT PERCENT; run; data category_wide; merge category_wide demo (in = indem); by w19_1011_lnr_k2_; indemo = indem; array n [9] circ_n endo_n pulm_n gast_n urog_n musc_n hema_n canc_n neur_n; array a [9] circ_a endo_a pulm_a gast_a urog_a musc_a hema_a canc_a neur_a; do i = 1 to 9; if n[i] > 0 then a[i] = 1; else a[i] = 0; end; drop i; run; ods output OneWayFreqs = category_a; proc freq data = category_wide; table circ_a endo_a pulm_a gast_a urog_a musc_a hema_a canc_a neur_a; run; ods output close; data category_a; set category_a; if MOD(_N_,2) = 0 then value = 1; else value = 0; if value = 1; code = SUBSTR(Table, 7); if code = "circ_a" then code = "Circulatory System"; else if code = "endo_a" then code = "Endocrine System"; else if code = "pulm_a" then code = "Pulmonary System and Allergy"; else if code = "gast_a" then code = "Gastrointestinal System"; else if code = "urog_a" then code = "Urogenital System"; else if code = "musc_a" then code = "Musculoskeletal System"; else if code = "hema_a" then code = "Hematological System"; else if code = "canc_a" then code = "Cancers"; else if code = "neur_a" then code = "Neurological System"; all_n = frequency; all_p = percent/100; keep code all_n all_p; run; ods output OneWayFreqs = category_m; proc freq data = category_wide; table circ_a endo_a pulm_a gast_a urog_a musc_a hema_a canc_a neur_a; where sex = 1; run; ods output close; data category_m; set category_m; if MOD(_N_,2) = 0 then value = 1; else value = 0; if value = 1; code = SUBSTR(Table, 7); if code = "circ_a" then code = "Circulatory System"; else if code = "endo_a" then code = "Endocrine System"; else if code = "pulm_a" then code = "Pulmonary System and Allergy"; else if code = "gast_a" then code = "Gastrointestinal System"; else if code = "urog_a" then code = "Urogenital System"; else if code = "musc_a" then code = "Musculoskeletal System"; else if code = "hema_a" then code = "Hematological System"; else if code = "canc_a" then code = "Cancers"; else if code = "neur_a" then code = "Neurological System"; male_n = frequency; male_p = percent/100; keep code male_n male_p; run; ods output OneWayFreqs = category_f; proc freq data = category_wide; table circ_a endo_a pulm_a gast_a urog_a musc_a hema_a canc_a neur_a; where sex = 2; run; ods output close; data category_f; set category_f; if MOD(_N_,2) = 0 then value = 1; else value = 0; if value = 1; code = SUBSTR(Table, 7); if code = "circ_a" then code = "Circulatory System"; else if code = "endo_a" then code = "Endocrine System"; else if code = "pulm_a" then code = "Pulmonary System and Allergy"; else if code = "gast_a" then code = "Gastrointestinal System"; else if code = "urog_a" then code = "Urogenital System"; else if code = "musc_a" then code = "Musculoskeletal System"; else if code = "hema_a" then code = "Hematological System"; else if code = "canc_a" then code = "Cancers"; else if code = "neur_a" then code = "Neurological System"; female_n = frequency; female_p = percent/100; keep code female_n female_p; run; proc sort data = category_a; by code; proc sort data = category_f; by code; proc sort data = category_m; by code; run; data category; merge category_a category_f category_m; by code; run; * Mental Health categories; proc freq data = medrec1 noprint; table w19_1011_lnr_k2_*MHdx / out = MHdx_long; where diag_chapter = "P"; run; proc sort data = MHdx_long; by w19_1011_lnr_k2_; run; data MHdx_wide; array n [24] str_n adhd_n anx_n dem_n dep_n dev_n eat_n phb_n psy_n ptsd_n sex_n slp_n som_n sub_n sui_n cha_n con_n per_n chf_n adl_n stu_n fea_n irr_n oth_n; do i = 1 to 24 until (last.w19_1011_lnr_k2_); set MHdx_long; by w19_1011_lnr_k2_; if MHdx = 1 then str_n = COUNT; if MHdx = 2 then adhd_n = COUNT; if MHdx = 3 then anx_n = COUNT; if MHdx = 4 then dem_n = COUNT; if MHdx = 5 then dep_n = COUNT; if MHdx = 6 then dev_n = COUNT; if MHdx = 7 then eat_n = COUNT; if MHdx = 8 then phb_n = COUNT; if MHdx = 9 then psy_n = COUNT; if MHdx = 10 then ptsd_n = COUNT; if MHdx = 11 then sex_n = COUNT; if MHdx = 12 then slp_n = COUNT; if MHdx = 13 then som_n = COUNT; if MHdx = 14 then sub_n = COUNT; if MHdx = 15 then sui_n = COUNT; if MHdx = 16 then cha_n = COUNT; if MHdx = 17 then con_n = COUNT; if MHdx = 18 then per_n = COUNT; if MHdx = 19 then chf_n = COUNT; if MHdx = 20 then adl_n = COUNT; if MHdx = 21 then stu_n = COUNT; if MHdx = 22 then fea_n = COUNT; if MHdx = 23 then irr_n = COUNT; if MHdx = 24 then oth_n = COUNT; end; drop i MHdx COUNT PERCENT; run; data MHdx_wide; merge MHdx_wide demo (in = indem); by w19_1011_lnr_k2_; indemo = indem; array n [24] str_n adhd_n anx_n dem_n dep_n dev_n eat_n phb_n psy_n ptsd_n sex_n slp_n som_n sub_n sui_n cha_n con_n per_n chf_n adl_n stu_n fea_n irr_n oth_n; array a [24] str_a adhd_a anx_a dem_a dep_a dev_a eat_a phb_a psy_a ptsd_a sex_a slp_a som_a sub_a sui_a cha_a con_a per_a chf_a adl_a stu_a fea_a irr_a oth_a; do i = 1 to 24; if n[i] > 0 then a[i] = 1; else a[i] = 0; end; * Delete n = 2 whose age_dx > age_end (age_end disqualifies them, age_dx doesn't); if DOB = . then delete; drop i; run; ods output OneWayFreqs = MHdx_a; proc freq data = MHdx_wide; table str_a adhd_a anx_a dem_a dep_a dev_a eat_a phb_a psy_a ptsd_a sex_a slp_a som_a sub_a sui_a cha_a con_a per_a chf_a adl_a stu_a fea_a irr_a oth_a; run; ods output close; data MHdx_a; set MHdx_a; if MOD(_N_,2) = 0 then value = 1; else value = 0; if value = 1; code = SUBSTR(Table, 7); if code = "str_a" then code = "Acute stress reaction"; else if code = "adhd_a" then code = "ADHD"; else if code = "anx_a" then code = "Anxiety"; else if code = "dem_a" then code = "Dementia/Memory problems"; else if code = "dep_a" then code = "Depression"; else if code = "dev_a" then code = "Developmental delay/Learning problems"; else if code = "eat_a" then code = "Eating disorder"; else if code = "phb_a" then code = "Phobia/Compulsive disorder"; else if code = "psy_a" then code = "Psychosis"; else if code = "ptsd_a" then code = "PTSD"; else if code = "sex_a" then code = "Sexual concern"; else if code = "slp_a" then code = "Sleep disturbance"; else if code = "som_a" then code = "Somatization"; else if code = "sub_a" then code = "Substance abuse"; else if code = "sui_a" then code = "Suicide/Suicide attempt"; else if code = "cha_a" then code = "Child/Adolescent behavior symptom/complaint"; else if code = "con_a" then code = "Continence issues"; else if code = "per_a" then code = "Personality disorder"; else if code = "chf_a" then code = "Neuresthenia/surmenage (chronic fatigue)"; else if code = "adl_a" then code = "Phase of life problem adult"; else if code = "stu_a" then code = "Stammering/stuttering/tic"; else if code = "fea_a" then code = "Fear of mental disorder"; else if code = "irr_a" then code = "Feeling/behaving irritable/angry"; else if code = "oth_a" then code = "Other psychological symptom/disease"; all_n = frequency; all_p = percent/100; keep code all_n all_p; run; ods output OneWayFreqs = MHdx_m; proc freq data = MHdx_wide; table str_a adhd_a anx_a dem_a dep_a dev_a eat_a phb_a psy_a ptsd_a sex_a slp_a som_a sub_a sui_a cha_a con_a per_a chf_a adl_a stu_a fea_a irr_a oth_a; where sex = 1; run; ods output close; data MHdx_m; set MHdx_m; if MOD(_N_,2) = 0 then value = 1; else value = 0; if value = 1; code = SUBSTR(Table, 7); if code = "str_a" then code = "Acute stress reaction"; else if code = "adhd_a" then code = "ADHD"; else if code = "anx_a" then code = "Anxiety"; else if code = "dem_a" then code = "Dementia/Memory problems"; else if code = "dep_a" then code = "Depression"; else if code = "dev_a" then code = "Developmental delay/Learning problems"; else if code = "eat_a" then code = "Eating disorder"; else if code = "phb_a" then code = "Phobia/Compulsive disorder"; else if code = "psy_a" then code = "Psychosis"; else if code = "ptsd_a" then code = "PTSD"; else if code = "sex_a" then code = "Sexual concern"; else if code = "slp_a" then code = "Sleep disturbance"; else if code = "som_a" then code = "Somatization"; else if code = "sub_a" then code = "Substance abuse"; else if code = "sui_a" then code = "Suicide/Suicide attempt"; else if code = "cha_a" then code = "Child/Adolescent behavior symptom/complaint"; else if code = "con_a" then code = "Continence issues"; else if code = "per_a" then code = "Personality disorder"; else if code = "chf_a" then code = "Neuresthenia/surmenage (chronic fatigue)"; else if code = "adl_a" then code = "Phase of life problem adult"; else if code = "stu_a" then code = "Stammering/stuttering/tic"; else if code = "fea_a" then code = "Fear of mental disorder"; else if code = "irr_a" then code = "Feeling/behaving irritable/angry"; else if code = "oth_a" then code = "Other psychological symptom/disease"; male_n = frequency; male_p = percent/100; keep code male_n male_p; run; ods output OneWayFreqs = MHdx_f; proc freq data = MHdx_wide; table str_a adhd_a anx_a dem_a dep_a dev_a eat_a phb_a psy_a ptsd_a sex_a slp_a som_a sub_a sui_a cha_a con_a per_a chf_a adl_a stu_a fea_a irr_a oth_a; where sex = 2; run; ods output close; data MHdx_f; set MHdx_f; if MOD(_N_,2) = 0 then value = 1; else value = 0; if value = 1; code = SUBSTR(Table, 7); if code = "str_a" then code = "Acute stress reaction"; else if code = "adhd_a" then code = "ADHD"; else if code = "anx_a" then code = "Anxiety"; else if code = "dem_a" then code = "Dementia/Memory problems"; else if code = "dep_a" then code = "Depression"; else if code = "dev_a" then code = "Developmental delay/Learning problems"; else if code = "eat_a" then code = "Eating disorder"; else if code = "phb_a" then code = "Phobia/Compulsive disorder"; else if code = "psy_a" then code = "Psychosis"; else if code = "ptsd_a" then code = "PTSD"; else if code = "sex_a" then code = "Sexual concern"; else if code = "slp_a" then code = "Sleep disturbance"; else if code = "som_a" then code = "Somatization"; else if code = "sub_a" then code = "Substance abuse"; else if code = "sui_a" then code = "Suicide/Suicide attempt"; else if code = "cha_a" then code = "Child/Adolescent behavior symptom/complaint"; else if code = "con_a" then code = "Continence issues"; else if code = "per_a" then code = "Personality disorder"; else if code = "chf_a" then code = "Neuresthenia/surmenage (chronic fatigue)"; else if code = "adl_a" then code = "Phase of life problem adult"; else if code = "stu_a" then code = "Stammering/stuttering/tic"; else if code = "fea_a" then code = "Fear of mental disorder"; else if code = "irr_a" then code = "Feeling/behaving irritable/angry"; else if code = "oth_a" then code = "Other psychological symptom/disease"; female_n = frequency; female_p = percent/100; keep code female_n female_p; run; proc sort data = MHdx_a; by code; proc sort data = MHdx_f; by code; proc sort data = MHdx_m; by code; run; data MHdx; merge MHdx_a MHdx_f MHdx_m; by code; run; * Mental Health Codes; proc freq data = medrec1 noprint; table w19_1011_lnr_k2_*DIAG / out = MH_DIAG_long; where diag_chapter = "P"; run; proc sort data = MH_DIAG_long; by w19_1011_lnr_k2_; run; data MH_DIAG_wide; array n [44] P02_n P81_n P01_n P74_n P05_n P20_n P70_n P03_n P76_n P24_n P28_n P85_n P11_n P86_n P79_n P71_n P72_n P73_n P98_n P82_n P07_n P08_n P09_n P06_n P75_n P15_n P16_n P17_n P18_n P19_n P77_n P04_n P10_n P12_n P13_n P22_n P23_n P25_n P27_n P29_n P78_n P80_n P99_n P_n; do i = 1 to 44 until (last.w19_1011_lnr_k2_); set MH_DIAG_long; by w19_1011_lnr_k2_; if DIAG = "P02" then P02_n = COUNT; if DIAG = "P81" then P81_n = COUNT; if DIAG = "P01" then P01_n = COUNT; if DIAG = "P74" then P74_n = COUNT; if DIAG = "P05" then P05_n = COUNT; if DIAG = "P20" then P20_n = COUNT; if DIAG = "P70" then P70_n = COUNT; if DIAG = "P03" then P03_n = COUNT; if DIAG = "P76" then P76_n = COUNT; if DIAG = "P24" then P24_n = COUNT; if DIAG = "P28" then P28_n = COUNT; if DIAG = "P85" then P85_n = COUNT; if DIAG = "P11" then P11_n = COUNT; if DIAG = "P86" then P86_n = COUNT; if DIAG = "P79" then P79_n = COUNT; if DIAG = "P71" then P71_n = COUNT; if DIAG = "P72" then P72_n = COUNT; if DIAG = "P73" then P73_n = COUNT; if DIAG = "P98" then P98_n = COUNT; if DIAG = "P82" then P82_n = COUNT; if DIAG = "P07" then P07_n = COUNT; if DIAG = "P08" then P08_n = COUNT; if DIAG = "P09" then P09_n = COUNT; if DIAG = "P06" then P06_n = COUNT; if DIAG = "P75" then P75_n = COUNT; if DIAG = "P15" then P15_n = COUNT; if DIAG = "P16" then P16_n = COUNT; if DIAG = "P17" then P17_n = COUNT; if DIAG = "P18" then P18_n = COUNT; if DIAG = "P19" then P19_n = COUNT; if DIAG = "P77" then P77_n = COUNT; if DIAG = "P04" then P04_n = COUNT; if DIAG = "P10" then P10_n = COUNT; if DIAG = "P12" then P12_n = COUNT; if DIAG = "P13" then P13_n = COUNT; if DIAG = "P22" then P22_n = COUNT; if DIAG = "P23" then P23_n = COUNT; if DIAG = "P25" then P25_n = COUNT; if DIAG = "P27" then P27_n = COUNT; if DIAG = "P29" then P29_n = COUNT; if DIAG = "P78" then P78_n = COUNT; if DIAG = "P80" then P80_n = COUNT; if DIAG = "P99" then P99_n = COUNT; if DIAG = "P" then P_n = COUNT; end; drop i DIAG COUNT PERCENT; run; data MH_DIAG_wide; merge MH_DIAG_wide demo (in = indem); by w19_1011_lnr_k2_; indemo = indem; array n [44] P02_n P81_n P01_n P74_n P05_n P20_n P70_n P03_n P76_n P24_n P28_n P85_n P11_n P86_n P79_n P71_n P72_n P73_n P98_n P82_n P07_n P08_n P09_n P06_n P75_n P15_n P16_n P17_n P18_n P19_n P77_n P04_n P10_n P12_n P13_n P22_n P23_n P25_n P27_n P29_n P78_n P80_n P99_n P_n; array a [44] P02_a P81_a P01_a P74_a P05_a P20_a P70_a P03_a P76_a P24_a P28_a P85_a P11_a P86_a P79_a P71_a P72_a P73_a P98_a P82_a P07_a P08_a P09_a P06_a P75_a P15_a P16_a P17_a P18_a P19_a P77_a P04_a P10_a P12_a P13_a P22_a P23_a P25_a P27_a P29_a P78_a P80_a P99_a P_a; do i = 1 to 44; if n[i] > 0 then a[i] = 1; else a[i] = 0; end; drop i; run; ods output OneWayFreqs = MH_DIAG_a; proc freq data = MH_DIAG_wide; table P02_a P81_a P01_a P74_a P05_a P20_a P70_a P03_a P76_a P24_a P28_a P85_a P11_a P86_a P79_a P71_a P72_a P73_a P98_a P82_a P07_a P08_a P09_a P06_a P75_a P15_a P16_a P17_a P18_a P19_a P77_a P04_a P10_a P12_a P13_a P22_a P23_a P25_a P27_a P29_a P78_a P80_a P99_a P_a; run; ods output close; data MH_DIAG_a; set MH_DIAG_a; if MOD(_N_,2) = 0 then value = 1; else value = 0; if value = 1; code = SUBSTR(Table, 7, 3); if code = "P_a" then code = "P-codes: No number"; all_n = frequency; all_p = percent/100; keep code all_n all_p; run; ods output OneWayFreqs = MH_DIAG_m; proc freq data = MH_DIAG_wide; table P02_a P81_a P01_a P74_a P05_a P20_a P70_a P03_a P76_a P24_a P28_a P85_a P11_a P86_a P79_a P71_a P72_a P73_a P98_a P82_a P07_a P08_a P09_a P06_a P75_a P15_a P16_a P17_a P18_a P19_a P77_a P04_a P10_a P12_a P13_a P22_a P23_a P25_a P27_a P29_a P78_a P80_a P99_a P_a; where sex = 1; run; ods output close; data MH_DIAG_m; set MH_DIAG_m; if MOD(_N_,2) = 0 then value = 1; else value = 0; if value = 1; code = SUBSTR(Table, 7, 3); if code = "P_a" then code = "P-codes: No number"; male_n = frequency; male_p = percent/100; keep code male_n male_p; run; ods output OneWayFreqs = MH_DIAG_f; proc freq data = MH_DIAG_wide; table P02_a P81_a P01_a P74_a P05_a P20_a P70_a P03_a P76_a P24_a P28_a P85_a P11_a P86_a P79_a P71_a P72_a P73_a P98_a P82_a P07_a P08_a P09_a P06_a P75_a P15_a P16_a P17_a P18_a P19_a P77_a P04_a P10_a P12_a P13_a P22_a P23_a P25_a P27_a P29_a P78_a P80_a P99_a P_a; where sex = 2; run; ods output close; data MH_DIAG_f; set MH_DIAG_f; if MOD(_N_,2) = 0 then value = 1; else value = 0; if value = 1; code = SUBSTR(Table, 7, 3); if code = "P_a" then code = "P-codes: No number"; female_n = frequency; female_p = percent/100; keep code female_n female_p; run; proc sort data = MH_DIAG_a; by code; proc sort data = MH_DIAG_f; by code; proc sort data = MH_DIAG_m; by code; run; data MH_DIAG; merge MH_DIAG_a MH_DIAG_f MH_DIAG_m; by code; run; * P diagnosis codes; proc freq data = medrec1 noprint; table w19_1011_lnr_k2_*Pdx / out = Pdx; run; proc sort data = Pdx; by w19_1011_lnr_k2_; run; data Pdx; set Pdx; if PERCENT ne .; Pdx_a = Pdx; Pdx_n = COUNT; keep w19_1011_lnr_k2_ Pdx_n Pdx_a; run; * Infections; proc freq data = medrec1 noprint; table w19_1011_lnr_k2_*infect / out = infect; run; proc sort data = infect; by w19_1011_lnr_k2_; run; data infect; set infect; if PERCENT ne .; infect_a = infect; infect_n = COUNT; keep w19_1011_lnr_k2_ infect_n infect_a; run; * Pain; proc freq data = medrec1 noprint; table w19_1011_lnr_k2_*pain / out = pain; run; proc sort data = pain; by w19_1011_lnr_k2_; run; data pain; set pain; if PERCENT ne .; pain_a = pain; pain_n = COUNT; keep w19_1011_lnr_k2_ pain_n pain_a; run; * Injuries; proc freq data = medrec1 noprint; table w19_1011_lnr_k2_*injury / out = injury; run; proc sort data = injury; by w19_1011_lnr_k2_; run; data injury; set injury; if PERCENT ne .; injury_a = injury; injury_n = COUNT; keep w19_1011_lnr_k2_ injury_n injury_a; run; * Combine P-dx, infections, pain, injuries; data pinfpaininj; merge demo (in = indem) pdx infect pain injury; by w19_1011_lnr_k2_; indemo = indem; if pdx_a = . then pdx_a = 0; if pdx_n = . then pdx_n = 0; if infect_a = . then infect_a = 0; if infect_n = . then infect_n = 0; if pain_a = . then pain_a = 0; if pain_n = . then pain_n = 0; if injury_a = . then injury_a = 0; if injury_n = . then injury_n = 0; run; ods output OneWayFreqs = pinfpaininj_a; proc freq data = pinfpaininj; table pdx_a infect_a pain_a injury_a; run; ods output close; data pinfpaininj_a; set pinfpaininj_a; if MOD(_N_,2) = 0 then value = 1; else value = 0; if value = 1; if Table = "Table infect_a" then code = "Infection"; else if Table = "Table pain_a" then code = "Pain"; else if Table = "Table Pdx_a" then code = "P-dx"; else if Table = "Table injury_a" then code = "Injury"; all_n = frequency; all_p = percent/100; keep code all_n all_p; run; ods output OneWayFreqs = pinfpaininj_m; proc freq data = pinfpaininj; table pdx_a infect_a pain_a injury_a; where sex = 1; run; ods output close; data pinfpaininj_m; set pinfpaininj_m; if MOD(_N_,2) = 0 then value = 1; else value = 0; if value = 1; if Table = "Table infect_a" then code = "Infection"; else if Table = "Table pain_a" then code = "Pain"; else if Table = "Table Pdx_a" then code = "P-dx"; else if Table = "Table injury_a" then code = "Injury"; male_n = frequency; male_p = percent/100; keep code male_n male_p; run; ods output OneWayFreqs = pinfpaininj_f; proc freq data = pinfpaininj; table pdx_a infect_a pain_a injury_a; where sex = 2; run; ods output close; data pinfpaininj_f; set pinfpaininj_f; if MOD(_N_,2) = 0 then value = 1; else value = 0; if value = 1; if Table = "Table infect_a" then code = "Infection"; else if Table = "Table pain_a" then code = "Pain"; else if Table = "Table Pdx_a" then code = "P-dx"; else if Table = "Table injury_a" then code = "Injury"; female_n = frequency; female_p = percent/100; keep code female_n female_p; run; proc sort data = pinfpaininj_a; by code; proc sort data = pinfpaininj_f; by code; proc sort data = pinfpaininj_m; by code; run; data pinfpaininj_keep; merge pinfpaininj_a pinfpaininj_f pinfpaininj_m; by code; run; * Combine and save; data people; set chapter category mhdx mh_diag pinfpaininj_keep; run; proc datasets; delete chapter_long chapter_wide chapter_a chapter_f chapter_m category_long category_wide category_a category_f category_m MHdx_long MHdx_wide MHdx_a MHdx_f MHdx_m MH_DIAG_long MH_DIAG_wide MH_DIAG_a MH_DIAG_f MH_DIAG_m Pdx infect pain injury pinfpaininj pinfpaininj_a pinfpaininj_f pinfpaininj_m chapter category mhdx mh_diag pinfpaininj_keep; run; quit; **************************************************************************************************; * Age analyses: Encounter code level; **************************************************************************************************; data medrec2; set medrec1; age_dx_f = FLOOR(age_dx); run; * Encounter level; * # encounters/age; proc freq data = medrec2 noprint; table age_dx_f / list missing out = enc_age; run; data enc_age; set enc_age; enc_n = COUNT; drop COUNT PERCENT; run; * Chapter codes; proc freq data = medrec2 noprint; table diag_chapter*age_dx_f / list missing out = age_chapter; run; proc sort data = age_chapter; by age_dx_f diag_chapter; run; data age_chapter_wide; array dx_ch [15] A_n B_n D_n F_n H_n K_n L_n N_n P_n R_n S_n T_n U_n W_n X_n; do i = 1 to 15 until (last.age_dx_f); set age_chapter; by age_dx_f; dx_ch[i] = COUNT; end; drop i COUNT diag_chapter PERCENT; run; data age_chapter_p; merge age_chapter_wide enc_age; by age_dx_f; age = age_dx_f; array n [15] A_n B_n D_n F_n H_n K_n L_n N_n P_n R_n S_n T_n U_n W_n X_n; array p [15] A_p B_p D_p F_p H_p K_p L_p N_p P_p R_p S_p T_p U_p W_p X_p; do i = 1 to 15; if n[i] = . then n[i] = 0; p[i] = n[i]/enc_n; end; drop i age_dx_f; run; * Physical Health categories; proc freq data = medrec2 noprint; table PHcat*age_dx_f / list missing out = age_phcat; run; proc sort data = age_phcat; by age_dx_f PHcat; run; data age_phcat; set age_phcat; if PHcat ne .; run; data age_PHcat_wide; array ph [9] circ_n endo_n pulm_n gast_n urog_n musc_n hema_n canc_n neur_n; do i = 1 to 9 until (last.age_dx_f); set age_PHcat; by age_dx_f; if PHcat = 1 then circ_n = COUNT; if PHcat = 2 then endo_n = COUNT; if PHcat = 3 then pulm_n = COUNT; if PHcat = 4 then gast_n = COUNT; if PHcat = 5 then urog_n = COUNT; if PHcat = 6 then musc_n = COUNT; if PHcat = 7 then hema_n = COUNT; if PHcat = 8 then canc_n = COUNT; if PHcat = 9 then neur_n = COUNT; end; drop i COUNT PHcat PERCENT; run; data age_PHcat_p; merge age_PHcat_wide enc_age; by age_dx_f; age = age_dx_f; array n [9] circ_n endo_n pulm_n gast_n urog_n musc_n hema_n canc_n neur_n; array p [9] circ_p endo_p pulm_p gast_p urog_p musc_p hema_p canc_p neur_p; do i = 1 to 9; if n[i] = . then n[i] = 0; p[i] = n[i]/enc_n; end; drop i age_dx_f; run; * Mental Health categories & diagnoses; proc freq data = medrec2 noprint; table MHdx*age_dx_f / list missing out = age_mhdx; where diag_chapter = "P"; run; proc sort data = age_MHdx; by age_dx_f MHdx; run; data age_MHdx; set age_MHdx; if MHdx ne .; run; data age_MHdx_wide; array n [24] str_n adhd_n anx_n dem_n dep_n dev_n eat_n phb_n psy_n ptsd_n sex_n slp_n som_n sub_n sui_n cha_n con_n per_n chf_n adl_n stu_n fea_n irr_n oth_n; do i = 1 to 24 until (last.age_dx_f); set age_MHdx; by age_dx_f; if MHdx = 1 then str_n = COUNT; if MHdx = 2 then adhd_n = COUNT; if MHdx = 3 then anx_n = COUNT; if MHdx = 4 then dem_n = COUNT; if MHdx = 5 then dep_n = COUNT; if MHdx = 6 then dev_n = COUNT; if MHdx = 7 then eat_n = COUNT; if MHdx = 8 then phb_n = COUNT; if MHdx = 9 then psy_n = COUNT; if MHdx = 10 then ptsd_n = COUNT; if MHdx = 11 then sex_n = COUNT; if MHdx = 12 then slp_n = COUNT; if MHdx = 13 then som_n = COUNT; if MHdx = 14 then sub_n = COUNT; if MHdx = 15 then sui_n = COUNT; if MHdx = 16 then cha_n = COUNT; if MHdx = 17 then con_n = COUNT; if MHdx = 18 then per_n = COUNT; if MHdx = 19 then chf_n = COUNT; if MHdx = 20 then adl_n = COUNT; if MHdx = 21 then stu_n = COUNT; if MHdx = 22 then fea_n = COUNT; if MHdx = 23 then irr_n = COUNT; if MHdx = 24 then oth_n = COUNT; end; drop i MHdx COUNT PERCENT; run; data age_MHdx_p; merge age_MHdx_wide enc_age; by age_dx_f; age = age_dx_f; array n [24] str_n adhd_n anx_n dem_n dep_n dev_n eat_n phb_n psy_n ptsd_n sex_n slp_n som_n sub_n sui_n cha_n con_n per_n chf_n adl_n stu_n fea_n irr_n oth_n; array p [24] str_p adhd_p anx_p dem_p dep_p dev_p eat_p phb_p psy_p ptsd_p sex_p slp_p som_p sub_p sui_p cha_p con_p per_p chf_p adl_p stu_p fea_p irr_p oth_p; do i = 1 to 24; if n[i] = . then n[i] = 0; p[i] = n[i]/enc_n; end; drop i age_dx_f; run; * Mental Health diagnoses; proc freq data = medrec2 noprint; table DIAG*age_dx_f / list missing out = age_DIAG; where diag_chapter = "P"; run; proc sort data = age_DIAG; by age_dx_f DIAG; run; data age_DIAG; set age_DIAG; if DIAG ne ""; run; data age_MHDIAG_wide; array n [44] P02_n P81_n P01_n P74_n P05_n P20_n P70_n P03_n P76_n P24_n P28_n P85_n P11_n P86_n P79_n P71_n P72_n P73_n P98_n P82_n P07_n P08_n P09_n P06_n P75_n P15_n P16_n P17_n P18_n P19_n P77_n P04_n P10_n P12_n P13_n P22_n P23_n P25_n P27_n P29_n P78_n P80_n P99_n Pcode_n; do i = 1 to 44 until (last.age_dx_f); set age_DIAG; by age_dx_f; if DIAG = "P02" then P02_n = COUNT; if DIAG = "P81" then P81_n = COUNT; if DIAG = "P01" then P01_n = COUNT; if DIAG = "P74" then P74_n = COUNT; if DIAG = "P05" then P05_n = COUNT; if DIAG = "P20" then P20_n = COUNT; if DIAG = "P70" then P70_n = COUNT; if DIAG = "P03" then P03_n = COUNT; if DIAG = "P76" then P76_n = COUNT; if DIAG = "P24" then P24_n = COUNT; if DIAG = "P28" then P28_n = COUNT; if DIAG = "P85" then P85_n = COUNT; if DIAG = "P11" then P11_n = COUNT; if DIAG = "P86" then P86_n = COUNT; if DIAG = "P79" then P79_n = COUNT; if DIAG = "P71" then P71_n = COUNT; if DIAG = "P72" then P72_n = COUNT; if DIAG = "P73" then P73_n = COUNT; if DIAG = "P98" then P98_n = COUNT; if DIAG = "P82" then P82_n = COUNT; if DIAG = "P07" then P07_n = COUNT; if DIAG = "P08" then P08_n = COUNT; if DIAG = "P09" then P09_n = COUNT; if DIAG = "P06" then P06_n = COUNT; if DIAG = "P75" then P75_n = COUNT; if DIAG = "P15" then P15_n = COUNT; if DIAG = "P16" then P16_n = COUNT; if DIAG = "P17" then P17_n = COUNT; if DIAG = "P18" then P18_n = COUNT; if DIAG = "P19" then P19_n = COUNT; if DIAG = "P77" then P77_n = COUNT; if DIAG = "P04" then P04_n = COUNT; if DIAG = "P10" then P10_n = COUNT; if DIAG = "P12" then P12_n = COUNT; if DIAG = "P13" then P13_n = COUNT; if DIAG = "P22" then P22_n = COUNT; if DIAG = "P23" then P23_n = COUNT; if DIAG = "P25" then P25_n = COUNT; if DIAG = "P27" then P27_n = COUNT; if DIAG = "P29" then P29_n = COUNT; if DIAG = "P78" then P78_n = COUNT; if DIAG = "P80" then P80_n = COUNT; if DIAG = "P99" then P99_n = COUNT; if DIAG = "P" then Pcode_n = COUNT; end; drop i DIAG COUNT PERCENT; run; data age_MHDIAG_p; merge age_MHDIAG_wide enc_age; by age_dx_f; age = age_dx_f; array n [44] P02_n P81_n P01_n P74_n P05_n P20_n P70_n P03_n P76_n P24_n P28_n P85_n P11_n P86_n P79_n P71_n P72_n P73_n P98_n P82_n P07_n P08_n P09_n P06_n P75_n P15_n P16_n P17_n P18_n P19_n P77_n P04_n P10_n P12_n P13_n P22_n P23_n P25_n P27_n P29_n P78_n P80_n P99_n Pcode_n; array p [44] P02_p P81_p P01_p P74_p P05_p P20_p P70_p P03_p P76_p P24_p P28_p P85_p P11_p P86_p P79_p P71_p P72_p P73_p P98_p P82_p P07_p P08_p P09_p P06_p P75_p P15_p P16_p P17_p P18_p P19_p P77_p P04_p P10_p P12_p P13_p P22_p P23_p P25_p P27_p P29_p P78_p P80_p P99_p Pcode_p; do i = 1 to 44; if n[i] = . then n[i] = 0; p[i] = n[i]/enc_n; end; drop i age_dx_f; run; * P-diagnosis codes; proc freq data = medrec2 noprint; table Pdx*age_dx_f / list missing out = age_Pdx; run; proc sort data = age_Pdx; by age_dx_f; run; data age_Pdx; set age_Pdx; if Pdx ne .; run; data age_Pdx_p; merge age_Pdx enc_age; by age_dx_f; age = age_dx_f; Pdx_n = COUNT; Pdx_p = Pdx_n/enc_n; drop age_dx_f Pdx COUNT PERCENT; run; * Infections; proc freq data = medrec2 noprint; table infect*age_dx_f / list missing out = age_infect; run; proc sort data = age_infect; by age_dx_f; run; data age_infect; set age_infect; if infect ne .; run; data age_infect_p; merge age_infect enc_age; by age_dx_f; age = age_dx_f; infect_n = COUNT; infect_p = infect_n/enc_n; drop age_dx_f infect COUNT PERCENT; run; * Pain; proc freq data = medrec2 noprint; table pain*age_dx_f / list missing out = age_pain; run; proc sort data = age_pain; by age_dx_f; run; data age_pain; set age_pain; if pain ne .; run; data age_pain_p; merge age_pain enc_age; by age_dx_f; age = age_dx_f; pain_n = COUNT; pain_p = pain_n/enc_n; drop age_dx_f pain COUNT PERCENT; run; * Injuries; proc freq data = medrec2 noprint; table injury*age_dx_f / list missing out = age_injury; run; proc sort data = age_injury; by age_dx_f; run; data age_injury; set age_injury; if injury ne .; run; data age_injury_p; merge age_injury enc_age; by age_dx_f; age = age_dx_f; injury_n = COUNT; injury_p = injury_n/enc_n; drop age_dx_f injury COUNT PERCENT; run; data age_enclevel; merge age_chapter_p age_phcat_p age_mhdx_p age_mhdiag_p age_pdx_p age_infect_p age_pain_p age_injury_p; by age; run; proc datasets; delete age_chapter age_chapter_wide age_PHcat age_PHcat_wide age_mhdx age_mhdx_wide age_DIAG age_MHDIAG_wide age_pdx age_infect age_pain age_pdx age_injury age_chapter_p age_phcat_p age_mhdx_p age_mhdiag_p age_pdx_p age_infect_p age_pain_p age_pdx_p age_injury_p; run; quit; **************************************************************************************************; * Age analysis: Person level; **************************************************************************************************; * People N's for each age; data ages; set demo; array age [101] a0-a100; do i = 1 to 101; if i-1 >= FLOOR(age_start) and i-1 <= FLOOR(age_end) then age[i] = 1; else age[i] = 0; end; keep w19_1011_lnr_k2_ age_start age_end a0-a100; run; proc summary data = ages; var a0-a100; output out=age_frq(keep = a0-a100) sum =; run; proc transpose data = age_frq out = ages_n; run; data ages_n; set ages_n; age_dx_f = INPUT(SUBSTR(_NAME_, 2), 8.); tot_n = COL1; drop _NAME_ COL1; run; * Chapter codes; proc freq data = medrec2 noprint; table w19_1011_lnr_k2_*diag_chapter*age_dx_f / list missing out = age_chapter_long1; run; proc freq data = age_chapter_long1 noprint; table diag_chapter*age_dx_f / list missing out = age_chapter_long2; run; proc sort data = age_chapter_long2; by age_dx_f diag_chapter; run; data age_chapter_wide; array dx_ch [15] A_n B_n D_n F_n H_n K_n L_n N_n P_n R_n S_n T_n U_n W_n X_n; do i = 1 to 15 until (last.age_dx_f); set age_chapter_long2; by age_dx_f; dx_ch[i] = COUNT; end; drop i COUNT diag_chapter PERCENT; run; data age_chapter_p; merge age_chapter_wide ages_n; by age_dx_f; age = age_dx_f; array n [15] A_n B_n D_n F_n H_n K_n L_n N_n P_n R_n S_n T_n U_n W_n X_n; array p [15] A_p B_p D_p F_p H_p K_p L_p N_p P_p R_p S_p T_p U_p W_p X_p; do i = 1 to 15; if n[i] = . then n[i] = 0; p[i] = n[i]/tot_n; end; drop i age_dx_f; run; * Physical Health cagetories; proc freq data = medrec2 noprint; table w19_1011_lnr_k2_*PHcat*age_dx_f / list out = age_PHcat_long1; run; proc freq data = age_PHcat_long1 noprint; table PHcat*age_dx_f / list out = age_PHcat_long2; run; proc sort data = age_PHcat_long2; by age_dx_f PHcat; run; data age_PHcat_long2; set age_PHcat_long2; if PERCENT ne .; run; data age_PHcat_wide; array ph [9] circ_n endo_n pulm_n gast_n urog_n musc_n hema_n canc_n neur_n; do i = 1 to 9 until (last.age_dx_f); set age_PHcat_long2; by age_dx_f; if PHcat = 1 then circ_n = COUNT; if PHcat = 2 then endo_n = COUNT; if PHcat = 3 then pulm_n = COUNT; if PHcat = 4 then gast_n = COUNT; if PHcat = 5 then urog_n = COUNT; if PHcat = 6 then musc_n = COUNT; if PHcat = 7 then hema_n = COUNT; if PHcat = 8 then canc_n = COUNT; if PHcat = 9 then neur_n = COUNT; end; drop i COUNT PHcat PERCENT; run; data age_PHcat_p; merge age_PHcat_wide ages_n; by age_dx_f; age = age_dx_f; array n [9] circ_n endo_n pulm_n gast_n urog_n musc_n hema_n canc_n neur_n; array p [9] circ_p endo_p pulm_p gast_p urog_p musc_p hema_p canc_p neur_p; do i = 1 to 9; if n[i] = . then n[i] = 0; p[i] = n[i]/tot_n; end; drop i age_dx_f; run; * Mental Health categories; proc freq data = medrec2 noprint; table w19_1011_lnr_k2_*MHdx*age_dx_f / list missing out = age_MHdx_long1; where diag_chapter = "P"; run; proc freq data = age_MHdx_long1 noprint; table MHdx*age_dx_f / list missing out = age_MHdx_long2; run; proc sort data = age_MHdx_long2; by age_dx_f MHdx; run; data age_MHdx_long2; set age_MHdx_long2; if MHdx ne .; run; data age_MHdx_wide; array n [24] str_n adhd_n anx_n dem_n dep_n dev_n eat_n phb_n psy_n ptsd_n sex_n slp_n som_n sub_n sui_n cha_n con_n per_n chf_n adl_n stu_n fea_n irr_n oth_n; do i = 1 to 24 until (last.age_dx_f); set age_MHdx_long2; by age_dx_f; if MHdx = 1 then str_n = COUNT; if MHdx = 2 then adhd_n = COUNT; if MHdx = 3 then anx_n = COUNT; if MHdx = 4 then dem_n = COUNT; if MHdx = 5 then dep_n = COUNT; if MHdx = 6 then dev_n = COUNT; if MHdx = 7 then eat_n = COUNT; if MHdx = 8 then phb_n = COUNT; if MHdx = 9 then psy_n = COUNT; if MHdx = 10 then ptsd_n = COUNT; if MHdx = 11 then sex_n = COUNT; if MHdx = 12 then slp_n = COUNT; if MHdx = 13 then som_n = COUNT; if MHdx = 14 then sub_n = COUNT; if MHdx = 15 then sui_n = COUNT; if MHdx = 16 then cha_n = COUNT; if MHdx = 17 then con_n = COUNT; if MHdx = 18 then per_n = COUNT; if MHdx = 19 then chf_n = COUNT; if MHdx = 20 then adl_n = COUNT; if MHdx = 21 then stu_n = COUNT; if MHdx = 22 then fea_n = COUNT; if MHdx = 23 then irr_n = COUNT; if MHdx = 24 then oth_n = COUNT; end; drop i MHdx COUNT PERCENT; run; data age_MHdx_p; merge age_MHdx_wide ages_n; by age_dx_f; age = age_dx_f; array n [24] str_n adhd_n anx_n dem_n dep_n dev_n eat_n phb_n psy_n ptsd_n sex_n slp_n som_n sub_n sui_n cha_n con_n per_n chf_n adl_n stu_n fea_n irr_n oth_n; array p [24] str_p adhd_p anx_p dem_p dep_p dev_p eat_p phb_p psy_p ptsd_p sex_p slp_p som_p sub_p sui_p cha_p con_p per_p chf_p adl_p stu_p fea_p irr_p oth_p; do i = 1 to 24; if n[i] = . then n[i] = 0; p[i] = n[i]/tot_n; end; drop i age_dx_f; run; * Mental Health codes; proc freq data = medrec2 noprint; table w19_1011_lnr_k2_*DIAG*age_dx_f / list missing out = age_MHDIAG_long1; where diag_chapter = "P"; run; proc freq data = age_MHDIAG_long1 noprint; table DIAG*age_dx_f / list missing out = age_MHDIAG_long2; run; proc sort data = age_MHDIAG_long2; by age_dx_f DIAG; run; data age_MHDIAG_long2; set age_MHDIAG_long2; if DIAG ne ""; run; data age_MHDIAG_wide; array n [44] P02_n P81_n P01_n P74_n P05_n P20_n P70_n P03_n P76_n P24_n P28_n P85_n P11_n P86_n P79_n P71_n P72_n P73_n P98_n P82_n P07_n P08_n P09_n P06_n P75_n P15_n P16_n P17_n P18_n P19_n P77_n P04_n P10_n P12_n P13_n P22_n P23_n P25_n P27_n P29_n P78_n P80_n P99_n Pcode_n; do i = 1 to 44 until (last.age_dx_f); set age_MHDIAG_long2; by age_dx_f; if DIAG = "P02" then P02_n = COUNT; if DIAG = "P81" then P81_n = COUNT; if DIAG = "P01" then P01_n = COUNT; if DIAG = "P74" then P74_n = COUNT; if DIAG = "P05" then P05_n = COUNT; if DIAG = "P20" then P20_n = COUNT; if DIAG = "P70" then P70_n = COUNT; if DIAG = "P03" then P03_n = COUNT; if DIAG = "P76" then P76_n = COUNT; if DIAG = "P24" then P24_n = COUNT; if DIAG = "P28" then P28_n = COUNT; if DIAG = "P85" then P85_n = COUNT; if DIAG = "P11" then P11_n = COUNT; if DIAG = "P86" then P86_n = COUNT; if DIAG = "P79" then P79_n = COUNT; if DIAG = "P71" then P71_n = COUNT; if DIAG = "P72" then P72_n = COUNT; if DIAG = "P73" then P73_n = COUNT; if DIAG = "P98" then P98_n = COUNT; if DIAG = "P82" then P82_n = COUNT; if DIAG = "P07" then P07_n = COUNT; if DIAG = "P08" then P08_n = COUNT; if DIAG = "P09" then P09_n = COUNT; if DIAG = "P06" then P06_n = COUNT; if DIAG = "P75" then P75_n = COUNT; if DIAG = "P15" then P15_n = COUNT; if DIAG = "P16" then P16_n = COUNT; if DIAG = "P17" then P17_n = COUNT; if DIAG = "P18" then P18_n = COUNT; if DIAG = "P19" then P19_n = COUNT; if DIAG = "P77" then P77_n = COUNT; if DIAG = "P04" then P04_n = COUNT; if DIAG = "P10" then P10_n = COUNT; if DIAG = "P12" then P12_n = COUNT; if DIAG = "P13" then P13_n = COUNT; if DIAG = "P22" then P22_n = COUNT; if DIAG = "P23" then P23_n = COUNT; if DIAG = "P25" then P25_n = COUNT; if DIAG = "P27" then P27_n = COUNT; if DIAG = "P29" then P29_n = COUNT; if DIAG = "P78" then P78_n = COUNT; if DIAG = "P80" then P80_n = COUNT; if DIAG = "P99" then P99_n = COUNT; if DIAG = "P" then Pcode_n = COUNT; end; drop i DIAG COUNT PERCENT; run; data age_MHDIAG_p; merge age_MHDIAG_wide ages_n; by age_dx_f; age = age_dx_f; array n [44] P02_n P81_n P01_n P74_n P05_n P20_n P70_n P03_n P76_n P24_n P28_n P85_n P11_n P86_n P79_n P71_n P72_n P73_n P98_n P82_n P07_n P08_n P09_n P06_n P75_n P15_n P16_n P17_n P18_n P19_n P77_n P04_n P10_n P12_n P13_n P22_n P23_n P25_n P27_n P29_n P78_n P80_n P99_n Pcode_n; array p [44] P02_p P81_p P01_p P74_p P05_p P20_p P70_p P03_p P76_p P24_p P28_p P85_p P11_p P86_p P79_p P71_p P72_p P73_p P98_p P82_p P07_p P08_p P09_p P06_p P75_p P15_p P16_p P17_p P18_p P19_p P77_p P04_p P10_p P12_p P13_p P22_p P23_p P25_p P27_p P29_p P78_p P80_p P99_p Pcode_p; do i = 1 to 44; if n[i] = . then n[i] = 0; p[i] = n[i]/tot_n; end; drop i age_dx_f; run; * P-diagnoses; proc freq data = medrec2 noprint; table w19_1011_lnr_k2_*Pdx*age_dx_f / list missing out = age_Pdx_long1; run; proc freq data = age_Pdx_long1 noprint; table Pdx*age_dx_f / list missing out = age_Pdx_long2; run; proc sort data = age_Pdx_long2; by age_dx_f; run; data age_Pdx_long2; set age_Pdx_long2; if Pdx ne .; run; data age_Pdx_p; merge age_Pdx_long2 ages_n; by age_dx_f; age = age_dx_f; Pdx_n = COUNT; Pdx_p = Pdx_n/tot_n; drop age_dx_f Pdx COUNT PERCENT; run; * Infection; proc freq data = medrec2 noprint; table w19_1011_lnr_k2_*infect*age_dx_f / list missing out = age_infect_long1; run; proc freq data = age_infect_long1 noprint; table infect*age_dx_f / list missing out = age_infect_long2; run; proc sort data = age_infect_long2; by age_dx_f; run; data age_infect_long2; set age_infect_long2; if infect ne .; run; data age_infect_p; merge age_infect_long2 ages_n; by age_dx_f; age = age_dx_f; infect_n = COUNT; infect_p = infect_n/tot_n; drop age_dx_f infect COUNT PERCENT; run; * Pain; proc freq data = medrec2 noprint; table w19_1011_lnr_k2_*pain*age_dx_f / list missing out = age_pain_long1; run; proc freq data = age_pain_long1 noprint; table pain*age_dx_f / list missing out = age_pain_long2; run; proc sort data = age_pain_long2; by age_dx_f; run; data age_pain_long2; set age_pain_long2; if pain ne .; run; data age_pain_p; merge age_pain_long2 ages_n; by age_dx_f; age = age_dx_f; pain_n = COUNT; pain_p = pain_n/tot_n; drop age_dx_f pain COUNT PERCENT; run; * Injury; proc freq data = medrec2 noprint; table w19_1011_lnr_k2_*injury*age_dx_f / list missing out = age_injury_long1; run; proc freq data = age_injury_long1 noprint; table injury*age_dx_f / list missing out = age_injury_long2; run; proc sort data = age_injury_long2; by age_dx_f; run; data age_injury_long2; set age_injury_long2; if injury ne .; run; data age_injury_p; merge age_injury_long2 ages_n; by age_dx_f; age = age_dx_f; injury_n = COUNT; injury_p = injury_n/tot_n; drop age_dx_f injury COUNT PERCENT; run; * Combine and save; data age_personlevel; merge age_chapter_p age_phcat_p age_mhdx_p age_mhdiag_p age_Pdx_p age_infect_p age_pain_p age_injury_p; by age; run; proc datasets; delete ages age_frq age_chapter_long1 age_chapter_long2 age_chapter_wide age_PHcat_long1 age_PHcat_long2 age_PHcat_wide age_MHdx_long1 age_MHdx_long2 age_MHdx_wide age_MHDIAG_long1 age_MHDIAG_long2 age_MHDIAG_wide age_Pdx_long1 age_pdx_long2 age_infect_long1 age_infect_long2 age_pain_long1 age_pain_long2 age_injury_long1 age_injury_long2 age_chapter_p age_phcat_p age_mhdx_p age_mhdiag_p age_pdx_p age_infect_p age_pain_p age_injury_p; run; quit; * Output files as csv for graphing in R; proc export data = encounters outfile = "M:\p1074-renateh\2023_DayInLifeGP\Work\Results\EncounterLevel_Sept2023.csv" dbms = csv replace; run; proc export data = people outfile = "M:\p1074-renateh\2023_DayInLifeGP\Work\Results\PersonLevel_Sept2023.csv" dbms = csv replace; run; proc export data = age_enclevel outfile = "M:\p1074-renateh\2023_DayInLifeGP\Work\Results\EncounterLevel_age_Sept2023.csv" dbms = csv replace; run; proc export data = age_personlevel outfile = "M:\p1074-renateh\2023_DayInLifeGP\Work\Results\PersonLevel_age_Sept2023.csv" dbms = csv replace; run;