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

Hello

I am using proc sql with into statement to create SAS Macro Varaible.

In this example I create a  Macro Varaible called nameP that should recieve value   "Peter","Paul"

What is the correct way to get it?

In the code that I wrote I get value Peter,Paul  and then there is an error 

******INTO Statement in PROC SQL to Create Macro Variables******;
 
data RawData;
length name $10;
input name $;
datalines;
Peter
John
Paul
David
;
run;

/*Solution with error*/
proc sql noprint;
select name
into :namesP separated by ','
from RawData
where substr(name,1,1) = 'P'
;
quit;
%put &namesP;/*Peter,Paul*/

data Outcome;
set RawData;
where name IN (&namesP.);
run;


/*Correct solution*/
%let names="Peter","Paul";
data Outcome;
set RawData;
where name IN (&names.);
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

If you need quotes, you have to set them:

data RawData;
length name $10;
input name $;
datalines;
Peter
John
Paul
David
;
run;

proc sql noprint;
select quote(trim(name))
into :namesP separated by ','
from RawData
where substr(name,1,1) = 'P'
;
quit;

%put &namesP;

Log excerpt:

46         %put &namesP;
"Peter","Paul"

View solution in original post

1 REPLY 1
Kurt_Bremser
Super User

If you need quotes, you have to set them:

data RawData;
length name $10;
input name $;
datalines;
Peter
John
Paul
David
;
run;

proc sql noprint;
select quote(trim(name))
into :namesP separated by ','
from RawData
where substr(name,1,1) = 'P'
;
quit;

%put &namesP;

Log excerpt:

46         %put &namesP;
"Peter","Paul"

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 883 views
  • 1 like
  • 2 in conversation