BookmarkSubscribeRSS Feed
SAS_inquisitive
Lapis Lazuli | Level 10

 Hello,

 

When calling macro which macro quoting function should be used macro parameters?  In this example  both %nrstr and %nrbquote give the same result. Thanks !

 


%macro m (par);
   data a;
     x = "&par";
   run;
%mend m;
%m(%nrstr(A|B))

%macro m (par);
   data b;
     x = "&par";
   run;
%mend m;
%m(%nrbquote(A|B))
3 REPLIES 3
Astounding
PROC Star

For this particular example, the answer is "none of the above".  This macro call should generate the same result:

 

%m(A|B)

 

So the first question should be whether macro quoting is necessary at all.  In this example, it isn't.  In general (in case your actual application is more complex than the one you have shown), the idea is that macro quoting removes special significance from characters that would impact the interpretation of the program.  Take the case where your macro defines a single parameter:

 

%macro m (varname);

 

Now this would be an illegal call to the macro since the comma is a significant character that identifies when one macro parameter ends and a new one begins:

 

%m (A, B)

 

To get "A, B" to be interpreted as the value of a single macro parameter:

 

%m (%str(A, B))

 

The %STR function removes the special significance of the comma, and treats it as text instead.  Suppose the situation were more complex:

 

%let list = A, B;

%m (&list)

 

Now the comma doesn't even appear in the macro call.  However, resolving &LIST generates a comma which causes the same problem as before.  That's when you need a macro function that quotes later on, as the macro executes:

 

%m (%bquote(&list))

 

The differences between %str and %nrstr (and between %bquote and %nrbquote) are only a matter of which characters are impacted by quoting.  The NR in the function name quotes % and & in addition to all the other characters quoted by %str.

Ksharp
Super User

I would pick up %nrstr()   %nrbquote() is generally for a macro variable .

SAS_inquisitive
Lapis Lazuli | Level 10
I have inherited programs that have used %nrbquote by default whether there is macro triggers (&, %) or not. So I was wondering %nrstr can be replaced by %nrbquote in macro call at all conditions.

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