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

Hi,

 

I have a dataset as below 

 

data REL_DATA;
input ENUM $ RELATED_ENUM $ ROLE $;
datalines;
D1234 R1234 84
D1234 R1234 85
D1234 L2345 90
D1234 P1234 81
D1234 N4567 90
D1234 N4567 88
;
run;

 

I want the output as below 

ENUM RELATED_NUM  ROLES
D1234 R1234                  84,85
D1234 L2345                  90
D1234 P1234                  81
D1234 N4567                 90,88

Basically if the Related_num is repeated for a different role, i want it to be merged to one row with the roles displayed in the ROLES column

 

Thank you.  

1 ACCEPTED SOLUTION

Accepted Solutions
tarheel13
Rhodochrosite | Level 12

If it was me, I would sort and transpose it and then concatenate all the values of role after transposing.

data REL_DATA;
input ENUM $ RELATED_ENUM $ ROLE $;
datalines;
D1234 R1234 84
D1234 R1234 85
D1234 L2345 90
D1234 P1234 81
D1234 N4567 90
D1234 N4567 88
;
run;

proc sort data=rel_data;
	by enum related_enum;
run;

proc transpose data=rel_data out=rel_data_t(drop=_name_);
	by enum related_enum;
	var role;
run;

data rel_data_t2;
	set rel_data_t;
	roles=catx(',',of col:);
run;

View solution in original post

4 REPLIES 4
tarheel13
Rhodochrosite | Level 12

If it was me, I would sort and transpose it and then concatenate all the values of role after transposing.

data REL_DATA;
input ENUM $ RELATED_ENUM $ ROLE $;
datalines;
D1234 R1234 84
D1234 R1234 85
D1234 L2345 90
D1234 P1234 81
D1234 N4567 90
D1234 N4567 88
;
run;

proc sort data=rel_data;
	by enum related_enum;
run;

proc transpose data=rel_data out=rel_data_t(drop=_name_);
	by enum related_enum;
	var role;
run;

data rel_data_t2;
	set rel_data_t;
	roles=catx(',',of col:);
run;
Anuz
Quartz | Level 8

@tarheel13  Thanks a ton... It did work for what I am trying to do ... 🙂 

Reeza
Super User
There are two different standard methods to do that, both are illustrated here.

https://gist.github.com/statgeek/d583cfa992bf56da51d435165b07e96a
Anuz
Quartz | Level 8

@Reeza  Thank you very much . It helped... Appreciate it. 

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
  • 4 replies
  • 994 views
  • 3 likes
  • 3 in conversation