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.

BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
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-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
  • 5 replies
  • 576 views
  • 3 likes
  • 5 in conversation