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

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 380 views
  • 3 likes
  • 3 in conversation