<?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 doesn't this macro work? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Why-doesn-t-this-macro-work/m-p/340330#M77767</link>
    <description>&lt;P&gt;Just to clarify things: with macro language you &lt;U&gt;create&lt;/U&gt; code for exection, you do &lt;U&gt;not do&lt;/U&gt; anything else!&lt;/P&gt;
&lt;P&gt;That code will be there, and if it is syntactically wrong code, the data step will not compile, even if that branch wouldn't be executed at runtime.&lt;/P&gt;</description>
    <pubDate>Mon, 13 Mar 2017 08:44:40 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2017-03-13T08:44:40Z</dc:date>
    <item>
      <title>Why doesn't this macro work?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-doesn-t-this-macro-work/m-p/340302#M77756</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This situation is when one of the condition hit (if &amp;amp;&amp;amp;nA&amp;amp;i &amp;lt;= 0), that line of data should not go throught the logic, however with the code below, it still run throught the logic.&lt;/P&gt;&lt;P&gt;Can anyone help to debug/refine the code below, many thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA WORK.TESTING;&lt;BR /&gt;attrib acct length=$2.;&lt;BR /&gt;INFILE DATALINES DLM="$" DSD;&lt;BR /&gt;INPUT acct a1 a2 a3 a4 a5 a6 a7;&lt;BR /&gt;DATALINES;&lt;BR /&gt;A1$6$2$3$56$76$887$12&lt;BR /&gt;A2$4$90$89$76$887$12$12&lt;BR /&gt;A3$6$90$098$76$887$12$7&lt;BR /&gt;A4$9$20$12$76$887$23$12&lt;BR /&gt;A5$1$$20$23$76$887$12&lt;BR /&gt;;&lt;BR /&gt;RUN;&lt;BR /&gt;&lt;BR /&gt;%macro test;&lt;BR /&gt;data _null_;&lt;BR /&gt;set work.testing end=lastrec;&lt;BR /&gt;remain_bl = SUM(7,-a1);&lt;BR /&gt;bl_value = "a"||STRIP(PUT(remain_bl,best.));&lt;/P&gt;&lt;P&gt;list_cnt = put(_n_,3.);&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;call symput('nA'||STRIP(list_cnt),remain_bl);&lt;BR /&gt;call symput('Avalue'||STRIP(list_cnt),bl_value);&lt;BR /&gt;if lastrec then call symput ('nRows',list_cnt);&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data output;&lt;BR /&gt;set testing;&lt;BR /&gt;%do i = 1 %to &amp;amp;nRows;&lt;BR /&gt;%put &amp;amp;i &amp;amp;&amp;amp;nA&amp;amp;i &amp;amp;&amp;amp;Avalue&amp;amp;i;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;if _n_ = &amp;amp;i then do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if &amp;amp;&amp;amp;nA&amp;amp;i &amp;gt; 0 then do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;checking = "&amp;amp;&amp;amp;Avalue&amp;amp;i.";&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;SumOf = SUM( of a1 - &amp;amp;&amp;amp;Avalue&amp;amp;i);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;else if &amp;amp;&amp;amp;nA&amp;amp;i &amp;lt;= 0 then do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;checking = "&amp;amp;&amp;amp;Avalue&amp;amp;i.";&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;SumOf = 999999;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;output;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;else do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;checking = '';&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;SumOf = .;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;output;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;end;&lt;BR /&gt;%end;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;%MEND test;&lt;BR /&gt;%test;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2017 04:03:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-doesn-t-this-macro-work/m-p/340302#M77756</guid>
      <dc:creator>Jordan_H</dc:creator>
      <dc:date>2017-03-13T04:03:33Z</dc:date>
    </item>
    <item>
      <title>Re: Why doesn't this macro work?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-doesn-t-this-macro-work/m-p/340329#M77766</link>
      <description>&lt;P&gt;You are making calculations with a massive abuse of the macro language that can be done in one simple data step. Are you competing in the Obfuscated SAS Code Contest?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The way I see it, you are creating lookup values for all observations, that are then merged back via repetitions of code for every _n_. DEpending on those values, you do a calculation or not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First of all, your macro code as posted does not work, as it creates invalid code that causes a syntax error:&lt;/P&gt;
&lt;PRE&gt;NOTE: Line generated by the macro variable "AVALUE4".
75          a-2&lt;/PRE&gt;
&lt;P&gt;That's because you have a value of a1=9, and 7-9 = -2, and you can't have a variable named a-2; so there's a logical flaw there.&lt;/P&gt;
&lt;P&gt;Summing a subset of your variables dynamically is best done with an array:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data output;
set testing;
array vars {*} a1-a7;
remain_bl = SUM(7,-a1);
checking = "a"||STRIP(PUT(remain_bl,best.));
if remain_bl &amp;gt; 0 then do;
  sumof = 0;
  do i = 1 to a1;
    sumof = sumof + vars{i};
  end;
end;
else do;
  SumOf = 999999;
end;
drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Mar 2017 08:42:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-doesn-t-this-macro-work/m-p/340329#M77766</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-03-13T08:42:27Z</dc:date>
    </item>
    <item>
      <title>Re: Why doesn't this macro work?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-doesn-t-this-macro-work/m-p/340330#M77767</link>
      <description>&lt;P&gt;Just to clarify things: with macro language you &lt;U&gt;create&lt;/U&gt; code for exection, you do &lt;U&gt;not do&lt;/U&gt; anything else!&lt;/P&gt;
&lt;P&gt;That code will be there, and if it is syntactically wrong code, the data step will not compile, even if that branch wouldn't be executed at runtime.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2017 08:44:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-doesn-t-this-macro-work/m-p/340330#M77767</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-03-13T08:44:40Z</dc:date>
    </item>
    <item>
      <title>Re: Why doesn't this macro work?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-doesn-t-this-macro-work/m-p/340334#M77769</link>
      <description>&lt;P&gt;Thanks for the advice, actually the intention of the code is to use the calculated "remain_bl" to determine the stopping point of summation.&lt;/P&gt;&lt;P&gt;For example, if calculated remain_bl = 3, then the summation should start from SUM(of a1 - a3).&lt;/P&gt;&lt;P&gt;Because of the negative result returns, I have to think of another way.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is that a competition for Obfuscated SAS cd contest? Haha.&lt;/P&gt;&lt;P&gt;Anyway, this code works for me. Thanks!!!&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2017 09:01:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-doesn-t-this-macro-work/m-p/340334#M77769</guid>
      <dc:creator>Jordan_H</dc:creator>
      <dc:date>2017-03-13T09:01:04Z</dc:date>
    </item>
    <item>
      <title>Re: Why doesn't this macro work?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-doesn-t-this-macro-work/m-p/340340#M77771</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;Jordan_H wrote: &lt;BR /&gt;
&lt;P&gt;Is that a competition for Obfuscated SAS cd contest? Haha.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You can see the difference between the two codepieces. It took me some time to infer your intentions from the code, while I guess my code is much easier to decipher.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since there actually are such contests (the most famous being &lt;A href="http://www.ioccc.org/" target="_blank"&gt;The International Obfuscated C Code Contest&lt;/A&gt;), I often use that remark when I encounter unnecessarily complicated code that is hard to read and re-engineer.&lt;/P&gt;
&lt;P&gt;No insult intended, missed to add the &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2017 09:24:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-doesn-t-this-macro-work/m-p/340340#M77771</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-03-13T09:24:04Z</dc:date>
    </item>
    <item>
      <title>Re: Why doesn't this macro work?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-doesn-t-this-macro-work/m-p/340419#M77797</link>
      <description>Hi:&lt;BR /&gt;  Just to chime in. Kurt is correct when he says that the Macro Language and Macro Facility do not DO anything except generate code that goes forward to the compiler. By the time your code gets to the compiler and gets executed, there are NOT any % or &amp;amp; left in the code. &lt;BR /&gt;&lt;BR /&gt;This is the reason, in our Macro classes, we recommend that students start with a basic, working SAS program, without any macro variable or macro program references. Then, they work backward from a program that works, to "macro-tize" it -- first replacing data variables and pieces of DATA step and procedure statements with macro variable references and then after the changed program works with macro variable references, turning your program into a macro program definitions with %DO loops and %IF statement to control the conditional creation of SAS code. &lt;BR /&gt;&lt;BR /&gt;These papers outline the basic concepts and workings of the Macro Facility:&lt;BR /&gt;&lt;A href="http://www2.sas.com/proceedings/sugi28/056-28.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi28/056-28.pdf&lt;/A&gt; and&lt;BR /&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings13/120-2013.pdf" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings13/120-2013.pdf&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;cynthia</description>
      <pubDate>Mon, 13 Mar 2017 13:55:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-doesn-t-this-macro-work/m-p/340419#M77797</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2017-03-13T13:55:43Z</dc:date>
    </item>
    <item>
      <title>Re: Why doesn't this macro work?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-doesn-t-this-macro-work/m-p/340617#M77868</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;Yup, i think classes and experiences is what I'm lacking of.</description>
      <pubDate>Tue, 14 Mar 2017 01:20:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-doesn-t-this-macro-work/m-p/340617#M77868</guid>
      <dc:creator>Jordan_H</dc:creator>
      <dc:date>2017-03-14T01:20:22Z</dc:date>
    </item>
  </channel>
</rss>

