/* Program identifies CEOs from position data 1 */

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

* Reads position data for CEOs;

data aaa_ceo;
 set lib100.positions;						
 if ASEMA = 'TJ';
run;

proc sort nodupkey data = aaa_ceo;		
 by ID YEAR FIRM ROOLIN_LOPPU_PVM; 
run;

* Identify CEO changes during the fiscal year and keep those CEOs who are CEOs at the end of each fiscal year; 

proc sort data = aaa_ceo;
 by FIRM YEAR descending ROOLIN_ALKU_PVM; 
run;

data aaa_ceo2;
 set aaa_ceo;
 DELETE = 0;
 if FIRM = lag(FIRM) and YEAR = lag(YEAR) then DELETE = 1;
 if DELETE = 1 then delete;
 drop DELETE;
run;

proc sort nodupkey data = aaa_ceo2;
 by FIRM YEAR; 
run;

* Reads the position data for board chairs. If the company has no designeted CEO, the Limited Liability Companies Act stipulates that the board serves in the CEO role. Board chair represents the board;

data aaa_chair;
 set lib100.positions;						
 if ASEMA = 'PJ';
run;

proc sort nodupkey data = aaa_chair;		
 by ID YEAR FIRM ROOLIN_LOPPU_PVM;  
run;

* Identify board chair changes during the fiscal year and keep those chairs who are chairs at the end of each fiscal year; 

proc sort data = aaa_chair;
 by FIRM YEAR descending ROOLIN_ALKU_PVM;  
run;

data aaa_chair2;
 set aaa_chair;
 DELETE = 0;
 if FIRM = lag(FIRM) and YEAR = lag(YEAR) then DELETE = 1;
 if DELETE = 1 then delete;
 drop DELETE;
run;

proc sort nodupkey data = aaa_chair2;
 by FIRM YEAR; 
run;

* Merge CEO and board chair data; 

data combine;
 set aaa_ceo2 aaa_chair2;
run;

proc sort data = combine;
 by FIRM YEAR;
run; 

proc sort nodupkey data = combine;	
 by FIRM YEAR ASEMA; 
run;

* Remove board chairs when the firm has a designated CEO;

proc sort data = combine;
 by FIRM YEAR descending ASEMA;
run;

data combine2;
 set combine;
 REMOVE = 0;
 if FIRM = lag(FIRM) and year = lag(YEAR) and ASEMA ne lag(ASEMA) then REMOVE = 1;		
 if REMOVE = 1 then delete;
 drop REMOVE;	
run; 

proc sort nodupkey data = combine2;	
 by FIRM YEAR;
run;

* Saves data;

data lib100.CEOs;
 set combine2;
run;
