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

/* Program creates additional demografic variables used in constructing the biasing score variable */ 

data demografinen_data;
set lib100.kaikki_perheet_data_250925;

mukana = 1;

keep id m_id f_id mukana;

run;

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

/* Demographics for executives */

data demografiset_exec;
 set lib100.demografiset_220524;

 vuosi_17v = birthyear + 17; 

 year = vuosi_17v;

keep id year vuosi_17v;

run;

data demografinen_data_2;
merge demografinen_data demografiset_exec;
by id;

if mukana = 1;

run;

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

/* Demografics for parents */

data demografiset;
 set lib100.demografiset_220524;

 if year >= 1987;
 
keep id year SOCIOECONOMIC_POSITION RESIDENTIALFORM age;

run;

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

data demografiset_2;
 set demografiset;

if id = lag(id) then LAG_SOCIOECONOMIC_POSITION = lag(SOCIOECONOMIC_POSITION);
if id = lag2(id) then LAG2_SOCIOECONOMIC_POSITION = lag2(SOCIOECONOMIC_POSITION);
if id = lag3(id) then LAG3_SOCIOECONOMIC_POSITION = lag3(SOCIOECONOMIC_POSITION);
if id = lag4(id) then LAG4_SOCIOECONOMIC_POSITION = lag4(SOCIOECONOMIC_POSITION);

if year = 1987 and id = lag3(id) then SOCIOECONOMIC_POSITION = LAG3_SOCIOECONOMIC_POSITION;
if year = 1988 and id = lag2(id) then SOCIOECONOMIC_POSITION = LAG2_SOCIOECONOMIC_POSITION;
if year = 1989 and id = lag(id) then SOCIOECONOMIC_POSITION = LAG_SOCIOECONOMIC_POSITION;

if year = 1991 and id = lag2(id) then SOCIOECONOMIC_POSITION = LAG2_SOCIOECONOMIC_POSITION;
if year = 1992 and id = lag(id) then SOCIOECONOMIC_POSITION = LAG_SOCIOECONOMIC_POSITION;

if year = 1994 and id = lag(id) then SOCIOECONOMIC_POSITION = LAG_SOCIOECONOMIC_POSITION;

if year = 1996 and id = lag4(id) then SOCIOECONOMIC_POSITION = LAG4_SOCIOECONOMIC_POSITION;
if year = 1997 and id = lag3(id) then SOCIOECONOMIC_POSITION = LAG3_SOCIOECONOMIC_POSITION;
if year = 1998 and id = lag2(id) then SOCIOECONOMIC_POSITION = LAG2_SOCIOECONOMIC_POSITION;
if year = 1999 and id = lag(id) then SOCIOECONOMIC_POSITION = LAG_SOCIOECONOMIC_POSITION;

if year = 2001 and id = lag3(id) then SOCIOECONOMIC_POSITION = LAG3_SOCIOECONOMIC_POSITION;
if year = 2002 and id = lag2(id) then SOCIOECONOMIC_POSITION = LAG2_SOCIOECONOMIC_POSITION;
if year = 2003 and id = lag(id) then SOCIOECONOMIC_POSITION = LAG_SOCIOECONOMIC_POSITION;

run;

/* Salary data */

data Tulodata;
set lib100.henkiloverotus_220925;

keep id year salarytotal totalincome capitaltotal;
run;

/* Separate data for fathers and mothers */

data aidit;
set demografinen_data_2;

if m_id ^= '';

keep m_id year mukana;

rename m_id = id;

run;

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


data isat;
set demografinen_data_2;

if f_id ^= '';

keep f_id year mukana;

rename f_id = id;

run;

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

/*----*/

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

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

/* Mothers */

data Aidit_2;
merge Aidit demografiset_2 tulodata;
by id year;

if mukana = . then delete;

rename id = m_id SOCIOECONOMIC_POSITION = m_SOCIOECONOMIC_POSITION RESIDENTIALFORM = m_RESIDENTIALFORM
salarytotal = m_salarytotal totalincome = m_totalincome capitaltotal = m_capitaltotal;

run;

/* Fathers */

data isat_2;
merge isat demografiset_2 tulodata;
by id year;

if mukana = . then delete;

rename id = f_id SOCIOECONOMIC_POSITION = f_SOCIOECONOMIC_POSITION RESIDENTIALFORM = f_RESIDENTIALFORM
salarytotal = f_salarytotal totalincome = f_totalincome capitaltotal = f_capitaltotal;

run;

/*---------*/

proc sort data = demografinen_data_2;
by m_id year;
run;

proc sort data = aidit_2;
by m_id year;
run;

data demografinen_data_3;
merge demografinen_data_2 aidit_2;
by m_id year;

run;

proc sort data = demografinen_data_3;
by f_id year;
run;

proc sort data = isat_2;
by f_id year;
run;

data demografinen_data_4;
merge demografinen_data_3 isat_2;
by f_id year;

if f_id = '' and m_id = '' then delete;

run;

proc sort nodupkey data = demografinen_data_4;
by id year;

run;

/*---*/

data demografinen_data_5;
set demografinen_data_4;

P_SALARYTOTAL = ((M_SALARYTOTAL + F_SALARYTOTAL) / 2);
P_TOTALINCOME = ((M_TOTALINCOME + F_TOTALINCOME) / 2);
P_CAPITALTOTAL = ((M_CAPITALTOTAL + F_CAPITALTOTAL) / 2);

if M_SALARYTOTAL = . then P_SALARYTOTAL = F_SALARYTOTAL;
if F_SALARYTOTAL = . then P_SALARYTOTAL = M_SALARYTOTAL;
if M_TOTALINCOME = . then P_TOTALINCOME = F_TOTALINCOME;
if F_TOTALINCOME = . then P_TOTALINCOME = M_TOTALINCOME;
if M_CAPITALTOTAL = . then P_CAPITALTOTAL = F_CAPITALTOTAL;
if F_CAPITALTOTAL = . then P_CAPITALTOTAL = M_CAPITALTOTAL;

Vuokra_17v = 0;
if 3 <= m_RESIDENTIALFORM <= 5 or 3 <= f_RESIDENTIALFORM <= 5 then Vuokra_17v = 1;

Isan_sose_asema_17v = 0;
if f_SOCIOECONOMIC_POSITION = 81 or f_SOCIOECONOMIC_POSITION = 91 then Isan_sose_asema_17v = 1;

run;

/* Income quintiles */

data vanh_tulot;
set lib100.kaikki_perheet_data_250925;

mukaan = 1;

keep m_id f_id mukaan;

run;

/* Separate data for fathers and mothers */

data aidit_tulot;
set vanh_tulot;

if m_id ^= '';

keep m_id mukaan;

rename m_id = id;

run;

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

data isat_tulot;
set vanh_tulot;

if f_id ^= '';

keep f_id mukaan;

rename f_id = id;

run;

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

/*----*/

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

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

/* Mothers */

data Aidit_tulot_2;
merge Aidit_tulot tulodata demografiset;
by id;

if mukaan = . then delete;
if age = . then delete;

rename id = m_id salarytotal = m_salarytotal totalincome = m_totalincome capitaltotal = m_capitaltotal;

drop age;

run;

/* Fathers */

data isat_tulot_2;
merge isat_tulot tulodata demografiset;
by id;

if mukaan = . then delete;
if age = . then delete;

rename id = f_id salarytotal = f_salarytotal totalincome = f_totalincome capitaltotal = f_capitaltotal;

drop age;

run;

/*---------*/

proc sort data = vanh_tulot;
by m_id;
run;

proc sort data = aidit_tulot_2;
by m_id;
run;

data vanh_tulot_2;
merge vanh_tulot aidit_tulot_2;
by m_id;

run;

proc sort data = vanh_tulot_2;
by f_id;
run;

proc sort data = isat_tulot_2;
by f_id;
run;

data vanh_tulot_3;
merge vanh_tulot_2 isat_tulot_2;
by f_id;

if f_id = '' and m_id = '' then delete;

if 1987 <= YEAR <= 2017;

run;

proc sort nodupkey data = vanh_tulot_3;
by m_id f_id year;

run;

/*---*/

data vanh_tulot_4;
set vanh_tulot_3;

P_SALARYTOTAL = ((M_SALARYTOTAL + F_SALARYTOTAL) / 2);
P_TOTALINCOME = ((M_TOTALINCOME + F_TOTALINCOME) / 2);
P_CAPITALTOTAL = ((M_CAPITALTOTAL + F_CAPITALTOTAL) / 2);

if M_SALARYTOTAL = . then P_SALARYTOTAL = F_SALARYTOTAL;
if F_SALARYTOTAL = . then P_SALARYTOTAL = M_SALARYTOTAL;
if M_TOTALINCOME = . then P_TOTALINCOME = F_TOTALINCOME;
if F_TOTALINCOME = . then P_TOTALINCOME = M_TOTALINCOME;
if M_CAPITALTOTAL = . then P_CAPITALTOTAL = F_CAPITALTOTAL;
if F_CAPITALTOTAL = . then P_CAPITALTOTAL = M_CAPITALTOTAL;

run;

proc sort data = vanh_tulot_4;
by year;

run;

proc univariate noprint data = vanh_tulot_4;
 by year;
 var P_TOTALINCOME;
 output out = Tulo_kvintiili pctlpts = 20 pctlpre = Tulot_;
run;

/*----*/

proc sort data = demografinen_data_5;
by year;

run;

data demografinen_data_6;
 merge demografinen_data_5 Tulo_kvintiili;
 by year;

Vanhempien_tulot_17v = 0;

if P_TOTALINCOME < Tulot_20 then Vanhempien_tulot_17v = 1;

run; 


/* Save data */

data lib100.Demog_lisamuuttujat_011025;
set demografinen_data_6;

keep id Vuokra_17v Isan_sose_asema_17v Vanhempien_tulot_17v;
run;




