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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 293 views
  • 5 likes
  • 4 in conversation