/* Reads data on co-residing social 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_s1 = '' and shnro_s2 = '' then delete;

keep shnro shnro_s1 shnro_s2;

rename shnro = id;

run;

data vanhemmat;
set lib4.u0599_kohdj_vanhemmat_s;

if shnro_s1 = '' and shnro_s2 = '' then delete;

keep shnro shnro_s1 shnro_s2;

rename shnro = 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 mukana = 1;

if shnro_s1 = '' and shnro_s2 = '' then delete;

run;

data kaikki_3;
set kaikki_2;

if shnro_s1 ^= '' then shnro_f = shnro_s1;
if shnro_s2 ^= '' then shnro_m = shnro_s2;

keep id mukana shnro_m shnro_f;

rename shnro_m = m_id shnro_f = f_id;

run;

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

/* Saves data */

data lib100.sijaisperheet_250925;
set kaikki_3;
run;
