<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Odd use of %STR() at execution time in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Odd-use-of-STR-at-execution-time/m-p/463526#M118102</link>
    <description>&lt;P&gt;That is just what I want to say .&lt;/P&gt;</description>
    <pubDate>Sat, 19 May 2018 13:46:01 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2018-05-19T13:46:01Z</dc:date>
    <item>
      <title>Odd use of %STR() at execution time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Odd-use-of-STR-at-execution-time/m-p/463424#M118067</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Over on Stack Overflow there was a routine question about how to macro quote a value like OR, and one of the comments surprised me. &lt;A href="https://stackoverflow.com/questions/50395483/how-to-mask-or-with-variable-list-passed-through-using-syspbuff-in-macro" target="_blank"&gt;https://stackoverflow.com/questions/50395483/how-to-mask-or-with-variable-list-passed-through-using-syspbuff-in-macro&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A simple macro like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro ck(state) ;
  %if &amp;amp;state=RI %then %put Rhode Island! ;
  %else %put Not Rhode Island ;
%mend ck ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Will choke given a value like OR or 1+.2, because they are seen by the implied %EVAL as operators:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;100 %ck(OR)
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric
operand is required. The condition was: &amp;amp;state=RI
ERROR: The macro CK will stop executing.

101 %ck(1+.2)
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric
operand is required. The condition was: &amp;amp;state=RI
ERROR: The macro CK will stop executing.

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My answer would be to use a macro execution time quoting function, e.g. %superq(state) or %bquote(&amp;amp;state) to quote the resolved value of STATE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The surprise to me is that apparently %STR(&amp;amp;state) will work (?) for the value of OR:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;110 %macro ck(state) ;
111 %if %str(&amp;amp;state)=RI %then %put Rhode Island! ;
112 %else %put Not Rhode Island ;
113 %mend ck ;
114
115 %ck(OR)
Not Rhode Island

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would have expected that to fail, since %str() works at macro compile time, I would think the OR would still be seen during macro execution time, and would cause problems for %EVAL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Happily, %STR() is not enough to mask 1+.2 , so it still errors:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;116 %ck(1+.2)
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric
operand is required. The condition was: &amp;amp;state=RI
ERROR: The macro CK will stop executing.

&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;And happily, %SUPERQ can handle all those scenarios, so I'll definitely keep using %superq and %bquote for execution-time quoting. But I'm curious why %STR(&amp;amp;state) seems to be enough to mask OR.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind Regards,&lt;BR /&gt;--Q.&lt;/P&gt;</description>
      <pubDate>Fri, 18 May 2018 19:32:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Odd-use-of-STR-at-execution-time/m-p/463424#M118067</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2018-05-18T19:32:35Z</dc:date>
    </item>
    <item>
      <title>Re: Odd use of %STR() at execution time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Odd-use-of-STR-at-execution-time/m-p/463431#M118070</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Odd use of %STR() at execution time beats all odds of SAS documenatation. Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;Sir for sharing and helping folks like me learn something new.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 18 May 2018 20:17:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Odd-use-of-STR-at-execution-time/m-p/463431#M118070</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-05-18T20:17:23Z</dc:date>
    </item>
    <item>
      <title>Re: Odd use of %STR() at execution time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Odd-use-of-STR-at-execution-time/m-p/463516#M118098</link>
      <description>&lt;P&gt;I am not sure I correctly understood it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Compile stage is before execution stage, then mask a macro variable would not be a problem .&lt;/P&gt;
&lt;P&gt;i.e. the mask character(delta) is still there. when in execution stage the macro variable is still be masked .&lt;/P&gt;</description>
      <pubDate>Sat, 19 May 2018 13:08:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Odd-use-of-STR-at-execution-time/m-p/463516#M118098</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-05-19T13:08:53Z</dc:date>
    </item>
    <item>
      <title>Re: Odd use of %STR() at execution time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Odd-use-of-STR-at-execution-time/m-p/463518#M118099</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I am not sure I correctly understood it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Compile stage is before execution stage, then mask a macro variable would not be a problem .&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since %STR() works at macro compile time, it cannot quote a value that results from resolving a macro variable. Because it works before the macro variable has resolved.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So below, the first comparison succeeds because %STR() masks the + operator.&amp;nbsp; The second comparison errors because %STR() can't mask the + operator that results from resolving MVAR because it doesn't see it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test(dummy) ;
  %if %str(1+A)=%str(1+A) %then %put 1+A=1+A ;

  %local mvar ;
  %let mvar=1+B ;
  %if %str(&amp;amp;mvar)=%str(1+B) %then %put 1+B=1+B ;
%mend ;

%test()&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The surprise to me was that %STR() can effectively mask OR&amp;nbsp; which results from resolving MVAR.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test(dummy) ;
  %if %str(1 OR A)=%str(1 OR A) %then %put 1+A=1+A ;

  %local mvar ;
  %let mvar=1 OR B ;
  %if %str(&amp;amp;mvar)=%str(1 OR B) %then %put 1 OR B=1 OR B ;
%mend ;

%test()&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think I understand it now, will add a separate post with my explanation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 19 May 2018 13:18:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Odd-use-of-STR-at-execution-time/m-p/463518#M118099</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2018-05-19T13:18:29Z</dc:date>
    </item>
    <item>
      <title>Re: Odd use of %STR() at execution time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Odd-use-of-STR-at-execution-time/m-p/463524#M118100</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Interesting thing is if there is not =-/* ,the code would work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test(dummy) ;
  %if %str(1+A)=%str(1+A) %then %put 1+A=1+A ;

  %local mvar ;
  %let mvar=&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;1BB&lt;/STRONG&gt;&lt;/FONT&gt; ;
  %if %str(&amp;amp;mvar)=%str(1+B) %then %put 1+B=1+B ;
%mend ;

%test()&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It looks like SAS add %eval() where string has '+' .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if there is a '+' ,then code would look like&amp;nbsp; %if %str(%eval(1+b))&amp;nbsp; --&amp;gt;&amp;nbsp; %if&amp;nbsp; delta %eval() delta&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if there is not a '+'&amp;nbsp;&lt;SPAN&gt;then code would look like&amp;nbsp; %if %str(1bb)&amp;nbsp; --&amp;gt;&amp;nbsp; %if&amp;nbsp; delta 1bb delta&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;so this should work .&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 19 May 2018 13:33:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Odd-use-of-STR-at-execution-time/m-p/463524#M118100</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-05-19T13:33:24Z</dc:date>
    </item>
    <item>
      <title>Re: Odd use of %STR() at execution time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Odd-use-of-STR-at-execution-time/m-p/463525#M118101</link>
      <description>&lt;P&gt;Given a macro that uses %STR() to attempt execution-time macro quoting:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro ck(state) ;
  %if %str(&amp;amp;state)=RI %then %put Rhode Island! ;
  %else %put Not Rhode Island ;
%mend ck ;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Why does %ck(OR) work? That is, why does %STR() suffice to mask the resolved OR operator?&lt;BR /&gt;2. Given that %ck(OR) works, why does %ck(|) not work? Why doesn't %STR() mask the resolved | operator?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think I understand it after re-reading what I consider to be the trinity of macro quoting papers:&lt;BR /&gt;&lt;A href="https://www.lexjansen.com/nesug/nesug99/bt/bt185.pdf" target="_blank"&gt;https://www.lexjansen.com/nesug/nesug99/bt/bt185.pdf&lt;/A&gt;&lt;BR /&gt;&lt;A href="https://www.lexjansen.com/nesug/nesug03/at/at012.pdf" target="_blank"&gt;https://www.lexjansen.com/nesug/nesug03/at/at012.pdf&lt;/A&gt;&lt;BR /&gt;&lt;A href="https://analytics.ncsu.edu/sesug/2008/CS-049.pdf" target="_blank"&gt;https://analytics.ncsu.edu/sesug/2008/CS-049.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%STR() works at macro compile time. %STR(SOME ARGUMENT) adds an unprintable delta character before SOME ARGUMENT, an unprintable delta character after SOME ARGUMENT, and looks in the argument for a list of symbols that need to be masked&amp;nbsp;by converting them to delta characters. The list includes ; | + - = etc. Since %STR() works at compile time, it cannot mask any symbols that result from resolving a macro reference. because it won't see them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So why does %STR(&amp;amp;state) work when state resolves to OR? It's because even though %STR never sees the OR token, it still adds the delta characters before and after the OR. And it turns out that when %STR() macro quotes an OR, it doesn't actually convert OR into delta characters.&amp;nbsp; Adding the delta characters to the beginning and end is enough.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you run:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test(dummy) ;
  %let OR=%str(0 OR 1) ;
  %let pipe=%str(0|1) ; 
  %let plus=%str(0+1) ;
  %put _local_ ;
%mend ck ;
%test()

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;The %PUT _local_ allows you to see some of the delta characters added. This shows that in order to mask OR, %str() does not actually convert the OR token to masked characters. The delta characters added at the beginning and end of the string are enough to mask the OR. But for the pipe operator and + operator, when %STR masks them it converts them to delta characters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think that explains why %ck(OR) works but %ck(|) and %ck(1+.1) still error.&amp;nbsp; %STR() doesn't actually have to see the OR in order to mask it, because adding a delta character to the front and end is enough. But for | and + to be masked, %STR() needs to see them, so that they can be converted to a delta character. Since %STR can't see them (when they result from resolving a macro variable), it can't mask them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sounds reasonable?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 19 May 2018 13:39:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Odd-use-of-STR-at-execution-time/m-p/463525#M118101</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2018-05-19T13:39:03Z</dc:date>
    </item>
    <item>
      <title>Re: Odd use of %STR() at execution time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Odd-use-of-STR-at-execution-time/m-p/463526#M118102</link>
      <description>&lt;P&gt;That is just what I want to say .&lt;/P&gt;</description>
      <pubDate>Sat, 19 May 2018 13:46:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Odd-use-of-STR-at-execution-time/m-p/463526#M118102</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-05-19T13:46:01Z</dc:date>
    </item>
    <item>
      <title>Re: Odd use of %STR() at execution time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Odd-use-of-STR-at-execution-time/m-p/463527#M118103</link>
      <description>&lt;P&gt;The %IF statement always uses %EVAL to evaluate the expression.&amp;nbsp; I added a separate answer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think the short answer is that %str(&amp;amp;mvar) can "work" to mask OR because of the way %STR() quotes OR, which is different than the way %STR() quotes + or |.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%STR() quotes OR by adding a delta character at the beginning and end of the quoted string.&lt;/P&gt;
&lt;P&gt;%STR() quotes + and | by adding a delta character&amp;nbsp;&lt;SPAN&gt;at the beginning and end of the quoted string, and converting the quoted symbols into delta characters.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Since %STR(OR) doesn't convert OR into delta characters, I think its a side effect that %STR(&amp;amp;mvar) works when MVAR resolves to OR.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 19 May 2018 13:50:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Odd-use-of-STR-at-execution-time/m-p/463527#M118103</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2018-05-19T13:50:10Z</dc:date>
    </item>
  </channel>
</rss>

