BookmarkSubscribeRSS Feed
Sathya3
Obsidian | Level 7

Can someone please explain difference between bquote and str in sas with a working example.I am aware of theoretical difference between them.

But ,I want to understand with example which shows why str can't be used in place of bquote

3 REPLIES 3
PaigeMiller
Diamond | Level 26

https://documentation.sas.com/doc/en/mcrolref/1.0/p1780jrqrtwtw7n16x83peo2zpxr.htm, which says:

 

%QUOTE and %NRQUOTE mask the same items as %STR and %NRSTR, respectively. However, %STR and %NRSTR mask constant text instead of a resolved value. And, %STR and %NRSTR work when a macro compiles, and %QUOTE and %NRQUOTE work when a macro executes.

Constant text is text which will not change. Resolved values are inside a macro variable, and could change when the value of the macro variable changes.

 

Also:

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

 

which says "If you can see the problem, it is a compile issue (use %str or %nrstr); otherwise, it is execution time (use %bquote or %superq)". Please see the examples.

--
Paige Miller
BrunoMueller
SAS Super FREQ

Take this program

%let unknownContent = a,b,c;
%put NOTE: word 2 is %scan(%str(&unknownContent), 2, %str(,)); 
%put NOTE: word 2 is %scan(%bquote(&unknownContent), 2, %str(,));

As general rule, if you can see the "difficult" characters in your code use %STR as in the example of the %SCAN function third argument, the "," is a "difficult" character so we use %STR so it is not treated as part of the code, but just as a "normal" comma.

 

If you can not see the "difficult" characters in your code as in the second %SCAN example use %BQUOTE.

 

For a more detailed explanation check this blog post https://blogs.sas.com/content/sgf/2014/08/15/macro-quoting-made-easy/

 

ballardw
Super User

For what very little it may be worth, If I find myself looking for the difference between any of the macro quoting functions the next step is to rethink the problem as I may be trying to "fix" something that would be better off not existing in the first place.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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
  • 3 replies
  • 1236 views
  • 5 likes
  • 4 in conversation