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 (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 13906 views
  • 0 likes
  • 5 in conversation