BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
West26
Obsidian | Level 7

Hello,

Struggling with the below. Basically I shouldn't be having multiple rows of same id. At the same time, their corresponding values of 'Desc' filed, has to be combined.

 

Data have:
ID        Desc

1234    AB
5678    CD
5678     EF
9999     GH

 

Data want:
ID       Desc
1234    AB
5678   CD_EF
9999    GH

1 ACCEPTED SOLUTION

Accepted Solutions
HB
Barite | Level 11 HB
Barite | Level 11

 

 

data have;
input ID DESC $2.;
datalines;
1234 AB
5678 CD
5678 EF
9999 GH
run;

data want(keep=id list);
  do until (last.id);
    set have;
    by id;
    length list $30;    
    list = catx('_',list,desc);
  end;
run;

gives

 

ID list
1234 AB
5678 CD_EF
9999 GH

 

Not my code.

https://communities.sas.com/t5/SAS-Programming/How-to-create-a-comma-separated-String-from-values-fr...

 

View solution in original post

3 REPLIES 3
HB
Barite | Level 11 HB
Barite | Level 11

 

 

data have;
input ID DESC $2.;
datalines;
1234 AB
5678 CD
5678 EF
9999 GH
run;

data want(keep=id list);
  do until (last.id);
    set have;
    by id;
    length list $30;    
    list = catx('_',list,desc);
  end;
run;

gives

 

ID list
1234 AB
5678 CD_EF
9999 GH

 

Not my code.

https://communities.sas.com/t5/SAS-Programming/How-to-create-a-comma-separated-String-from-values-fr...

 

West26
Obsidian | Level 7

Hello HB,

Thanks for taking your time and providing the solution. Appreciated.

nikhilwagh
Obsidian | Level 7

Just another way --

data have;
input ID DESC $2.;
datalines;
1234 AB
5678 CD
5678 EF
9999 GH
run;

proc transpose data=have out=have_t(drop=_:);
by id;
var DESC;
run;

data want(drop=col:);
set have_t;
desc=catx("_", of col:);
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 890 views
  • 1 like
  • 3 in conversation