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

Hello, 

 

I need to merge the DATASET 1  with DATASET 2  to get to a final dataset "DATA WANT" and I need some guidance.

please note that dataset 1 is a matched pair data of students and dataset 2  contains additional variables on the matched pairs data.

 

DATASET 1

groupA_idgroupB_idgroupA_gradegroupB_gradegroupA_sexgroupB_sex
00000-00000100000-0002002211
00000-00000100000-0002012211
00000-00000100000-0002022211
00000-00000200000-0002032211
00000-00000200000-0002042211
00000-00000200000-0002052211
00000-00000300000-0002062211
00000-00000300000-0002072211
00000-00000300000-0002082211
 

 
DATASET 2
 
idSiblingseconomic status
00000-000001Yeslow
00000-000001yeslow
00000-000001yeslow
00000-000002yeslow
00000-000002yeslow
00000-000002yeslow
00000-000003nohigh
00000-000003nohigh
00000-000003nohigh
00000-000200yeslow
00000-000201nolow
00000-000202nohigh
00000-000203nomedium
00000-000204nohigh
00000-000205yeslow
00000-000206nomedium
00000-000207yeshigh
00000-000208yeshigh
 

DATA WANT

 

idgradesexSiblingseconomic status
00000-00000121Yeslow
00000-00000221yeslow
00000-00000321nohigh
00000-00000221yeslow
00000-00020021yeslow
00000-00020121nolow
00000-00020221nohigh
00000-00020321nomedium
00000-00020421nohigh
00000-00020521yeslow
00000-00020621nomedium
00000-00020721yeshigh
00000-00020821yeshigh

 

Any help would be appreciated. Thanks

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

If I understand the problem correctly, this should work:

 

data dataset1_revised;

set dataset1;

id = groupA_id;

grade = groupA_grade;

sex = groupA_sex;

output;

id = groupB_id;

grade = groupB_grade;

sex = groupB_sex;

output;

keep id grade sex;

run;

 

The rest becomes a more commonplace sort + merge:

 

proc sort data=dataset1_revised nodupkey;

   by id;

run;

 

proc sort data=dataset2 out=dataset2_revised nodupkey;

   by id;

run;

 

data want;

   merge dataset1_revised dataset2_revised;

   by id;

run;

 

 

View solution in original post

2 REPLIES 2
Astounding
PROC Star

If I understand the problem correctly, this should work:

 

data dataset1_revised;

set dataset1;

id = groupA_id;

grade = groupA_grade;

sex = groupA_sex;

output;

id = groupB_id;

grade = groupB_grade;

sex = groupB_sex;

output;

keep id grade sex;

run;

 

The rest becomes a more commonplace sort + merge:

 

proc sort data=dataset1_revised nodupkey;

   by id;

run;

 

proc sort data=dataset2 out=dataset2_revised nodupkey;

   by id;

run;

 

data want;

   merge dataset1_revised dataset2_revised;

   by id;

run;

 

 

Archibald2
Calcite | Level 5

Many thanks Astounding!

This is what I wanted to accomplish. Note- I added a keep statement for dataset1_revised since the last statement did work only for dataset2_revised.

 

 

 

 

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
  • 2 replies
  • 540 views
  • 0 likes
  • 2 in conversation