/* Program creates initial sample */

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

* Financial statement data;

data acc; 
set lib100.tilinpaatokset_kaikki_080622;
run;

data acc_2;
 set acc;
	
 if TPLV >= 100000 and TASEX >= 100000;			* Requires sales and assets to be greater than 100 000 EUR;
	
 if OMAVAR > 100 then delete;					* Removes likely data errors;
 if opo < -150 or opo > 150 then delete;		

 if year > 1993;

keep firm year tplv tasex omavar kopo opo;

rename TPLV = LV TASEX = TASE OMAVAR = OMAVARAISUUS kopo = ROA opo = ROE;
 
run;

* Position data;

data positiot;
set lib100.positions;

keep id firm asema year;

run;

data positiot_a;
set lib100.positions_asiakastieto_070824;

if 2016 <= year <= 2019;
if asema = 'Varsinainen jäsen' or asema = 'Puheenjohtaja' or asema = 'Toimitusjohtaja';


if asema = 'Varsinainen jäsen' then asema = 'J';
if asema = 'Puheenjohtaja' then asema = 'PJ';
if asema = 'Toimitusjohtaja' then asema = 'TJ';

keep id firm asema year;

run;

data positiot_2;
set positiot positiot_a;
run;

proc sort nodupkey data = positiot_2;
by id firm year asema;
run;

* Demografic data;

data demografiset;
 set lib100.demografiset_220524;

 if year >= 1987;

 keep id year MALE birthyear;

run;

* Combines accounting data to position data;

proc sort data = positiot_2;
by firm year;
run;

proc sort data = acc_2;
by firm year;
run;

data acc_positiot;
merge acc_2 positiot_2;
by firm year;

if firm = '' then delete;
if id = '' then delete;
if LV = . then delete;
if omavaraisuus = . then delete;

run;

* Combines demographic data;

proc sort data = demografiset;
by id year;
run;

proc sort data = acc_positiot;
by id year;
run;


data kirjanpitodata;
merge acc_positiot demografiset;
by id year;

if male = . then delete;
if birthyear = . then delete;

if firm = '' then delete;
if id = '' then delete;
if LV = . then delete;
if omavaraisuus = . then delete;

run;

* Initial sample;

data tutkimushenkilot;
set kirjanpitodata;

keep id year;

run;

proc sort nodupkey data = tutkimushenkilot;
by id year;
run;

data lib100.Tutkimushenkilot_id_year_250925;
set tutkimushenkilot;
run;

proc sort nodupkey data = tutkimushenkilot;
by id;
run;

* Saves data;

data lib100.Tutkimushenkilot_id_250925;
set tutkimushenkilot;
keep id;
run;



