BookmarkSubscribeRSS Feed
Astounding
PROC Star

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;

JHE
Obsidian | Level 7 JHE
Obsidian | Level 7

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

 

 

 

Astounding
PROC Star

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);

JHE
Obsidian | Level 7 JHE
Obsidian | Level 7

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;

JHE
Obsidian | Level 7 JHE
Obsidian | Level 7

This result:

 

0100

MHSC,MHSC1

0400

DEHG

0500

MSDD,HENH

Astounding
PROC Star

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.

Reeza
Super User

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

 

 

 


 

JHE
Obsidian | Level 7 JHE
Obsidian | Level 7

great ! thank you so much. I am a SQL person, SAS is powerful.

 

 

 

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 22 replies
  • 1724 views
  • 5 likes
  • 5 in conversation