BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Bill_S
Calcite | Level 5

Hi All,

I have 2 data sets for parents and child, like this:

Set A

ID        Mother_ID   Father_ID

1              .                1          

2             2                .

3             .                 .

Set B

ID    X  

1     a        

2     b      

3     c    

Question:

How can I merge two data sets to get a data set like this ( 3 is a child):

ID      X    X_Mother    X_Father

3       c       b                   a

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
KachiM
Rhodochrosite | Level 12

data a;

input id mid fid;

datalines;

1 .  1

2 2  .

3 .  .

;

run;

data b;

input id X $1.;

datalines;

1 a

2 b

3 c

;

run;

Get the ID for Mother(m) and Father(f)

data have;

retain m f;

   set a end = eof;

   if mid then m = _N_;

   else if fid then f = _N_;

   if eof then output;

keep m f;

run;

Use m and f to name the Variables, X_Mother and X_Father respectively.

data want;

   if _n_ = 1 then set have;

   set b end = last;

      retain X_Mother X_Father;

      if id = m then X_Mother = X;

      else if id = f then X_Father = X;

   if last and X = 'c';

keep X:;

run;

View solution in original post

3 REPLIES 3
RaviKommuri
Fluorite | Level 6

Hi,

Where is the actual relation between Child and Parents -  in which dataset?

KachiM
Rhodochrosite | Level 12

data a;

input id mid fid;

datalines;

1 .  1

2 2  .

3 .  .

;

run;

data b;

input id X $1.;

datalines;

1 a

2 b

3 c

;

run;

Get the ID for Mother(m) and Father(f)

data have;

retain m f;

   set a end = eof;

   if mid then m = _N_;

   else if fid then f = _N_;

   if eof then output;

keep m f;

run;

Use m and f to name the Variables, X_Mother and X_Father respectively.

data want;

   if _n_ = 1 then set have;

   set b end = last;

      retain X_Mother X_Father;

      if id = m then X_Mother = X;

      else if id = f then X_Father = X;

   if last and X = 'c';

keep X:;

run;

Bill_S
Calcite | Level 5

Dear datasp - this works. Thanks a lot!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 498 views
  • 0 likes
  • 3 in conversation