hello, learning about masking special tokens in Macro Variables in the Advanced prep guide 4e. pg 304.
When I reference the macro variable with %put &prog; it shows the special token ";" in the log.
But if I use %put _user_; or %put _all_; special token ";" is missing.
why is that?
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.
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';
okay, just read about %bquote, which seems to be so much easier than %str.
is there any point in using %str over %bquote?
@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.
@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.
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
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.
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';
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!
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.
Ready to level-up your skills? Choose your own adventure.