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

I am trying to take a list of text items associated with key values and concatenate them into semicolon-delimited lists.  Instead, I'm getting lists of semicolons only.  I can't figure out what is going wrong.  This is SAS 9.4 for Windows.

 

 

ODS HTML;
DATA HAVE;
     INPUT Key $ 1 Text $ 3-20;
     CARDS;
A Alpha One
A alpha two
A Alpha 3
B bee one
C See me
C See you
;;;
RUN;

PROC SORT DATA=Have;
     by Key;
run;

PROC PRINT;
     TITLE Have;
RUN;

DATA NotWhatIWant (KEEP=Key List);
     SET HAVE;
     BY Key;
     LENGTH List $50;
     RETAIN LIST;
     IF first.key THEN
          LIST = ' ';
     LIST = CATX(List,';',Text);
     IF LAST.KEY THEN
          OUTPUT NotWhatIWant;
RUN;

PROC PRINT DATA=NotWhatIWant;
     TITLE Not What I Want;
RUN;
ODS CLOSE;

what I want.PNG

 

 

 

 

 

 

not what I want.PNG

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You were nearly there, I update your code.  Do avoid coding all in uppercase or mixed case, its really hard to read.

proc sort data=have;
  by key;
run;

data notwhatiwant (keep=key list);
  set have;
  by key;
  length list $50;
  retain list;
  list=ifc(first.key,'',catx(';',list,text));
  if last.key;
run;

Note how I use ifc() boolean function to simplfy the if, and you do not need the output notwhatiwant.  

View solution in original post

4 REPLIES 4
LinusH
Tourmaline | Level 20
Are your sure that HAVE is looking good?
Use put statements or the data step debugger to analyze your variables during execution.
Data never sleeps
Astounding
PROC Star

When using CATX, the first parameter (not the second) should be the delimiter. 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

You were nearly there, I update your code.  Do avoid coding all in uppercase or mixed case, its really hard to read.

proc sort data=have;
  by key;
run;

data notwhatiwant (keep=key list);
  set have;
  by key;
  length list $50;
  retain list;
  list=ifc(first.key,'',catx(';',list,text));
  if last.key;
run;

Note how I use ifc() boolean function to simplfy the if, and you do not need the output notwhatiwant.  

juliet_scott
Fluorite | Level 6

The old-fashioned way of concatenating strings uses the operator “||”. This is still available, and still perfectly respectable. When using it, it is important to be aware of the lengths of the variables involved.

 

1.-Concatenating-Strings.jpg

 

The log shows:

 

2.png

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
  • 4 replies
  • 1588 views
  • 5 likes
  • 5 in conversation