<?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: Why does a macro parameter inside %EVAL continue to loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Why-does-a-macro-parameter-inside-EVAL-continue-to-loop/m-p/440085#M282495</link>
    <description>Thanks. But why it would loop like that?</description>
    <pubDate>Sun, 25 Feb 2018 22:40:27 GMT</pubDate>
    <dc:creator>LL5</dc:creator>
    <dc:date>2018-02-25T22:40:27Z</dc:date>
    <item>
      <title>Why does a macro parameter inside %EVAL continue to loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-does-a-macro-parameter-inside-EVAL-continue-to-loop/m-p/440077#M282493</link>
      <description>&lt;P&gt;Hi All, I am trying to understand why %MYSUM(4) resolved to 10 - see codes and log below.&lt;/P&gt;&lt;P&gt;My understanding would be &amp;amp;N is 4, the inside %MYSUM macro resolved to 3, so the answer would be 4 + 3 = 7.&lt;/P&gt;&lt;P&gt;However, according to the log, it seems the macro continues to loop until the condition of &amp;amp;N &amp;gt; 1 is false.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone share some thoughts and give some advises?&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;OPTIONS&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;MLOGIC&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; ;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;%MACRO&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; MYSUM(N);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%IF&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; &amp;amp;N &amp;gt;&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%THEN&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%EVAL&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;(&amp;amp;N+ %&lt;STRONG&gt;&lt;I&gt;MYSUM&lt;/I&gt;&lt;/STRONG&gt;(&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%EVAL&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;(&amp;amp;N-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;)));&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%ELSE&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; &amp;amp;N;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;%MEND&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%PUT&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; %MYSUM(4);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%MACRO MYSUM(N);&lt;/P&gt;&lt;P&gt;26 %IF &amp;amp;N &amp;gt;1 %THEN %EVAL(&amp;amp;N+ %MYSUM(%EVAL(&amp;amp;N-1)));&lt;/P&gt;&lt;P&gt;27 %ELSE &amp;amp;N;&lt;/P&gt;&lt;P&gt;28 %MEND;&lt;/P&gt;&lt;P&gt;29&lt;/P&gt;&lt;P&gt;30 %PUT %MYSUM(4);&lt;/P&gt;&lt;P&gt;MLOGIC(MYSUM): Beginning execution.&lt;/P&gt;&lt;P&gt;MLOGIC(MYSUM): Parameter N has value 4&lt;/P&gt;&lt;P&gt;SYMBOLGEN: Macro variable N resolves to 4&lt;/P&gt;&lt;P&gt;MLOGIC(MYSUM): %IF condition &amp;amp;N &amp;gt;1 is TRUE&lt;/P&gt;&lt;P&gt;SYMBOLGEN: Macro variable N resolves to 4&lt;/P&gt;&lt;P&gt;MLOGIC(MYSUM): Beginning execution.&lt;/P&gt;&lt;P&gt;SYMBOLGEN: Macro variable N resolves to 4&lt;/P&gt;&lt;P&gt;MLOGIC(MYSUM): Parameter N has value 3&lt;/P&gt;&lt;P&gt;SYMBOLGEN: Macro variable N resolves to 3&lt;/P&gt;&lt;P&gt;MLOGIC(MYSUM): %IF condition &amp;amp;N &amp;gt;1 is TRUE&lt;/P&gt;&lt;P&gt;SYMBOLGEN: Macro variable N resolves to 3&lt;/P&gt;&lt;P&gt;MLOGIC(MYSUM): Beginning execution.&lt;/P&gt;&lt;P&gt;SYMBOLGEN: Macro variable N resolves to 3&lt;/P&gt;&lt;P&gt;MLOGIC(MYSUM): Parameter N has value 2&lt;/P&gt;&lt;P&gt;SYMBOLGEN: Macro variable N resolves to 2&lt;/P&gt;&lt;P&gt;MLOGIC(MYSUM): %IF condition &amp;amp;N &amp;gt;1 is TRUE&lt;/P&gt;&lt;P&gt;SYMBOLGEN: Macro variable N resolves to 2&lt;/P&gt;&lt;P&gt;MLOGIC(MYSUM): Beginning execution.&lt;/P&gt;&lt;P&gt;SYMBOLGEN: Macro variable N resolves to 2&lt;/P&gt;&lt;P&gt;MLOGIC(MYSUM): Parameter N has value 1&lt;/P&gt;&lt;P&gt;SYMBOLGEN: Macro variable N resolves to 1&lt;/P&gt;&lt;P&gt;MLOGIC(MYSUM): %IF condition &amp;amp;N &amp;gt;1 is FALSE&lt;/P&gt;&lt;P&gt;SYMBOLGEN: Macro variable N resolves to 1&lt;/P&gt;&lt;P&gt;MLOGIC(MYSUM): Ending execution.&lt;/P&gt;&lt;P&gt;MLOGIC(MYSUM): Ending execution.&lt;/P&gt;&lt;P&gt;2 The SAS System 13:02 Sunday, February 25, 2018&lt;/P&gt;&lt;P&gt;MLOGIC(MYSUM): Ending execution.&lt;/P&gt;&lt;P&gt;MLOGIC(MYSUM): Ending execution.&lt;/P&gt;&lt;P&gt;10&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 25 Feb 2018 21:49:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-does-a-macro-parameter-inside-EVAL-continue-to-loop/m-p/440077#M282493</guid>
      <dc:creator>LL5</dc:creator>
      <dc:date>2018-02-25T21:49:12Z</dc:date>
    </item>
    <item>
      <title>Re: Why does a macro parameter inside %EVAL continue to loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-does-a-macro-parameter-inside-EVAL-continue-to-loop/m-p/440083#M282494</link>
      <description>&lt;P&gt;4+3+2+1 = 10&lt;/P&gt;</description>
      <pubDate>Sun, 25 Feb 2018 22:30:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-does-a-macro-parameter-inside-EVAL-continue-to-loop/m-p/440083#M282494</guid>
      <dc:creator>WarrenKuhfeld</dc:creator>
      <dc:date>2018-02-25T22:30:33Z</dc:date>
    </item>
    <item>
      <title>Re: Why does a macro parameter inside %EVAL continue to loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-does-a-macro-parameter-inside-EVAL-continue-to-loop/m-p/440085#M282495</link>
      <description>Thanks. But why it would loop like that?</description>
      <pubDate>Sun, 25 Feb 2018 22:40:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-does-a-macro-parameter-inside-EVAL-continue-to-loop/m-p/440085#M282495</guid>
      <dc:creator>LL5</dc:creator>
      <dc:date>2018-02-25T22:40:27Z</dc:date>
    </item>
    <item>
      <title>Re: Why does a macro parameter inside %EVAL continue to loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-does-a-macro-parameter-inside-EVAL-continue-to-loop/m-p/440086#M282496</link>
      <description>&lt;P&gt;Notice how the macro calls itself internally.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The increment is not + (&amp;amp;N-1).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead it is + (%MYSUM(%EVAL(&amp;amp;N-1))&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That's what gets you the repetition.&lt;/P&gt;</description>
      <pubDate>Sun, 25 Feb 2018 22:45:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-does-a-macro-parameter-inside-EVAL-continue-to-loop/m-p/440086#M282496</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-02-25T22:45:31Z</dc:date>
    </item>
    <item>
      <title>Re: Why does a macro parameter inside %EVAL continue to loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-does-a-macro-parameter-inside-EVAL-continue-to-loop/m-p/440090#M282497</link>
      <description>&lt;P&gt;It's a recursive loop.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 25 Feb 2018 23:01:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-does-a-macro-parameter-inside-EVAL-continue-to-loop/m-p/440090#M282497</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-02-25T23:01:51Z</dc:date>
    </item>
    <item>
      <title>Re: Why does a macro parameter inside %EVAL continue to loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-does-a-macro-parameter-inside-EVAL-continue-to-loop/m-p/440093#M282498</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/89004"&gt;@LL5&lt;/a&gt;&amp;nbsp;Thank you for the amazing code and a very interesting question:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here you go:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;OPTIONS&amp;nbsp;MLOGIC&amp;nbsp;;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;%MACRO&lt;/STRONG&gt;&amp;nbsp;MYSUM(N);&lt;/P&gt;&lt;P&gt;%IF&amp;nbsp;&amp;amp;N &amp;gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&amp;nbsp;%THEN&amp;nbsp;%EVAL(&amp;amp;N+ %&lt;STRONG&gt;&lt;EM&gt;MYSUM&lt;/EM&gt;&lt;/STRONG&gt;(%EVAL(&amp;amp;N-&lt;STRONG&gt;1&lt;/STRONG&gt;)));&lt;/P&gt;&lt;P&gt;%ELSE&amp;nbsp;&amp;amp;N;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;%MEND&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;%PUT&amp;nbsp;%MYSUM(4);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Execution:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;%if &amp;nbsp;4&amp;gt;1 (true)&lt;/P&gt;&lt;P&gt;So (4+%mysum(%eval(4-1)))&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;(4+%mysum(3))&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Now execute&lt;/STRONG&gt;&lt;STRONG&gt;&amp;nbsp;%mysum(3)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;%if 3&amp;gt;1 (true)&lt;/P&gt;&lt;P&gt;So&amp;nbsp; 3+%mysum(%eval(3-1)))&lt;/P&gt;&lt;P&gt;à3+%mysum(2)&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Now execute&amp;nbsp;&lt;/STRONG&gt;&lt;STRONG&gt;%mysum(2)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;%if 2&amp;gt;1(true)&lt;/P&gt;&lt;P&gt;So&amp;nbsp; 2+%mysum(%eval(2-1)))&lt;/P&gt;&lt;P&gt;2+%mysum(1)&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Now execute&amp;nbsp;&lt;/STRONG&gt;&lt;STRONG&gt;%mysum(1)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;%if 1&amp;gt;1 (false)&lt;/P&gt;&lt;P&gt;So N takes the value of 1.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Final equation %eval(4+3+2+1)=10&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;Basically, the code is generating the equation&amp;nbsp;%EVAL(&amp;amp;N+ %MYSUM(%EVAL(&amp;amp;N-1))); with one execution of the outer parameter&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this helps&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;The bold is intended to make it distinct where i thought it is important and not to sound harsh.&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 25 Feb 2018 23:25:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-does-a-macro-parameter-inside-EVAL-continue-to-loop/m-p/440093#M282498</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-02-25T23:25:55Z</dc:date>
    </item>
    <item>
      <title>Re: Why does a macro parameter inside %EVAL continue to loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-does-a-macro-parameter-inside-EVAL-continue-to-loop/m-p/440095#M282499</link>
      <description>Thanks novinosrin for this step by step illustration! Now I understand the equation is about &amp;amp;n + %mysum(n), so the inside macro continues to loop until the condition is false. This’s really helpful.</description>
      <pubDate>Sun, 25 Feb 2018 23:53:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-does-a-macro-parameter-inside-EVAL-continue-to-loop/m-p/440095#M282499</guid>
      <dc:creator>LL5</dc:creator>
      <dc:date>2018-02-25T23:53:58Z</dc:date>
    </item>
    <item>
      <title>Re: Why does a macro parameter inside %EVAL continue to loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-does-a-macro-parameter-inside-EVAL-continue-to-loop/m-p/440109#M282500</link>
      <description>&lt;P&gt;Turn on the new MLOGICNEST option and the recursive nature of the function is more obvious.&lt;/P&gt;
&lt;P&gt;Also you don't need that extra %EVAL().&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mysum(n);
  %if &amp;amp;n &amp;gt;1 %then %eval(&amp;amp;n+ %mysum(&amp;amp;n-1));
  %else &amp;amp;n;
%mend;

options mlogic mlogicnest;
%put %mysum(3);
options nomlogic nomlogicnest;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;248  %put %mysum(3);
MLOGIC(MYSUM):  Beginning execution.
MLOGIC(MYSUM):  Parameter N has value 3
MLOGIC(MYSUM):  %IF condition &amp;amp;n &amp;gt;1 is TRUE
MLOGIC(MYSUM.MYSUM):  Beginning execution.
MLOGIC(MYSUM.MYSUM):  Parameter N has value 3-1
MLOGIC(MYSUM.MYSUM):  %IF condition &amp;amp;n &amp;gt;1 is TRUE
MLOGIC(MYSUM.MYSUM.MYSUM):  Beginning execution.
MLOGIC(MYSUM.MYSUM.MYSUM):  Parameter N has value 3-1-1
MLOGIC(MYSUM.MYSUM.MYSUM):  %IF condition &amp;amp;n &amp;gt;1 is FALSE
MLOGIC(MYSUM.MYSUM.MYSUM):  Ending execution.
MLOGIC(MYSUM.MYSUM):  Ending execution.
MLOGIC(MYSUM):  Ending execution.
6&lt;/PRE&gt;</description>
      <pubDate>Mon, 26 Feb 2018 03:41:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-does-a-macro-parameter-inside-EVAL-continue-to-loop/m-p/440109#M282500</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-02-26T03:41:03Z</dc:date>
    </item>
    <item>
      <title>Re: Why does a macro parameter inside %EVAL continue to loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-does-a-macro-parameter-inside-EVAL-continue-to-loop/m-p/440110#M282501</link>
      <description>Interesting. So I would think the if then else condition is kind of nested into the inner %mysum macro. So probably the concept is like below:&lt;BR /&gt;&lt;BR /&gt;%MACRO MYSUM(4);&lt;BR /&gt;&lt;BR /&gt;%IF &amp;amp;N &amp;gt;1 %THEN %EVAL(4 + %MYSUM(4-1=3)&lt;BR /&gt;&lt;BR /&gt;%IF &amp;amp;N &amp;gt;1 %THEN %EVAL(3 + %MYSUM(3-1=2)&lt;BR /&gt;&lt;BR /&gt;%IF &amp;amp;N &amp;gt;1 %EVAL(2 + %MYSUM(%EVAL(2-1=1)&lt;BR /&gt;&lt;BR /&gt;%IF &amp;amp;N &amp;gt;1 %EVAL(1 + %MYSUM(1-1=0)&lt;BR /&gt;&lt;BR /&gt;);&lt;BR /&gt;&lt;BR /&gt;%ELSE &amp;amp;N));&lt;BR /&gt;&lt;BR /&gt;%ELSE &amp;amp;N;&lt;BR /&gt;&lt;BR /&gt;%MEND;&lt;BR /&gt;&lt;BR /&gt;%PUT %MYSUM(4);</description>
      <pubDate>Mon, 26 Feb 2018 03:56:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-does-a-macro-parameter-inside-EVAL-continue-to-loop/m-p/440110#M282501</guid>
      <dc:creator>LL5</dc:creator>
      <dc:date>2018-02-26T03:56:01Z</dc:date>
    </item>
    <item>
      <title>Re: Why does a macro parameter inside %EVAL continue to loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-does-a-macro-parameter-inside-EVAL-continue-to-loop/m-p/440135#M282502</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/89004"&gt;@LL5&lt;/a&gt; wrote:&lt;BR /&gt;Thanks. But why it would loop like that?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;See Maxim 23.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(Just kidding)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to see a really elegant application of the recursion principle to a real-world problem, see the &lt;A href="https://en.wikipedia.org/wiki/Quicksort" target="_blank"&gt;wikipedia entry for the quicksort algorithm&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2018 09:06:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-does-a-macro-parameter-inside-EVAL-continue-to-loop/m-p/440135#M282502</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-02-26T09:06:02Z</dc:date>
    </item>
  </channel>
</rss>

