I am attempting to concatenate all values in a column into one macro variable with some additional characters. Here is what I'm starting with.
data have;
input test_values :$2.;
datalines;
AB
BC
CD
DE
EF
;
What I would like is for the values to be stored in a macro with the following structure. Assuming we were storing into a macro variable named test and I ran
%put &test;
I would like to see the following:
'AB','BC','CD','DE','EF'
I have been trying to use some variation of the following to do it, but haven't been successful yet.
proc sql noprint;
select test_values into: test separated by ','
from have;
quit;
Let the QUOTE() function have all the fun.
proc sql noprint;
select quote(test_values,"'") into: test separated by ','
from have;
quit;
Let the QUOTE() function have all the fun.
proc sql noprint;
select quote(test_values,"'") into: test separated by ','
from have;
quit;
Depending on the defined length of the text variable you may need: quote(strip(test_values),"'") to avoid trailing blanks inside the quotes for any value not the full defined length of the variable.
You normally would not want to use STRIP(). That will remove both leading and trailing spaces. SAS comparisons normally ignore trailing spaces, but leadings spaces it considers meaningful. So use TRIM() instead.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.