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;
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.
When using CATX, the first parameter (not the second) should be the delimiter.
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.
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.
The log shows:
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.