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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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