<?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: How can we calculate the amount given and taken by whom using retain and loops? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-can-we-calculate-the-amount-given-and-taken-by-whom-using/m-p/647377#M193744</link>
    <description>&lt;P&gt;That was a fun challenge! Here is a solution that works, at least for the data given:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  length member $3;
  input Member Deviation;
cards;
Aus -2000
Br 3000
Nz -10000
In 4000
No 5000
Br  -3000.36
Nz 2000.36
In -4000.53
No 5000.53
;run;

data want;
  set have(where=(Deviation&amp;lt;0) rename=(Member=From));
  retain Remainder 0;
  if Remainder&amp;gt;0 then do;
    Amount=Min(Remainder,-Deviation);
    output;
    end;
  Remainder=Remainder+Deviation;
  do while(not done and Remainder&amp;lt;0);
    set have(where=(Deviation&amp;gt;0) rename=(Member=To)) end=done;
    Amount=min(-Remainder,Deviation);
    output;
    Remainder=Remainder+Deviation;
    end;
  Keep From Amount To;
run;
    
    
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 13 May 2020 09:56:24 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2020-05-13T09:56:24Z</dc:date>
    <item>
      <title>How can we calculate the amount given and taken by whom using retain and loops?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-we-calculate-the-amount-given-and-taken-by-whom-using/m-p/647335#M193721</link>
      <description>&lt;P&gt;I have a dataset with the list of givers and takers present in one column and the amount that is given (the negative values) Or taken (the positive value) by them in another column.&lt;BR /&gt;&lt;BR /&gt;The amount deviation is such that its total sum is equals to 0.&lt;BR /&gt;&lt;BR /&gt;I want to create a dataset consisting of from, amount, to as variables.&lt;BR /&gt;&lt;BR /&gt;The selection of the members should be in sequence for from and to.&lt;BR /&gt;&lt;BR /&gt;Input dataset :&lt;BR /&gt;Member Deviation&lt;BR /&gt;Aus -2000&lt;BR /&gt;Br 3000&lt;BR /&gt;Nz -10000&lt;BR /&gt;In 4000&lt;BR /&gt;No 5000&lt;BR /&gt;Br&amp;nbsp; -3000.36&lt;BR /&gt;Nz 2000.36&lt;BR /&gt;In -4000.53&lt;BR /&gt;No 5000.53&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Output dataset:&lt;BR /&gt;From&amp;nbsp; &amp;nbsp;Amount&amp;nbsp; &amp;nbsp;To&lt;BR /&gt;Aus&amp;nbsp; &amp;nbsp; &amp;nbsp;2000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Br&lt;BR /&gt;Nz&amp;nbsp; &amp;nbsp; &amp;nbsp; 1000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Br&lt;BR /&gt;Nz&amp;nbsp; &amp;nbsp; &amp;nbsp; 4000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; In&lt;BR /&gt;Nz&amp;nbsp; &amp;nbsp; &amp;nbsp; 5000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; No&lt;/P&gt;&lt;P&gt;Br&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2000.36&amp;nbsp; &amp;nbsp;Nz&lt;/P&gt;&lt;P&gt;Br&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; No&lt;/P&gt;&lt;P&gt;In&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4000.53&amp;nbsp; No&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Can anyone help solving it using retain and loops?&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 May 2020 07:13:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-we-calculate-the-amount-given-and-taken-by-whom-using/m-p/647335#M193721</guid>
      <dc:creator>AnupamaMishra</dc:creator>
      <dc:date>2020-05-13T07:13:27Z</dc:date>
    </item>
    <item>
      <title>Re: How can we calculate the amount given and taken by whom using retain and loops?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-we-calculate-the-amount-given-and-taken-by-whom-using/m-p/647377#M193744</link>
      <description>&lt;P&gt;That was a fun challenge! Here is a solution that works, at least for the data given:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  length member $3;
  input Member Deviation;
cards;
Aus -2000
Br 3000
Nz -10000
In 4000
No 5000
Br  -3000.36
Nz 2000.36
In -4000.53
No 5000.53
;run;

data want;
  set have(where=(Deviation&amp;lt;0) rename=(Member=From));
  retain Remainder 0;
  if Remainder&amp;gt;0 then do;
    Amount=Min(Remainder,-Deviation);
    output;
    end;
  Remainder=Remainder+Deviation;
  do while(not done and Remainder&amp;lt;0);
    set have(where=(Deviation&amp;gt;0) rename=(Member=To)) end=done;
    Amount=min(-Remainder,Deviation);
    output;
    Remainder=Remainder+Deviation;
    end;
  Keep From Amount To;
run;
    
    
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 May 2020 09:56:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-we-calculate-the-amount-given-and-taken-by-whom-using/m-p/647377#M193744</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-05-13T09:56:24Z</dc:date>
    </item>
  </channel>
</rss>

