SAS Programming

DATA Step, Macro, Functions and more
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;

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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