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

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;
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Let the QUOTE() function have all the fun.

 

proc sql noprint;
select quote(test_values,"'") into: test separated by ','
from have;
quit;
--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

Let the QUOTE() function have all the fun.

 

proc sql noprint;
select quote(test_values,"'") into: test separated by ','
from have;
quit;
--
Paige Miller
ballardw
Super User

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.

 

Tom
Super User Tom
Super User

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.

Peter_C
Rhodochrosite | Level 12
alternatively
select test_values format= $quote200. into: test separated by ','

deals with trailing spaces as required and highlights that we can apply useful formats when SELECT-ing INTO :macrovars

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1655 views
  • 2 likes
  • 5 in conversation