BookmarkSubscribeRSS Feed
Babloo
Rhodochrosite | Level 12

May I request someone to provide me an easy example to understand the macro functions like %str, %nrstr, %quote and %bquote? Although I read the documentation I've trouble understanding them.

 

Thanks!

8 REPLIES 8
LinusH
Tourmaline | Level 20

If there were an easy/short explanation for this, I'm pretty sure that should be in the documentation.

Read again, read various papers on the subject.

Use examples in doc, modify them and notice the behaviour.

Data never sleeps
data_null__
Jade | Level 19

I recall a really nice blog post from one of the SAS folks on this subject.  Does anyone remember that or am I having a senior moment?

Quentin
Super User

Perhaps @data_null__ you are referring to this blog post from Russ Tyndall.  I think it's an excellent resource. 

http://blogs.sas.com/content/sgf/2014/08/15/macro-quoting-made-easy/

 

For more detailed reviews, see UG such as:

Whitlock: http://www.lexjansen.com/nesug/nesug03/at/at012.pdf

Dunn: http://www.lexjansen.com/scsug/2010/Dunn_5/Macro_Quoting.pdf

 

The Boston Area SAS Users Group is hosting free webinars!
Next up: Lisa Mendez & Richann Watson present Get Tipsy with Debugging Tips for SAS® Code: The After Party on Wednesday Jul 16.
Register now at https://www.basug.org/events.
FreelanceReinh
Jade | Level 19

A former colleague of mine, Roland Rashleigh-Berry, has published a nice article on this subject: http://www.datasavantconsulting.com/roland/mquoting.html

I think you'll like it.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Just my opinion, but if your having to use these, then there is 95% chance of a better way of doing things using Base SAS programming, in fact other than 1% of extreme circumstances, almost everything can be done in Base SAS.

Ksharp
Super User

 Quickly  go through this . Detials check documentation.

 

%str, %nrstr  ,%quote ,%bquote are all used to mask the special characters (like blank comma ..) in a string.

 

%str, %nrstr  are generally for a STRING .

%str will resolve the macro variable if it is included in the STRING. and need add % before non-pair  '  " (  )  %    

%nrstr will NOT resolve the macro variable if it is included in the STRING. and need add % before non-pair  '  " (  )  %    

 

 

%quote ,%bquote are generally for a MACRO VARIABLE .

%quote is old version of %bquote . Now almost be replaced by %bquote .

 %bquote  unlike %quote  which need add % before non-pair  '  "  

Babloo
Rhodochrosite | Level 12

Thank you!

 

An simple example would be helpful.

Ksharp
Super User

OK.

 

 

%str, %nrstr  are generally for a STRING .

%str will resolve the macro variable if it is included in the STRING. and need add % before non-pair  '  " (  )  %    

1)
54 %let a=xxx;
55 %let b=%str(xxx&a.xxx);
56 %put &b ;
xxxxxxxxx

 

2)

54 %let a=xxx;
55 %let b=%str(xxx&a.x%'xx%"xx%%);
56 %put &b ;
xxxxxxx'xx"xx%
 
 

%nrstr will NOT resolve the macro variable if it is included in the STRING. and need add % before non-pair  '  " (  )  %    

1)

54 %let a=xxx;

55 %let b=%nrstr(xxx&a.x%'xx%"xx%%);
56 %put &b ;
xxx&a.x'xx"xx%

 

 

 

%quote ,%bquote are generally for a MACRO VARIABLE .

%quote is old version of %bquote . Now almost be replaced by %bquote .

 %bquote  unlike %quote  which need add % before non-pair  '  "  

%bquote will resolve the macro variable in it .

1)

54 %let a=xxx;
55 %let b=%bquote(xxx&a.x'xx"xx);
56 %put &b ;
xxxxxxx'xx"xx

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 8 replies
  • 2552 views
  • 2 likes
  • 7 in conversation