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

 

BASUG is hosting free webinars Next up: Mike Raithel presenting on validating data files on Wednesday July 17. Register now at the Boston Area SAS Users Group event page: 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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 8 replies
  • 1497 views
  • 2 likes
  • 7 in conversation