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:
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.
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.