BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
jffeudo86
Quartz | Level 8

Hello everyone,

How do I pass a quoted delimited argument to a macro?  Below is a sample code to show the issue:

 

%macro generate_data(ds, list);

proc sql;

create table &ds. as

select * from some_table

where code in (&list.);

quit;

%mend generate_data;

 

%generate_data(test, "'a', 'b', 'c'");

 

 

How do I pass/format the 2nd argument 'a', 'b', 'c'? if I pass it without the double quotes at either ends, it will be treated as more than 1 argument.  Basically the proc sql in side the macro would resolve to:

 

proc sql;

create table test as

select * from some_table

where code in ('a', 'b', 'c');

quit;

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The easiest way is to not type the commas. 

The IN operator does not care if you use commas or spaces in the list of values.

%generate_data(test, 'a' 'b' 'c');

If you use actual quotes then remove them when generating the code.

where code in (%sysfunc(dequote(&list.)));

You could remove the parentheses from the macro code and add them in the call.

...
where code in &list.;
...
%generate_data(test, ('a','b' 'c'));

You could use macro quoting in the call.

%generate_data(test, %str('a','b' 'c'));

 

 

View solution in original post

3 REPLIES 3
yabwon
Amethyst | Level 16
%macro generate_data(ds, list);
proc sql;
create table &ds. as
select * from some_table
where code in &list.; /* <- drop () */
quit;
%mend generate_data;

%generate_data(test, ('a', 'b', 'c')) /* <- add() */
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

The easiest way is to not type the commas. 

The IN operator does not care if you use commas or spaces in the list of values.

%generate_data(test, 'a' 'b' 'c');

If you use actual quotes then remove them when generating the code.

where code in (%sysfunc(dequote(&list.)));

You could remove the parentheses from the macro code and add them in the call.

...
where code in &list.;
...
%generate_data(test, ('a','b' 'c'));

You could use macro quoting in the call.

%generate_data(test, %str('a','b' 'c'));

 

 

jffeudo86
Quartz | Level 8
Thank you!

I tried your second suggestion just right after I posted my question:
.. where code in &list.;
..
%generate_data(test, (‘a’, ‘b’, ‘c’));

I learned more from your reply such as the us of %str and %dequote

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 3 replies
  • 823 views
  • 2 likes
  • 3 in conversation