How can I pass a macro variable that has commas into a macro? I tried using %quote and oddly it does not work.
%Global degStudLev;
%Macro convertCommaListToSpaced(commaList, spacedList =);
%Mend;
%let degStudLevComma = D1,D2,D3;
%convertCommaListToSpaced(%quote(°StudLevComma), degStudLev);
Another way to mask a comma in a macro parameter value is to simply use parenthesis around the value. The problem is that the parenthesis become part of the value.
Since you have the value in symbol X another way would be to pass the symbol name and let the macro resolve it using the proper "protection".
I found this. Bquote should work. Not sure how to unquote this when using bquote.
http://support.sas.com/kb/31/012.html
/*example 1*/
%macro test(value);
%put &value;
%mend;
%test(%str(a,b,c))
In example 2, an execution-time function is needed. %BQUOTE can be used to mask the commas in the resolved value of the macro variable X.
/*example 2*/
%macro test(value);
%put &value;
%mend;
%let x=a,b,c;
%test(%bquote(&x))
When I try to alter a variable by reference it breaks. "more positional parameters found than defined."
%macro test(value, var2 =);
%put &value;
%mend;
%let variableToBeUpdated =;
%let x=a,b,c;
%test(%bquote(&x), variableToBeUpdated)
The second variable must be referenced as var2=variableToBeUpdated since you defined it as a keyword parameter not positional.
%test(%bquote(&x), var2=variableToBeUpdated)
Another way to mask a comma in a macro parameter value is to simply use parenthesis around the value. The problem is that the parenthesis become part of the value.
Since you have the value in symbol X another way would be to pass the symbol name and let the macro resolve it using the proper "protection".
Got to say, I really don't see why you would want to do this. There are numerous other alternatives - keeping parameters in a dataset for instance, or %sysfunc(tranwrd()). Also, if you have a list of comma separated data and want a list of space why not use parmbuff?
%macro test/parmbuff;
%let num=1;
%let param=%scan(&syspbuff,&num);
%do %while(¶m. ne);
%put ¶m.;
%let num=%eval(&num. + 1);
%let param=%scan(&syspbuff,&num);
%end;
%mend test;
%let x=a,b,c;
%test (&x.);
Agree with what you found, %BQUOTE() should work. %QUOTE() is old and I believe deprecated. %SUPERQ() is another alternative, if you want to mask & or %.
In theory, the macro processor should unquote values for you automatically before returning them to SAS. But sometimes it doesn't work, and it is necessary to unquote with %UNQUOTE(). The rule I learned was: if the SAS code in the MPRINT log looks correct but you still get an error message, try %UNQUOTE().
After passing in variableToBeUpdated to be used as var2; I learned that I can set variableToBeUpdated in the macro by using
%let &var2 = the cat ran;
How can I set something like
%let &var2 = the cat ran;
%let &var2 = &var2 and jumped;
I found that using the &&& worked
%Global variableToBeUpdated;
%macro test(value, var2 =);
%let &var2 = &&&var2 Test Part2;
%mend;
%let variableToBeUpdated = Test Part 1;
%let x=a,b,c;
%test((&x), var2 =variableToBeUpdated);
%macro displayStuff();
%put &variableToBeUpdated;
%mend;
%displayStuff();
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.