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.

 

 

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 901 views
  • 0 likes
  • 2 in conversation