**** The program reads data on mental disorder diagnoses;

libname lib1 "W:\Uudet_datat\Hilmo_ja_kela\";
libname lib100 "W:\Tutkimusprojektit\Kirjanpito\Dokumentaatio\JAR\Datat\";
run;

*** Data for years 1996-2022, ICD-10 codes;

data hilmo1;
 set lib100.hilmot9622;
 
 * First character of diagnosis;

 DG1_1CHR = substr (DG1,1,1);
 DG2_1CHR = substr (DG2,1,1);
 DG3_1CHR = substr (DG3,1,1);
 DG4_1CHR = substr (DG4,1,1);
 DG5_1CHR = substr (DG5,1,1);
 DG6_1CHR = substr (DG6,1,1);
 DG7_1CHR = substr (DG7,1,1);
 DG8_1CHR = substr (DG8,1,1);
 DG9_1CHR = substr (DG9,1,1);
 DG10_1CHR = substr (DG10,1,1);
 DG11_1CHR = substr (DG11,1,1);
 DG12_1CHR = substr (DG12,1,1);
 DG13_1CHR = substr (DG13,1,1);
 DG14_1CHR = substr (DG14,1,1);
 DG15_1CHR = substr (DG15,1,1);
 DG16_1CHR = substr (DG16,1,1);

 * First two characters of diagnosis;

 DG1_2CHR = substr (DG1,1,2);
 DG2_2CHR = substr (DG2,1,2);
 DG3_2CHR = substr (DG3,1,2);
 DG4_2CHR = substr (DG4,1,2);
 DG5_2CHR = substr (DG5,1,2);
 DG6_2CHR = substr (DG6,1,2);
 DG7_2CHR = substr (DG7,1,2);
 DG8_2CHR = substr (DG8,1,2);
 DG9_2CHR = substr (DG9,1,2);
 DG10_2CHR = substr (DG10,1,2);
 DG11_2CHR = substr (DG11,1,2);
 DG12_2CHR = substr (DG12,1,2);
 DG13_2CHR = substr (DG13,1,2);
 DG14_2CHR = substr (DG14,1,2);
 DG15_2CHR = substr (DG15,1,2);
 DG16_2CHR = substr (DG16,1,2);

 * First three characters of diagnosis;

 DG1_3CHR = substr (DG1,1,3);
 DG2_3CHR = substr (DG2,1,3);
 DG3_3CHR = substr (DG3,1,3);
 DG4_3CHR = substr (DG4,1,3);
 DG5_3CHR = substr (DG5,1,3);
 DG6_3CHR = substr (DG6,1,3);
 DG7_3CHR = substr (DG7,1,3);
 DG8_3CHR = substr (DG8,1,3);
 DG9_3CHR = substr (DG9,1,3);
 DG10_3CHR = substr (DG10,1,3);
 DG11_3CHR = substr (DG11,1,3);
 DG12_3CHR = substr (DG12,1,3);
 DG13_3CHR = substr (DG13,1,3);
 DG14_3CHR = substr (DG14,1,3);
 DG15_3CHR = substr (DG15,1,3);
 DG16_3CHR = substr (DG16,1,3);


 * First four characters of diagnosis;

 DG1_4CHR = substr (DG1,1,4);
 DG2_4CHR = substr (DG2,1,4);
 DG3_4CHR = substr (DG3,1,4);
 DG4_4CHR = substr (DG4,1,4);
 DG5_4CHR = substr (DG5,1,4);
 DG6_4CHR = substr (DG6,1,4);
 DG7_4CHR = substr (DG7,1,4);
 DG8_4CHR = substr (DG8,1,4);
 DG9_4CHR = substr (DG9,1,4);
 DG10_4CHR = substr (DG10,1,4);
 DG11_4CHR = substr (DG11,1,4);
 DG12_4CHR = substr (DG12,1,4);
 DG13_4CHR = substr (DG13,1,4);
 DG14_4CHR = substr (DG14,1,4);
 DG15_4CHR = substr (DG15,1,4);
 DG16_4CHR = substr (DG16,1,4);

*** Diagnosis groups;

** Any psychiatric disorder: F-code in ICD 10;

 ANYPSYC = 0;

 if 	DG1_1CHR = 'F' or DG2_1CHR = 'F' or
   	DG3_1CHR = 'F' or DG4_1CHR = 'F' or
   	DG5_1CHR = 'F' or DG6_1CHR = 'F' or
	DG7_1CHR = 'F' or DG8_1CHR = 'F' or
	DG9_1CHR = 'F' or DG10_1CHR = 'F' or
	DG11_1CHR = 'F' or DG12_1CHR = 'F' or
	DG13_1CHR = 'F' or DG14_1CHR = 'F' or
	DG15_1CHR = 'F' or DG16_1CHR = 'F' then ANYPSYC = 1;

** Alcohol related disorders: F10 code in ICD 10;

 ALCOHOL = 0;

 if 	DG1_3CHR = 'F10' or
    	DG2_3CHR = 'F10' or
	DG3_3CHR = 'F10' or
	DG4_3CHR = 'F10' or
	DG5_3CHR = 'F10' or
	DG6_3CHR = 'F10' or
	DG7_3CHR = 'F10' or
	DG8_3CHR = 'F10' or
	DG9_3CHR = 'F10' or
	DG10_3CHR = 'F10' or
	DG11_3CHR = 'F10' or
	DG12_3CHR = 'F10' or
	DG13_3CHR = 'F10' or
	DG14_3CHR = 'F10' or
	DG15_3CHR = 'F10' or
	DG16_3CHR = 'F10' then ALCOHOL = 1;

** Drug use disorders: F11-F16, F18-F19 (not F17, tobacco) codes in ICD 10;

 DRUG = 0;

 if 	DG1_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
    	DG2_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG3_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG4_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG5_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG6_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG7_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG8_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG9_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG10_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG11_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG12_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG13_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG14_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG15_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG16_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') then DRUG = 1;

if ALCOHOL = 1 or DRUG = 1 then SUBSTANCE = 1;

** Other F-diagnosis;

if (DG1_2CHR ne 'F1' and DG1_2CHR ne '') or DG1_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG2_2CHR ne 'F1' and DG2_2CHR ne '') or DG2_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG3_2CHR ne 'F1' and DG3_2CHR ne '') or DG3_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG4_2CHR ne 'F1' and DG4_2CHR ne '') or DG4_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG5_2CHR ne 'F1' and DG5_2CHR ne '') or DG5_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG6_2CHR ne 'F1' and DG6_2CHR ne '') or DG6_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG7_2CHR ne 'F1' and DG7_2CHR ne '') or DG7_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG8_2CHR ne 'F1' and DG8_2CHR ne '') or DG8_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG9_2CHR ne 'F1' and DG9_2CHR ne '') or DG9_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG10_2CHR ne 'F1' and DG10_2CHR ne '') or DG10_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG11_2CHR ne 'F1' and DG11_2CHR ne '') or DG11_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG12_2CHR ne 'F1' and DG12_2CHR ne '') or DG12_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG13_2CHR ne 'F1' and DG13_2CHR ne '') or DG13_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG14_2CHR ne 'F1' and DG14_2CHR ne '') or DG14_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG15_2CHR ne 'F1' and DG15_2CHR ne '') or DG15_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG16_2CHR ne 'F1' and DG16_2CHR ne '') or DG16_3CHR = 'F17' then ALL_BUT_SUBS = 1;

** Final data, includes only persons having any some psychiatric diagnosis;

if ANYPSYC = 1;

 TULOVUOSI = year(TUPVA);
 TULOKK = month(TUPVA);
 TULOPP = day(TUPVA);

 LAHTOVUOSI = year(LPVM);
 LAHTOKK = month(LPVM);
 LAHTOPP = day(LPVM);

 if TULOVUOSI >= 1996;

keep shnro TULOVUOSI TULOKK TULOPP LAHTOVUOSI SUBSTANCE ALL_BUT_SUBS;
rename shnro = ID;
run;

*** Data for years 1994-1995;

data hilmo2;
 set lib1.suoj_u0599_poisto9495;


 * First two characters of diagnosis;

 PDG_2CHR = substr (PDG,1,2);
 SDG1_2CHR = substr (SDG1,1,2);
 SDG2_2CHR = substr (SDG2,1,2);
 
 * First three characters of diagnosis;

 PDG_3CHR = substr (PDG,1,3);
 SDG1_3CHR = substr (SDG1,1,3);
 SDG2_3CHR = substr (SDG2,1,3);

 * First four characters of diagnosis;

 PDG_4CHR = substr (PDG,1,4);
 SDG1_4CHR = substr (SDG1,1,4);
 SDG2_4CHR = substr (SDG2,1,4);

*** Diagnosis groups;

** Any psychiatric disorder: Codes 290-319 from 1987;

 ANYPSYC = 0;

 if PDG_2CHR in ('29','30','31') or
	SDG1_2CHR in ('29','30','31') or
	SDG2_2CHR in ('29','30','31') then ANYPSYC = 1;

 ** Alcohol related disorders: Codes 291, 303x, 3050 from 1987;

 ALCOHOL = 0;

 if PDG_3CHR in ('303','291') or
	SDG1_3CHR in ('303','291') or
	SDG2_3CHR in ('303','291') or
	PDG_4CHR in ('3050') or
	SDG1_4CHR in ('3050') or
	SDG2_4CHR in ('3050') then ALCOHOL = 1;

** Drug use disorders: Codes 292, 304, 3052, 3053, 3054, 3055, 3056, 3057, 3059 from 1987;

 DRUG = 0;

 if PDG_3CHR in ('292','304') or
    SDG1_3CHR in ('292','304') or
	SDG2_3CHR in ('292','304') or
	PDG_4CHR in ('3052','3053','3054','3055','3056','3057','3059') or
	SDG1_4CHR in ('3052','3053','3054','3055','3056','3057','3059') or
	SDG2_4CHR in ('3052','3053','3054','3055','3056','3057','3059') then DRUG  = 1;

if ALCOHOL = 1 or DRUG = 1 then SUBSTANCE = 1;

** Other F-diagnosis;

 if PDG_2CHR in ('29','30','31') and PDG_3CHR ne '303' and PDG_3CHR ne '291' and PDG_4CHR ne '3050' and
 PDG_3CHR ne '292' and PDG_3CHR ne '304' and PDG_4CHR ne '3052' and PDG_4CHR ne '3053' and
 PDG_4CHR ne '3054' and PDG_4CHR ne '3055' and PDG_4CHR ne '3056' and PDG_4CHR ne '3057' and PDG_4CHR ne '3059' then ALL_BUT_SUBS = 1;

 if SDG1_2CHR in ('29','30','31') and SDG1_3CHR ne '303' and SDG1_3CHR ne '291' and SDG1_4CHR ne '3050' and
 SDG1_3CHR ne '292' and SDG1_3CHR ne '304' and SDG1_4CHR ne '3052' and SDG1_4CHR ne '3053' and
 SDG1_4CHR ne '3054' and SDG1_4CHR ne '3055' and SDG1_4CHR ne '3056' and SDG1_4CHR ne '3057' and SDG1_4CHR ne '3059' then ALL_BUT_SUBS = 1;

  if SDG2_2CHR in ('29','30','31') and SDG2_3CHR ne '303' and SDG2_3CHR ne '291' and SDG2_4CHR ne '3050' and
 SDG2_3CHR ne '292' and SDG2_3CHR ne '304' and SDG2_4CHR ne '3052' and SDG2_4CHR ne '3053' and
 SDG2_4CHR ne '3054' and SDG2_4CHR ne '3055' and SDG2_4CHR ne '3056' and SDG2_4CHR ne '3057' and SDG2_4CHR ne '3059' then ALL_BUT_SUBS = 1;

** Final data, includes only persons having any some psychiatric diagnosis;

if ANYPSYC = 1;

 TULOVUOSI = substr (TUPVA,7,4)*1;
 TULOKK = substr (TUPVA,4,2)*1;
 TULOPP = substr (TUPVA,1,2)*1;

 LAHTOVUOSI = substr (LPVM,7,4)*1;
 LAHTOKK = substr (LPVM,4,2)*1;
 LAHTOPP = substr (LPVM,1,2)*1;

if 1994 <= TULOVUOSI <= 1995;

keep shnro TULOVUOSI TULOKK TULOPP LAHTOVUOSI SUBSTANCE ALL_BUT_SUBS;
rename shnro = ID;
run;

*** Data for years 1987-1993;

data hilmo3;
 set lib1.suoj_u0599_poisto8793;

 * First two characters of diagnosis;

 PDG_2CHR = substr (PDG,1,2);
 SDG1_2CHR = substr (SDG1,1,2);
 SDG2_2CHR = substr (SDG2,1,2);
 SDG3_2CHR = substr (SDG3,1,2);
 
 * First three characters of diagnosis;

 PDG_3CHR = substr (PDG,1,3);
 SDG1_3CHR = substr (SDG1,1,3);
 SDG2_3CHR = substr (SDG2,1,3);
 SDG3_3CHR = substr (SDG3,1,3);

 * First four characters of diagnosis;

 PDG_4CHR = substr (PDG,1,4);
 SDG1_4CHR = substr (SDG1,1,4);
 SDG2_4CHR = substr (SDG2,1,4);
 SDG3_4CHR = substr (SDG3,1,4);

*** Diagnosis groups;

** Any psychiatric disorder: Codes 290-319 from 1987;

 ANYPSYC = 0;

 if PDG_2CHR in ('29','30','31') or
	SDG1_2CHR in ('29','30','31') or
	SDG2_2CHR in ('29','30','31') or
	SDG3_2CHR in ('29','30','31') then ANYPSYC = 1;

 ** Alcohol related disorders: Codes 291, 303x, 3050 from 1987;

 ALCOHOL = 0;

 if PDG_3CHR in ('303','291') or
	SDG1_3CHR in ('303','291') or
	SDG2_3CHR in ('303','291') or
	SDG3_3CHR in ('303','291') or
	PDG_4CHR in ('3050') or
	SDG1_4CHR in ('3050') or
	SDG2_4CHR in ('3050') or
	SDG3_4CHR in ('3050') then ALCOHOL = 1;

** Drug use disorders: Codes 292, 304, 3052, 3053, 3054, 3055, 3056, 3057, 3059 from 1987;

 DRUG = 0;

 if PDG_3CHR in ('292','304') or
    SDG1_3CHR in ('292','304') or
	SDG2_3CHR in ('292','304') or
	SDG3_3CHR in ('292','304') or
	PDG_4CHR in ('3052','3053','3054','3055','3056','3057','3059') or
	SDG1_4CHR in ('3052','3053','3054','3055','3056','3057','3059') or
	SDG2_4CHR in ('3052','3053','3054','3055','3056','3057','3059') or
	SDG3_4CHR in ('3052','3053','3054','3055','3056','3057','3059')then DRUG  = 1;

if ALCOHOL = 1 or DRUG = 1 then SUBSTANCE = 1;

** Other F-diagnosis;

 if PDG_2CHR in ('29','30','31') and PDG_3CHR ne '303' and PDG_3CHR ne '291' and PDG_4CHR ne '3050' and
 PDG_3CHR ne '292' and PDG_3CHR ne '304' and PDG_4CHR ne '3052' and PDG_4CHR ne '3053' and
 PDG_4CHR ne '3054' and PDG_4CHR ne '3055' and PDG_4CHR ne '3056' and PDG_4CHR ne '3057' and PDG_4CHR ne '3059' then ALL_BUT_SUBS = 1;

 if SDG1_2CHR in ('29','30','31') and SDG1_3CHR ne '303' and SDG1_3CHR ne '291' and SDG1_4CHR ne '3050' and
 SDG1_3CHR ne '292' and SDG1_3CHR ne '304' and SDG1_4CHR ne '3052' and SDG1_4CHR ne '3053' and
 SDG1_4CHR ne '3054' and SDG1_4CHR ne '3055' and SDG1_4CHR ne '3056' and SDG1_4CHR ne '3057' and SDG1_4CHR ne '3059' then ALL_BUT_SUBS = 1;

  if SDG2_2CHR in ('29','30','31') and SDG2_3CHR ne '303' and SDG2_3CHR ne '291' and SDG2_4CHR ne '3050' and
 SDG2_3CHR ne '292' and SDG2_3CHR ne '304' and SDG2_4CHR ne '3052' and SDG2_4CHR ne '3053' and
 SDG2_4CHR ne '3054' and SDG2_4CHR ne '3055' and SDG2_4CHR ne '3056' and SDG2_4CHR ne '3057' and SDG2_4CHR ne '3059' then ALL_BUT_SUBS = 1;

  if SDG3_2CHR in ('29','30','31') and SDG3_3CHR ne '303' and SDG3_3CHR ne '291' and SDG3_4CHR ne '3050' and
 SDG3_3CHR ne '292' and SDG3_3CHR ne '304' and SDG3_4CHR ne '3052' and SDG3_4CHR ne '3053' and
 SDG3_4CHR ne '3054' and SDG3_4CHR ne '3055' and SDG3_4CHR ne '3056' and SDG3_4CHR ne '3057' and SDG3_4CHR ne '3059' then ALL_BUT_SUBS = 1;

** Final data, includes only persons having any some psychiatric diagnosis;

if ANYPSYC = 1;

 TULOVUOSI = substr (TUPVA,7,4)*1;
 TULOKK = substr (TUPVA,4,2)*1;
 TULOPP = substr (TUPVA,1,2)*1;

 LAHTOVUOSI = substr (LPVM,7,4)*1;
 LAHTOKK = substr (LPVM,4,2)*1;
 LAHTOPP = substr (LPVM,1,2)*1;

if 1987 <= TULOVUOSI <= 1993;

keep shnro TULOVUOSI TULOKK TULOPP LAHTOVUOSI SUBSTANCE ALL_BUT_SUBS;
rename shnro = ID;
run;


data hilmo4;
 set lib1.suoj_u0599_poisto6986;
	
 PDG = DG1;
 SDG1 = DG2;
 SDG2 = DG3;
 SDG3 = DG4;

 * First two characters of diagnosis;

 PDG_2CHR = substr (PDG,1,2);
 SDG1_2CHR = substr (SDG1,1,2);
 SDG2_2CHR = substr (SDG2,1,2);
 SDG3_2CHR = substr (SDG3,1,2);
 
 * First three characters of diagnosis;

 PDG_3CHR = substr (PDG,1,3);
 SDG1_3CHR = substr (SDG1,1,3);
 SDG2_3CHR = substr (SDG2,1,3);
 SDG3_3CHR = substr (SDG3,1,3);

 * First four characters of diagnosis;

 PDG_4CHR = substr (PDG,1,4);
 SDG1_4CHR = substr (SDG1,1,4);
 SDG2_4CHR = substr (SDG2,1,4);
 SDG3_4CHR = substr (SDG3,1,4);

*** Diagnosis groups;

** Any psychiatric disorder: Codes 290-319 from 1987;

 ANYPSYC = 0;

 if PDG_2CHR in ('29','30','31') or
	SDG1_2CHR in ('29','30','31') or
	SDG2_2CHR in ('29','30','31') or
	SDG3_2CHR in ('29','30','31') then ANYPSYC = 1;

 ** Alcohol related disorders: Codes 291, 303x from 1969;

 ALCOHOL = 0;

 if PDG_3CHR in ('303','291') or
	SDG1_3CHR in ('303','291') or
	SDG2_3CHR in ('303','291') or
	SDG3_3CHR in ('303','291') then ALCOHOL = 1;

** Drug use disorders: Codes 304, 2943 from 1969;

 DRUG = 0;

 if PDG_3CHR in ('304') or
    SDG1_3CHR in ('304') or
	SDG2_3CHR in ('304') or
	SDG3_3CHR in ('304') or
	PDG_4CHR in ('2943') or
	SDG1_4CHR in ('2943') or
	SDG2_4CHR in ('2943') or
	SDG3_4CHR in ('2943') then DRUG  = 1;

if ALCOHOL = 1 or DRUG = 1 then SUBSTANCE = 1;

** Other F-diagnosis;

  if PDG_2CHR in ('29','30','31') and PDG_3CHR ne '303' and PDG_3CHR ne '291' and
 PDG_3CHR ne '304' and PDG_4CHR ne '2943' then ALL_BUT_SUBS = 1;

  if SDG1_2CHR in ('29','30','31') and SDG1_3CHR ne '303' and SDG1_3CHR ne '291' and
 SDG1_3CHR ne '304' and SDG1_4CHR ne '2943' then ALL_BUT_SUBS = 1;

  if SDG2_2CHR in ('29','30','31') and SDG2_3CHR ne '303' and SDG2_3CHR ne '291' and
 SDG2_3CHR ne '304' and SDG2_4CHR ne '2943' then ALL_BUT_SUBS = 1;

  if SDG3_2CHR in ('29','30','31') and SDG3_3CHR ne '303' and SDG3_3CHR ne '291' and
 SDG3_3CHR ne '304' and SDG3_4CHR ne '2943' then ALL_BUT_SUBS = 1;

** Final data, includes only persons having any some psychiatric diagnosis;

if ANYPSYC = 1;

 TULOVUOSI = substr (TUPVA,7,4)*1;
 TULOKK = substr (TUPVA,4,2)*1;
 TULOPP = substr (TUPVA,1,2)*1;

 LAHTOVUOSI = substr (LPVM,7,4)*1;
 LAHTOKK = substr (LPVM,4,2)*1;
 LAHTOPP = substr (LPVM,1,2)*1;

if TULOVUOSI <= 1986;

keep shnro TULOVUOSI TULOKK TULOPP LAHTOVUOSI SUBSTANCE ALL_BUT_SUBS;
rename shnro = ID;
run;

*** Data for years 1996-2022, ICD-10 codes;

data avohilmo;
 set lib100.avohilmot_kaikki;
 
 * First character of diagnosis;

 DG1_1CHR = substr (DG1,1,1);
 DG2_1CHR = substr (DG2,1,1);
 DG3_1CHR = substr (DG3,1,1);
 DG4_1CHR = substr (DG4,1,1);
 DG5_1CHR = substr (DG5,1,1);
 DG6_1CHR = substr (DG6,1,1);
 DG7_1CHR = substr (DG7,1,1);
 DG8_1CHR = substr (DG8,1,1);
 DG9_1CHR = substr (DG9,1,1);
 DG10_1CHR = substr (DG10,1,1);
 DG11_1CHR = substr (DG11,1,1);
 DG12_1CHR = substr (DG12,1,1);
 DG13_1CHR = substr (DG13,1,1);

 * First two characters of diagnosis;

 DG1_2CHR = substr (DG1,1,2);
 DG2_2CHR = substr (DG2,1,2);
 DG3_2CHR = substr (DG3,1,2);
 DG4_2CHR = substr (DG4,1,2);
 DG5_2CHR = substr (DG5,1,2);
 DG6_2CHR = substr (DG6,1,2);
 DG7_2CHR = substr (DG7,1,2);
 DG8_2CHR = substr (DG8,1,2);
 DG9_2CHR = substr (DG9,1,2);
 DG10_2CHR = substr (DG10,1,2);
 DG11_2CHR = substr (DG11,1,2);
 DG12_2CHR = substr (DG12,1,2);
 DG13_2CHR = substr (DG13,1,2);

 * First three characters of diagnosis;

 DG1_3CHR = substr (DG1,1,3);
 DG2_3CHR = substr (DG2,1,3);
 DG3_3CHR = substr (DG3,1,3);
 DG4_3CHR = substr (DG4,1,3);
 DG5_3CHR = substr (DG5,1,3);
 DG6_3CHR = substr (DG6,1,3);
 DG7_3CHR = substr (DG7,1,3);
 DG8_3CHR = substr (DG8,1,3);
 DG9_3CHR = substr (DG9,1,3);
 DG10_3CHR = substr (DG10,1,3);
 DG11_3CHR = substr (DG11,1,3);
 DG12_3CHR = substr (DG12,1,3);
 DG13_3CHR = substr (DG13,1,3);
 
 * First four characters of diagnosis;

 DG1_4CHR = substr (DG1,1,4);
 DG2_4CHR = substr (DG2,1,4);
 DG3_4CHR = substr (DG3,1,4);
 DG4_4CHR = substr (DG4,1,4);
 DG5_4CHR = substr (DG5,1,4);
 DG6_4CHR = substr (DG6,1,4);
 DG7_4CHR = substr (DG7,1,4);
 DG8_4CHR = substr (DG8,1,4);
 DG9_4CHR = substr (DG9,1,4);
 DG10_4CHR = substr (DG10,1,4);
 DG11_4CHR = substr (DG11,1,4);
 DG12_4CHR = substr (DG12,1,4);
 DG13_4CHR = substr (DG13,1,4);

*** Diagnosis groups;

** Any psychiatric disorder: F-code in ICD 10;

 ANYPSYC = 0;

 if DG1_1CHR = 'F' or DG2_1CHR = 'F' or
    DG3_1CHR = 'F' or DG4_1CHR = 'F' or
    DG5_1CHR = 'F' or DG6_1CHR = 'F' or
	DG7_1CHR = 'F' or DG8_1CHR = 'F' or
	DG9_1CHR = 'F' or DG10_1CHR = 'F' or
	DG11_1CHR = 'F' or DG12_1CHR = 'F' or
	DG13_1CHR = 'F' then ANYPSYC = 1;

** Alcohol related disorders: F10 code in ICD 10;

 ALCOHOL = 0;

 if DG1_3CHR = 'F10' or
    DG2_3CHR = 'F10' or
	DG3_3CHR = 'F10' or
	DG4_3CHR = 'F10' or
	DG5_3CHR = 'F10' or
	DG6_3CHR = 'F10' or
	DG7_3CHR = 'F10' or
	DG8_3CHR = 'F10' or
	DG9_3CHR = 'F10' or
	DG10_3CHR = 'F10' or
	DG11_3CHR = 'F10' or
	DG12_3CHR = 'F10' or
	DG13_3CHR = 'F10' then ALCOHOL = 1;

** Drug use disorders: F11-F16, F18-F19 (not F17, tobacco) codes in ICD 10;

 DRUG = 0;

 if DG1_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
    DG2_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG3_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG4_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG5_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG6_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG7_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG8_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG9_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG10_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG11_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG12_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') or
	DG13_3CHR in ('F11','F12','F13','F14','F15','F16','F18','F19') then DRUG = 1;

if ALCOHOL = 1 or DRUG = 1 then SUBSTANCE = 1;

** Other F-diagnosis;

if (DG1_2CHR ne 'F1' and DG1_2CHR ne '') or DG1_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG2_2CHR ne 'F1' and DG2_2CHR ne '') or DG2_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG3_2CHR ne 'F1' and DG3_2CHR ne '') or DG3_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG4_2CHR ne 'F1' and DG4_2CHR ne '') or DG4_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG5_2CHR ne 'F1' and DG5_2CHR ne '') or DG5_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG6_2CHR ne 'F1' and DG6_2CHR ne '') or DG6_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG7_2CHR ne 'F1' and DG7_2CHR ne '') or DG7_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG8_2CHR ne 'F1' and DG8_2CHR ne '') or DG8_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG9_2CHR ne 'F1' and DG9_2CHR ne '') or DG9_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG10_2CHR ne 'F1' and DG10_2CHR ne '') or DG10_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG11_2CHR ne 'F1' and DG11_2CHR ne '') or DG11_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG12_2CHR ne 'F1' and DG12_2CHR ne '') or DG12_3CHR = 'F17' then ALL_BUT_SUBS = 1;
if (DG13_2CHR ne 'F1' and DG13_2CHR ne '') or DG13_3CHR = 'F17' then ALL_BUT_SUBS = 1;

** Final data, includes only persons having any some psychiatric diagnosis;

if ANYPSYC = 1;

 TULOVUOSI = substr (kaynti_alkoi,7,4)*1;
 TULOKK = substr (kaynti_alkoi,4,2)*1;
 TULOPP = substr (kaynti_alkoi,1,2)*1;

 LAHTOVUOSI = substr (kaynti_loppui,7,4)*1;
 LAHTOKK = substr (kaynti_loppui,4,2)*1;
 LAHTOPP = substr (kaynti_loppui,1,2)*1;


 if TULOVUOSI >= 1996;

keep shnro TULOVUOSI TULOKK TULOPP LAHTOVUOSI SUBSTANCE ALL_BUT_SUBS;
rename shnro = ID;
run;

/* Combines data from all periods to one dataset */

data hilmoall;
 set hilmo1 hilmo2 hilmo3 hilmo4 avohilmo;

mukana = 1;

run;

** Separate datas for each diagnosis;

proc sort data = hilmoall;
 by ID TULOVUOSI;
run;

data SUBSTANCE ALL_BUT_SUBS;
 set hilmoall;

 if SUBSTANCE = 1 then output SUBSTANCE;
 if ALL_BUT_SUBS = 1 then output ALL_BUT_SUBS;

run;

* # of times each person has been in care for a given disease by each year;

proc means noprint data = SUBSTANCE;
 var mukana;
 by ID TULOVUOSI;
 output out = SUBSTANCEout (keep = ID TULOVUOSI NSUBSTANCE) n = NSUBSTANCE;
run;

proc means noprint data = ALL_BUT_SUBS;
 var mukana;
 by ID TULOVUOSI;
 output out = ALL_BUT_SUBSout (keep = ID TULOVUOSI NALL_BUT_SUBS) n = NALL_BUT_SUBS;
run;

* Creates a person-year matrix of having diagnosis;

data all;
 merge SUBSTANCEout ALL_BUT_SUBSout;
 by ID TULOVUOSI;

 ANYDIAG = 0;
 if NSUBSTANCE ^=. or NALL_BUT_SUBS ^=. then ANYDIAG = 1;

if id ^= lag(id) then id2+1;

 YEAR = TULOVUOSI;
 
run;

data all_id;
set all;
keep id id2;
run;

proc sort data = all_id;
by id2;
run;

data structure;
 set lib100.henkilovuodet_150324;

rename id = id2;

run;

data structure_2;
merge structure all_id;
by id2;

keep id year;

run;

data all2;
 merge structure_2 all;
 by ID YEAR;

 * Calculates the cumulative # of diagnoses up to a given year;

 if ID ^= lag (ID) then do; 
	
	CUM_NSUBSTANCE = 0; 
	CUM_NALL_BUT_SUBS = 0;
	CUM_ANYDIAG = 0;

 end;

CUM_NSUBSTANCE + NSUBSTANCE; 
CUM_NALL_BUT_SUBS + NALL_BUT_SUBS;
CUM_ANYDIAG + ANYDIAG;

run;

proc sort data = all2;
by ID YEAR;
run;

/* Saves the person-year matrix of data */

data lib100.personyear_avo_180324;
 set all2;

drop id2;

run;









