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. 

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