<?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: Macro for computing summation variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-computing-summation-variable/m-p/330710#M271875</link>
    <description>&lt;P&gt;Hey Art&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your quick response.&lt;/P&gt;&lt;P&gt;I have computed values of the first few rows and called the variable wtdmb_h.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Revised excel attached&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Appreciate this.&lt;/P&gt;</description>
    <pubDate>Wed, 08 Feb 2017 04:09:00 GMT</pubDate>
    <dc:creator>anisfiu</dc:creator>
    <dc:date>2017-02-08T04:09:00Z</dc:date>
    <item>
      <title>Macro for computing summation variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-computing-summation-variable/m-p/330699#M271873</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;I will sincerely appreciate if someone can help with the below issue:&lt;/P&gt;&lt;P&gt;I have the attached dataset snaphot of which is as below. I want to compute a variable that for each row first takes the value of &amp;nbsp;&lt;SPAN&gt;cumafd_h, divides this into current and all the lagged values of afd_h, multiplies by the correspding value of mb_h (current when takes the current afd_h and lagged when takes lagged afd_h), and then sums the entire series. I want to do this for alll the rows and by gvkey. For example:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;- for the 3rd row (gvkey 1000, fyear 1972), the variable needs to be compuated as 1.38*(-7.05/&lt;STRONG&gt;-6.86&lt;/STRONG&gt;)+1.30*(.19/&lt;STRONG&gt;-6.86&lt;/STRONG&gt;)+1.42*((.)/(-&lt;STRONG&gt;6.86&lt;/STRONG&gt;))&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;- for the 4th row (gvkey 1000, fyear 1973),&amp;nbsp;the variable needs to be compuated as&amp;nbsp; 0.82*(-0.37/-7.23))+1.38*(-7.05/-7.23)+1.30*(.19/-7.23)+1.42*((.)/(-7.23))&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I also want to impose the restriction that whenever the value within the parrenthesis is &amp;lt;0 , the it is resert as 0. For example - for the 4th row,&amp;nbsp;(.19/-7.23) needs to be reset to 0.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;This variable also needs to be computed by group gvkey.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;My apologies if it is a lot of work.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;gvkey&lt;/TD&gt;&lt;TD&gt;fyear&lt;/TD&gt;&lt;TD&gt;mb_h&lt;/TD&gt;&lt;TD&gt;afd_h&lt;/TD&gt;&lt;TD&gt;cumafd_h&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1000&lt;/TD&gt;&lt;TD&gt;1970&lt;/TD&gt;&lt;TD&gt;1.42&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1000&lt;/TD&gt;&lt;TD&gt;1971&lt;/TD&gt;&lt;TD&gt;1.30&lt;/TD&gt;&lt;TD&gt;0.19&lt;/TD&gt;&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gt;&lt;TR&gt;&lt;TD&gt;1001&lt;/TD&gt;&lt;TD&gt;1984&lt;/TD&gt;&lt;TD&gt;1.25&lt;/TD&gt;&lt;TD&gt;0.27&lt;/TD&gt;&lt;TD&gt;0.27&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1001&lt;/TD&gt;&lt;TD&gt;1985&lt;/TD&gt;&lt;TD&gt;1.68&lt;/TD&gt;&lt;TD&gt;17.24&lt;/TD&gt;&lt;TD&gt;17.51&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1002&lt;/TD&gt;&lt;TD&gt;1972&lt;/TD&gt;&lt;TD&gt;0.75&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1003&lt;/TD&gt;&lt;TD&gt;1983&lt;/TD&gt;&lt;TD&gt;1.94&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1003&lt;/TD&gt;&lt;TD&gt;1984&lt;/TD&gt;&lt;TD&gt;1.11&lt;/TD&gt;&lt;TD&gt;-0.25&lt;/TD&gt;&lt;TD&gt;-0.25&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Wed, 08 Feb 2017 03:26:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-computing-summation-variable/m-p/330699#M271873</guid>
      <dc:creator>anisfiu</dc:creator>
      <dc:date>2017-02-08T03:26:44Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for computing summation variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-computing-summation-variable/m-p/330705#M271874</link>
      <description>&lt;P&gt;It would help if you showed the values you want computed for each row. Also, what do you want to call the new computed variable?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Feb 2017 03:42:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-computing-summation-variable/m-p/330705#M271874</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-02-08T03:42:36Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for computing summation variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-computing-summation-variable/m-p/330710#M271875</link>
      <description>&lt;P&gt;Hey Art&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your quick response.&lt;/P&gt;&lt;P&gt;I have computed values of the first few rows and called the variable wtdmb_h.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Revised excel attached&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Appreciate this.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Feb 2017 04:09:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-computing-summation-variable/m-p/330710#M271875</guid>
      <dc:creator>anisfiu</dc:creator>
      <dc:date>2017-02-08T04:09:00Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for computing summation variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-computing-summation-variable/m-p/330712#M271876</link>
      <description>&lt;P&gt;Please tell me how&amp;nbsp;you get 1.38215 for 1972 and 1.35329 for 1973.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I ask because 1972 includes the expression (.19/-6.86) which is negative and therefore should be reset to zero. But you do not appear to do so.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Feb 2017 05:25:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-computing-summation-variable/m-p/330712#M271876</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-02-08T05:25:39Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for computing summation variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-computing-summation-variable/m-p/330714#M271877</link>
      <description>&lt;P&gt;Assuming I have identified an error in your numbers, then this should work (10feb2017: deprecated, please see second version below)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=neg_sum pos_sum);
  set have;
  by gvkey;

  retain pos_sum neg_sum;

  if first.gvkey then call missing(pos_sum,neg_sum);

  if sign(afd_h)=-1 then do;
  	neg_sum=sum(neg_sum,afd_h*mb_h);
	result=neg_sum/cumafd_h;
  end;
  else if sign(afd_h)= 1 then do;
	pos_sum=sum(pos_sum,afd_h*mb_h);
	result=pos_sum/cumafd_h;
  end;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I leave the INCORRECT code above for those who saw this response previously.&amp;nbsp; The correct code, below, now properly identifies when the POS_SUM vs NEG_SUM should be used.&amp;nbsp; It produces the same results as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt;'s elsewhere in this topic discussion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=neg_sum pos_sum);
  set have;
  by gvkey;

  retain pos_sum neg_sum;

  if first.gvkey then call missing(pos_sum,neg_sum);

  if sign(afd_h)=-1 then neg_sum=sum(neg_sum,afd_h*mb_h); else
  if sign(afd_h)= 1 then pos_sum=sum(pos_sum,afd_h*mb_h);

  if sign(cumafd_h)=-1 then result=neg_sum/cumafd_h; else
  if sign(cumafd_h)= 1 then result=pos_sum/cumafd_h;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The neg_sum is the sum of products&amp;nbsp; of AFD_H*MB_H to be used when CUMAFD_H is negative.&amp;nbsp; pOS_SUM is the same to be used when&amp;nbsp; CUMAFD_H is positive. This implicitly sets the relevant parenthetical expressions (historic AFD_H divided by current CUMAFD_H) to zero when they would be negative.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Feb 2017 17:32:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-computing-summation-variable/m-p/330714#M271877</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-02-10T17:32:11Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for computing summation variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-computing-summation-variable/m-p/330715#M271878</link>
      <description>&lt;P&gt;This gets the same results as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;'s code, thus you should mark his as being the correct answer.&amp;nbsp;My version runs slower, but might save you the time for checking whether his calculations follow your logic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want (drop=counter i);
  set t2;
  array mb(100) _temporary_;
  array af(100) _temporary_;
  by gvkey;
  if first.gvkey then counter=0;
  else do;
    counter+1;
    mb(counter)=mb_h;
    af(counter)=afd_h;
    wtdmb_h=0;
    do i=1 to counter;
      wtdmb_h=wtdmb_h+mb(i)*max(0,af(i)/cumafd_h);
    end;
  end;
run;
&lt;/PRE&gt;
&lt;P&gt;HTH,&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Wed, 08 Feb 2017 05:57:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-computing-summation-variable/m-p/330715#M271878</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-02-08T05:57:49Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for computing summation variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-computing-summation-variable/m-p/330972#M271879</link>
      <description>&lt;P&gt;Hi mkeintz&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sorry for my late reply. Yes are absolutely right. My computation for 1972 was wrong. I ran your codes and it seems to work perfectly. Thank you very much.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Feb 2017 20:30:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-computing-summation-variable/m-p/330972#M271879</guid>
      <dc:creator>anisfiu</dc:creator>
      <dc:date>2017-02-08T20:30:48Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for computing summation variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-computing-summation-variable/m-p/330973#M271880</link>
      <description>&lt;P&gt;Hi art&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much for your solution to the problem. I ran your code and it seems to work perfectly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 08 Feb 2017 20:31:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-computing-summation-variable/m-p/330973#M271880</guid>
      <dc:creator>anisfiu</dc:creator>
      <dc:date>2017-02-08T20:31:47Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for computing summation variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-computing-summation-variable/m-p/331542#M271881</link>
      <description>&lt;P&gt;Hi mkeinthz&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was looking at the output randomly and also went over the logic of your codes again. I want to reset negative value of (afd_h/cumafd_h) to zero. It seems you are separating the neg and pos based on only afd_h.&amp;nbsp;It seems not to address the cases when afd_h and cumafd_h are individually negative leading to a pos value for&amp;nbsp;&lt;SPAN&gt;(afd_h/cumafd_h). I am looking into this again.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I do not expect you to fix this for me. For now I am good. If I fix this I will let you know. But for now it seems art's codes address those cases I mentioned.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Feb 2017 15:11:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-computing-summation-variable/m-p/331542#M271881</guid>
      <dc:creator>anisfiu</dc:creator>
      <dc:date>2017-02-10T15:11:07Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for computing summation variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-computing-summation-variable/m-p/331603#M271882</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/113915"&gt;@anisfiu&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi mkeinthz&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was looking at the output randomly and also went over the logic of your codes again. I want to reset negative value of (afd_h/cumafd_h) to zero. It seems you are separating the neg and pos based on only afd_h.&amp;nbsp;It seems not to address the cases when afd_h and cumafd_h are individually negative leading to a pos value for&amp;nbsp;&lt;SPAN&gt;(afd_h/cumafd_h). I am looking into this again.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I do not expect you to fix this for me. For now I am good. If I fix this I will let you know. But for now it seems art's codes address those cases I mentioned.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/113915"&gt;@anisfiu&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I believe you are correct, and that my code submitted earlier will produce erroneous results when sign(cumafd_h) ^= sign(afd_h).&amp;nbsp; Don't know how I missed that.&amp;nbsp; I'll edit my other post to include what I believe should be the correct code.&amp;nbsp; It now completely matches what &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt;'s code produces.&amp;nbsp; However, it does still offer some efficiencies in that it keeps 2 running totals instead of recalculating them with each new record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Feb 2017 17:27:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-computing-summation-variable/m-p/331603#M271882</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-02-10T17:27:23Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for computing summation variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-computing-summation-variable/m-p/332722#M271883</link>
      <description>&lt;P&gt;Thank you mkeintz.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Feb 2017 17:13:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-computing-summation-variable/m-p/332722#M271883</guid>
      <dc:creator>anisfiu</dc:creator>
      <dc:date>2017-02-14T17:13:11Z</dc:date>
    </item>
  </channel>
</rss>

