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

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(&degStudLevComma), degStudLev);

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

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".

%macro test(value, var2 😃;

  
%let value=&&&value;
     %put &value;
%mend;
%let variableToBeUpdated =;
%let x=a,b,c;
%test(x,var2=variableToBeUpdated)

23         %macro test(value, var2 =);
24        
25            %let value=&&&value;
26              %put &value;
27         %mend;
28         %let variableToBeUpdated =;
29         %let x=a,b,c;
30         %test(x,var2=variableToBeUpdated)
a,b,c

%macro test(value, var2 😃;
    
%put &value;
%mend;
%let variableToBeUpdated =;
%let x=a,b,c;
%test((&x),var2=variableToBeUpdated)

23         %macro test(value, var2 =);
24              %put &value;
25         %mend;
26         %let variableToBeUpdated =;
27         %let x=a,b,c;
28         %test((&x),var2=variableToBeUpdated)
(a,b,c)



View solution in original post

7 REPLIES 7
DavidPhillips2
Rhodochrosite | Level 12

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))

DavidPhillips2
Rhodochrosite | Level 12

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)

ballardw
Super User

The second variable must be referenced as var2=variableToBeUpdated since you defined it as a keyword parameter not positional.

%test(%bquote(&x), var2=variableToBeUpdated)

data_null__
Jade | Level 19

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".

%macro test(value, var2 😃;

  
%let value=&&&value;
     %put &value;
%mend;
%let variableToBeUpdated =;
%let x=a,b,c;
%test(x,var2=variableToBeUpdated)

23         %macro test(value, var2 =);
24        
25            %let value=&&&value;
26              %put &value;
27         %mend;
28         %let variableToBeUpdated =;
29         %let x=a,b,c;
30         %test(x,var2=variableToBeUpdated)
a,b,c

%macro test(value, var2 😃;
    
%put &value;
%mend;
%let variableToBeUpdated =;
%let x=a,b,c;
%test((&x),var2=variableToBeUpdated)

23         %macro test(value, var2 =);
24              %put &value;
25         %mend;
26         %let variableToBeUpdated =;
27         %let x=a,b,c;
28         %test((&x),var2=variableToBeUpdated)
(a,b,c)



RW9
Diamond | Level 26 RW9
Diamond | Level 26

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(&param. ne);

     %put &param.;

     %let num=%eval(&num. + 1);

     %let param=%scan(&syspbuff,&num);

   %end;

%mend test;

%let x=a,b,c;

%test (&x.);

Quentin
Super User

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().

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
DavidPhillips2
Rhodochrosite | Level 12

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();

SAS Innovate 2025: Register Now

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!

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
  • 7 replies
  • 14059 views
  • 0 likes
  • 5 in conversation