/* Reads education data */

libname lib24 "W:\Uudet_datat\U0599_9_toim\";
libname lib100 "W:\Tutkimusprojektit\Kirjanpito\Dokumentaatio\JAR\Datat\";
run;

data education;
 set lib24.demografiset_9_s;
 ID = shnro;								
 YEAR = vuosi;
 DEGREE_CODE = ututku; 							* Highest/latest degree awarded;
 DEGREE_YEAR = suorv; 							* Year of highest degree awarded;
 if DEGREE_CODE = ' ' then delete;				
 keep ID YEAR DEGREE_CODE DEGREE_YEAR;
run;

proc sort nodupkey data = education;
 by ID YEAR;
run;

* Education level;

data education_2;							
 set education;

 DEGREE_CODE_1_DIGIT = substr(DEGREE_CODE, 1, 1);			* First number indicates the level of education;
 
 EDULEVEL = .;
 if DEGREE_CODE_1_DIGIT = '3' then EDULEVEL = 1;			* Keskiaste (Lowest education level);	
 if DEGREE_CODE_1_DIGIT = '5' then EDULEVEL = 2;			* Alin korkea-aste;
 if DEGREE_CODE_1_DIGIT = '6' then EDULEVEL = 3;			* Alempi korkeakouluaste;
 if DEGREE_CODE_1_DIGIT = '7' then EDULEVEL = 4;			* Ylempi korkeakouluaste;
 if DEGREE_CODE_1_DIGIT = '8' then EDULEVEL = 5;			* Tutkijakouluaste (Highest education level);
 if EDULEVEL =. then EDULEVEL = 0;
run;

* Education field;

data education_3;							
 set education_2;

 DEGREE_CODE_LAST_4 = substr(DEGREE_CODE, 3, 4);            * Last 4 digits;

 DEGREE_CODE_2_DIGIT = substr(DEGREE_CODE, 1, 2);			* Second (or third) number indicates the field;

 DEGREE_CODE_3_DIGIT = substr(DEGREE_CODE, 1, 3);

 TEACHERDEG = 0;											* Teacher education;
 if DEGREE_CODE_2_DIGIT = '31' then TEACHERDEG = 1;
 if DEGREE_CODE_2_DIGIT = '51' then TEACHERDEG = 1;
 if DEGREE_CODE_2_DIGIT = '61' then TEACHERDEG = 1;
 if DEGREE_CODE_2_DIGIT = '71' then TEACHERDEG = 1;
 if DEGREE_CODE_2_DIGIT = '81' then TEACHERDEG = 1;
 if DEGREE_CODE_2_DIGIT = '91' then TEACHERDEG = 1;
 if DEGREE_CODE = ' ' then TEACHERDEG = .;

 HUMANITY_ART_DEG = 0;										* Humanistic or arts education;
 if DEGREE_CODE_2_DIGIT = '32' then HUMANITY_ART_DEG = 1;
 if DEGREE_CODE_2_DIGIT = '52' then HUMANITY_ART_DEG = 1;
 if DEGREE_CODE_2_DIGIT = '62' then HUMANITY_ART_DEG = 1;
 if DEGREE_CODE_2_DIGIT = '72' then HUMANITY_ART_DEG = 1;
 if DEGREE_CODE_2_DIGIT = '82' then HUMANITY_ART_DEG = 1;
 if DEGREE_CODE_2_DIGIT = '92' then HUMANITY_ART_DEG = 1;
 if DEGREE_CODE = ' ' then HUMANITY_ART_DEG = .;

 BUSINESS_SOCIALDEG = 0;									* Business or social sciences degree;
 if (DEGREE_CODE_3_DIGIT = '331' or DEGREE_CODE_3_DIGIT = '334' or DEGREE_CODE_3_DIGIT = '337' or DEGREE_CODE_3_DIGIT = '339' or DEGREE_CODE_3_DIGIT = '531'
 or DEGREE_CODE_3_DIGIT = '539' or DEGREE_CODE_3_DIGIT = '631' or DEGREE_CODE_3_DIGIT = '632' or DEGREE_CODE_3_DIGIT = '633' or DEGREE_CODE_3_DIGIT = '639'
 or DEGREE_CODE_3_DIGIT = '731' or DEGREE_CODE_3_DIGIT = '732' or DEGREE_CODE_3_DIGIT = '733' or DEGREE_CODE_3_DIGIT = '739' or DEGREE_CODE_3_DIGIT = '832'
 or DEGREE_CODE_3_DIGIT = '833' or DEGREE_CODE_3_DIGIT = '835' or DEGREE_CODE_3_DIGIT = '836' or DEGREE_CODE_3_DIGIT = '839' or DEGREE_CODE_2_DIGIT = '93')
 then BUSINESS_SOCIALDEG = 1;
 if DEGREE_CODE = ' ' then BUSINESS_SOCIALDEG = .;

 LAWDEG = 0;												* Law degree;
 if (DEGREE_CODE_3_DIGIT = '634' or DEGREE_CODE_3_DIGIT = '734' or DEGREE_CODE_3_DIGIT = '834' or DEGREE_CODE_3_DIGIT = '837') then LAWDEG = 1;
 if DEGREE_CODE = ' ' then LAWDEG = .;

 NATURAL_SCIENCEDEG = 0;									* Natural sciences or agriculture/forestry degree;
 if DEGREE_CODE_2_DIGIT = '34' then NATURAL_SCIENCEDEG = 1;	* Natural sciences;
 if DEGREE_CODE_2_DIGIT = '54' then NATURAL_SCIENCEDEG = 1;	
 if DEGREE_CODE_2_DIGIT = '64' then NATURAL_SCIENCEDEG = 1;	
 if DEGREE_CODE_2_DIGIT = '74' then NATURAL_SCIENCEDEG = 1;	
 if DEGREE_CODE_2_DIGIT = '84' then NATURAL_SCIENCEDEG = 1;	
 if DEGREE_CODE_2_DIGIT = '94' then NATURAL_SCIENCEDEG = 1;	
 if DEGREE_CODE_2_DIGIT = '36' then NATURAL_SCIENCEDEG = 1;	* Agriculture or forestry;
 if DEGREE_CODE_2_DIGIT = '56' then NATURAL_SCIENCEDEG = 1;	
 if DEGREE_CODE_2_DIGIT = '66' then NATURAL_SCIENCEDEG = 1;	
 if DEGREE_CODE_2_DIGIT = '76' then NATURAL_SCIENCEDEG = 1;	
 if DEGREE_CODE_2_DIGIT = '86' then NATURAL_SCIENCEDEG = 1;	
 if DEGREE_CODE_2_DIGIT = '96' then NATURAL_SCIENCEDEG = 1;	
 if DEGREE_CODE = ' ' then NATURAL_SCIENCEDEG = .;

 ENGINEERINGDEG = 0;										* Engineering degree;
 if DEGREE_CODE_2_DIGIT = '35' then ENGINEERINGDEG = 1;
 if DEGREE_CODE_2_DIGIT = '55' then ENGINEERINGDEG = 1;
 if DEGREE_CODE_2_DIGIT = '65' then ENGINEERINGDEG = 1;
 if DEGREE_CODE_2_DIGIT = '75' then ENGINEERINGDEG = 1;
 if DEGREE_CODE_2_DIGIT = '85' then ENGINEERINGDEG = 1;
 if DEGREE_CODE_2_DIGIT = '95' then ENGINEERINGDEG = 1;
 if DEGREE_CODE = ' ' then ENGINEERINGDEG = .;

 HEALTH_MEDICINEDEG = 0;									* Health or medicine degree;
 if DEGREE_CODE_2_DIGIT = '37' then HEALTH_MEDICINEDEG = 1;
 if DEGREE_CODE_2_DIGIT = '57' then HEALTH_MEDICINEDEG = 1;
 if DEGREE_CODE_2_DIGIT = '67' then HEALTH_MEDICINEDEG = 1;
 if DEGREE_CODE_2_DIGIT = '77' then HEALTH_MEDICINEDEG = 1;
 if DEGREE_CODE_2_DIGIT = '87' then HEALTH_MEDICINEDEG = 1;
 if DEGREE_CODE_2_DIGIT = '97' then HEALTH_MEDICINEDEG = 1;
 if DEGREE_CODE = ' ' then HEALTH_MEDICINEDEG = .; 

 SERVICEDEG = 0;
 if DEGREE_CODE_2_DIGIT = '38' then  SERVICEDEG = 1;		* Service degree;
 if DEGREE_CODE_2_DIGIT = '58' then  SERVICEDEG = 1;	
 if DEGREE_CODE_2_DIGIT = '68' then  SERVICEDEG = 1;	
 if DEGREE_CODE_2_DIGIT = '78' then  SERVICEDEG = 1;	
 if DEGREE_CODE_2_DIGIT = '88' then  SERVICEDEG = 1;	
 if DEGREE_CODE_2_DIGIT = '98' then  SERVICEDEG = 1;
 if DEGREE_CODE = ' ' then SERVICEDEG = .;	

 OTHERDEG = 0;
 if DEGREE_CODE_2_DIGIT = '39' then  OTHERDEG = 1;			* Other/unknown degree;
 if DEGREE_CODE_2_DIGIT = '59' then  OTHERDEG = 1;
 if DEGREE_CODE_2_DIGIT = '69' then  OTHERDEG = 1;
 if DEGREE_CODE_2_DIGIT = '79' then  OTHERDEG = 1;
 if DEGREE_CODE_2_DIGIT = '89' then  OTHERDEG = 1;
 if DEGREE_CODE_2_DIGIT = '99' then  OTHERDEG= 1;
 if DEGREE_CODE = ' ' then OTHERDEG = .;

 HIGHERDEG = 0;												* Higher degree, Licenciate or Phd degree;
 if (DEGREE_CODE_2_DIGIT = '81' or DEGREE_CODE_2_DIGIT = '82' or DEGREE_CODE_2_DIGIT = '83' or DEGREE_CODE_2_DIGIT = '84' or DEGREE_CODE_2_DIGIT = '85'
 or DEGREE_CODE_2_DIGIT = '86' or DEGREE_CODE_2_DIGIT = '87' or DEGREE_CODE_2_DIGIT = '88' or DEGREE_CODE_2_DIGIT = '89') then HIGHERDEG = 1;
 if DEGREE_CODE = ' ' then HIGHERDEG = .;

BUSINESSDEG = 0;
 if (DEGREE_CODE_1_DIGIT = '6' or DEGREE_CODE_1_DIGIT = '7' or DEGREE_CODE_1_DIGIT = '8') and BUSINESS_SOCIALDEG = 1 then BUSINESSDEG = 1;

run;

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

* Saves data;

data lib100.education_170524;
 set education_3;
 keep ID YEAR DEGREE_CODE DEGREE_YEAR EDULEVEL BUSINESS_SOCIALDEG BUSINESSDEG;
run;
