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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1251 views
  • 2 likes
  • 5 in conversation