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

Hi,

 

I have a table where the relation between two is known.

sarahzhou_1-1650818449374.png

 

I want to create a table for a couple related to their child records, for example:

sarahzhou_0-1650818060796.png

 

I've tried to duplicate the table then left join the id1's and id2's, however I couldn't get the correct result. 

 

code show as below:

 

data rf;
input p1$ p2$ rlf$;
cards;
f123 s234 son
s234 f123 father
m345 s234 son
s234 m345 mother
f123 m345 wife
m345 f123 husband
;

data rb;
input p1$ p2$ rlb$;
cards;
f123 s234 son
s234 f123 father
m345 s234 son
s234 m345 mother
f123 m345 wife
m345 f123 husband
;

proc sql;
select rf.p1 as id1, rf.rlf as name1, 
       rb.p2 as id2, rb.rlb as name2, 
       rb.p1 as id3, rb.rlb as name3 
from rf left join rb
on rf.p2=rb.p1
where (rlf in ("father", "mother") and rlb in ("son","daughter"));
quit;

 

I get the wrong result:

sarahzhou_3-1650819388151.png

 

 

How can I get the correct result as this?

sarahzhou_4-1650819460008.png

 

 

Kindly advise

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Why do you need duplicate results? Wouldn't this be more useful :

 

data rf;
input p1$ p2$ rlf$;
cards;
f123 s234 son
s234 f123 father
m345 s234 son
s234 m345 mother
f123 m345 wife
m345 f123 husband
;

proc sql;
/* create table cmf as */
select unique
    a.p2 as child,
    b.p2 as mother,
    c.p2 as father
from 
    rf as a left join
    rf as b on a.p2 = b.p1 left join
    rf as c on a.p2 = c.p1
where a.rlf in ("son","daughter") and b.rlf = "mother" and c.rlf = "father";
quit;

PGStats_0-1650828473336.png

Or maybe :

proc sql;
/* create table cmf as */
select unique
    a.p2 as child,
    a.rlf as rlf,
    b.p2 as mother,
    c.p2 as father
from 
    rf as a left join
    rf as b on a.p2 = b.p1 left join
    rf as c on a.p2 = c.p1
where a.rlf in ("son","daughter") and b.rlf = "mother" and c.rlf = "father";
quit;

PGStats_1-1650828626506.png

 

PG

View solution in original post

4 REPLIES 4
mkeintz
PROC Star

It's not exactly a family tree, is it?  It appears you only need to prepare for two generations.  Is that correct?

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
sarahzhou
Quartz | Level 8

yes, only two generations. thank you!

PGStats
Opal | Level 21

Why do you need duplicate results? Wouldn't this be more useful :

 

data rf;
input p1$ p2$ rlf$;
cards;
f123 s234 son
s234 f123 father
m345 s234 son
s234 m345 mother
f123 m345 wife
m345 f123 husband
;

proc sql;
/* create table cmf as */
select unique
    a.p2 as child,
    b.p2 as mother,
    c.p2 as father
from 
    rf as a left join
    rf as b on a.p2 = b.p1 left join
    rf as c on a.p2 = c.p1
where a.rlf in ("son","daughter") and b.rlf = "mother" and c.rlf = "father";
quit;

PGStats_0-1650828473336.png

Or maybe :

proc sql;
/* create table cmf as */
select unique
    a.p2 as child,
    a.rlf as rlf,
    b.p2 as mother,
    c.p2 as father
from 
    rf as a left join
    rf as b on a.p2 = b.p1 left join
    rf as c on a.p2 = c.p1
where a.rlf in ("son","daughter") and b.rlf = "mother" and c.rlf = "father";
quit;

PGStats_1-1650828626506.png

 

PG
PGStats
Opal | Level 21

But you could also simply transpose your actual data :

proc sort data=rf; by p1 rlf; run;

proc transpose data=rf out=cmf(drop=_name_);
where rlf in ("mother", "father", "uncle", "aunt");
by p1;
id rlf;
var p2;
run;
PG

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 639 views
  • 0 likes
  • 3 in conversation