/* Reads data on biological parents */

libname lib4 "W:\uudet_datat\u0599_al4";
libname lib100 "W:\Tutkimusprojektit\Kirjanpito\Dokumentaatio\JAR\Datat\";

run;

data tutkimushenkilot;

set lib100.tutkimushenkilot_id_year_250925;

mukana = 1;

keep id year mukana;

run;

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


data lapset;
set lib4.u0599_kohdj_laps_s;

if shnro_m = '' and shnro_f = '' then delete;

keep shnro shnro_m shnro_f;

rename shnro = id shnro_m = m_id shnro_f = f_id;

run;

data vanhemmat;
set lib4.u0599_kohdj_vanhemmat_s;

if shnro_m = '' and shnro_f = '' then delete;

keep shnro shnro_m shnro_f;

rename shnro = id shnro_m = m_id shnro_f = f_id;


run;

data kaikki;
set lapset vanhemmat;
run;


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


data kaikki_2;
merge tutkimushenkilot kaikki;
by id;

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

if mukana = 1;

drop year;

run;

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

/* Saves data */

data lib100.perheet_250925;
set kaikki_2;
run;
