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

I have tables with these structure, two IDs and a score.

 

 

id_A,id_B,score_01

id_A,id_B,score_02

id_A,id_B,score_03

id_A,id_B,score_04

id_A,id_B,score_0n

 

What's the most efficient way to combine so that i have a structure

id_A,id_B,score_01,score_02,score_03,score_04,score_0n?

 

Not that the combination of id_A and id_B may not be existing in all datasets.

 

Thank you very much!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

If you want something more efficient than your code, post your code.

You can try a data step merge.

 

data want;
merge data1 (rename=score=score1)
           data2 (rename=score=score2)
           ....
           data6 (rename=score=score6);
by idA idB;


run;

View solution in original post

5 REPLIES 5
Reeza
Super User

Do you have multiple records per IDs? Is it a one to one join, or many to many join? 

milts
Pyrite | Level 9

For each dataset, there is only one instance of the combination of an id_A and id_B.

 

Currently I'm using full outer join but I think there's a more efficient way.

Reeza
Super User

If you want something more efficient than your code, post your code.

You can try a data step merge.

 

data want;
merge data1 (rename=score=score1)
           data2 (rename=score=score2)
           ....
           data6 (rename=score=score6);
by idA idB;


run;
milts
Pyrite | Level 9

Thanks for the idea. My code previously was like using full outer join like these

 

select coalesce(a.id_A,b.id_A,....j.id_A) as id_A,
select coalesce(a.id_B,b.id_B,....j.id_B) as id_B,
score_1,...score_10

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

As always in these type of questions, my first response is, is the strcuture you require the best fit from a programming perspective.  The answer here is no.  Personally I would set these together and get one long table with each row being a separate result.  This simplfies any coding you need to do across the variables, and seeing as the strcuture wouldn't change if the number of items changed (i.e. increase or decrease) then the maintenance of such code also becomes far easier.  I find transposed data is only useful in output reports.

 

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
  • 5 replies
  • 1494 views
  • 1 like
  • 3 in conversation