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;
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'));
%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() */
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'));
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.