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;
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"
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"
April 27 – 30 | Gaylord Texan | Grapevine, Texas
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!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.