BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Nietzsche
Lapis Lazuli | Level 10

hello, learning about masking special tokens in Macro Variables in the Advanced prep guide 4e. pg 304.

Nietzsche_0-1679103501072.png

 

When I reference the macro variable with %put &prog; it shows the special token ";" in the log.

Nietzsche_1-1679103595337.png

 

But if I use %put _user_; or %put _all_;   special token ";" is missing.

Nietzsche_2-1679103673133.png

 

why is that?

SAS Base Programming (2022 Dec), Preparing for SAS Advanced Programming (Cancelled).
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Because that is how it works.

 

If you look carefully at the output of %PUT _USER_ you can see the special bytes that indicate the macro quoting.

Tom_0-1679108330504.png

And if you look at how it is surfaced by SASHELP.VMACRO you can see it also.

2045  data _null_;
2046    set sashelp.vmacro;
2047    where name='XX';
2048    put value / value :$hex36. ;
2049  run;

data one run
0164617461206F6E650E2072756E0E022020
NOTE: There were 1 observations read from the data set SASHELP.VMACRO.
      WHERE name='XX';

View solution in original post

5 REPLIES 5
Nietzsche
Lapis Lazuli | Level 10

okay, just read about %bquote, which seems to be so much easier than %str.

is there any point in using %str over %bquote? 

Nietzsche_0-1679104986883.png

 

SAS Base Programming (2022 Dec), Preparing for SAS Advanced Programming (Cancelled).
PaigeMiller
Diamond | Level 26

@Nietzsche wrote:

okay, just read about %bquote, which seems to be so much easier than %str.

is there any point in using %str over %bquote? 


 

%bquote and %str do different things. They are not interchangeable.

 

In simple terms, %str() masks special characters that you can see in the argument. For example, if you have a plus sign in the argument, you would need %str. You could use %str(Dan+Shay), you can see the + sign in the argument to the %str() function, and so the %str() function will treat the plus sign as text and not as addition; and quoting functions will not help here.

 

%bquote masks special characters that you cannot see in a macro variable. If a macro variable &A contains values with a plus sign, then the %str(&A) will not work, you cannot see the plus sign; you will need %bquote(&a) to prevent the plus sign from being interpreted as addition.

 

--
Paige Miller
Quentin
Super User

@Nietzsche wrote:

okay, just read about %bquote, which seems to be so much easier than %str.

is there any point in using %str over %bquote? '

This is a good question, but I suggest you make it a separate question.  It's deserving of its own thread.  The differences between %BQUOTE and %STR become clearer in the the context of a macro.  In open code (outside of a macro definition), there are people who use %BQUOTE in situations where %STR could be used.  But inside a macro definition, %BQUOTE and %STR do different things.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
yabwon
Onyx | Level 15

I recommend you this article by Susan O'Connor about macro quoting:

https://stats.oarc.ucla.edu/wp-content/uploads/2016/02/bt185.pdf

it's only 12 pages, so reads fast, but explains everything perfectly, especially differences between %str() and %bquote().

I'm sure @Quentin will agree on that.

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

Because that is how it works.

 

If you look carefully at the output of %PUT _USER_ you can see the special bytes that indicate the macro quoting.

Tom_0-1679108330504.png

And if you look at how it is surfaced by SASHELP.VMACRO you can see it also.

2045  data _null_;
2046    set sashelp.vmacro;
2047    where name='XX';
2048    put value / value :$hex36. ;
2049  run;

data one run
0164617461206F6E650E2072756E0E022020
NOTE: There were 1 observations read from the data set SASHELP.VMACRO.
      WHERE name='XX';

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 5 replies
  • 997 views
  • 3 likes
  • 5 in conversation