You could use a simple method:
data want;
set have;
by I;
length combo $ 1000;
if first.i then combo = variable;
else combo = catx(', ', combo, variable);
if last.i;
drop variable;
rename combo = variable;
run;
data C;
input LOB $ Code $;
cards;
0100 MHSC
0100 MHSC1
0500 MSHD
0500 HENH
0400 DEHG
;
proc sort data=C;
by lob;
run;
DATA OUTC;
SET C;
BY LOB;
LENGTH TYPE $100;
IF FIRST.LOB THEN TYPE=CODE;
ELSE TYPE =CATX(',',TYPE);
IF LAST.LOB;
DROP Code;
RUN;
What I should modify , in order to have result as this :
100 | MHSC,MHSC1 |
400 | DEHG |
500 | MSDD,HENH |
Looks like I forgot a RETAIN statement. For your program, it would be:
retain type;
It can go anywhere in the DATA step.
Also, CATX needs an additional piece:
else type = catx(',' , type , code);
DATA OUTC;
SET C;
BY LOB;
LENGTH TYPE $100;
IF FIRST.LOB THEN TYPE=CODE;
ELSE TYPE =CATX(',',type,CODE);
retain type;
IF LAST.LOB;
drop Code;
RUN;
This result:
0100 | MHSC,MHSC1 |
0400 | DEHG |
0500 | MSDD,HENH |
Why should MSDD be part of the result for 500? It's not part of your original data.
At any rate, if the program doesn't give you what you are looking for, show what you got, what you want instead, and the log.
This works
data C;
input LOB $ Code $;
cards;
0100 MHSC
0100 MHSC1
0500 MSHD
0500 HENH
0400 DEHG
;
proc sort data=C;
by lob;
run;
proc transpose data=c out=ct prefix=_code;
by lob;
var code;
run;
data want;
set ct;
combined = catx(", ", of _code:);
run;
proc print data=want;
run;
@JHE wrote:
data C;
input LOB $ Code $;
cards;
0100 MHSC
0100 MHSC1
0500 MSHD
0500 HENH
0400 DEHG
;
proc sort data=C;
by lob;
run;
DATA OUTC;
SET C;
BY LOB;
LENGTH TYPE $100;
IF FIRST.LOB THEN TYPE=CODE;
ELSE TYPE =CATX(',',TYPE);
IF LAST.LOB;
DROP Code;
RUN;
What I should modify , in order to have result as this :
100
MHSC,MHSC1
400
DEHG
500
MSDD,HENH
great ! thank you so much. I am a SQL person, SAS is powerful.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.