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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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