<?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: Sas data set inner loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sas-data-set-inner-loop/m-p/349375#M81022</link>
    <description>&lt;P&gt;Are you sure you don't need to add some ELSE statements in there? Did you mean for those IF/THEN statements to run in series like that?&amp;nbsp;Currently if the first one is true and it modifies the variables then it could cause one of the later ones to be true also.&lt;/P&gt;</description>
    <pubDate>Wed, 12 Apr 2017 11:26:50 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2017-04-12T11:26:50Z</dc:date>
    <item>
      <title>Sas data set inner loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sas-data-set-inner-loop/m-p/349174#M80964</link>
      <description>&lt;P&gt;Hi. I am new at sas. I need help in solving the following problem.&lt;BR /&gt;This problem is in context of loan taken from bank with given guarantee.&lt;BR /&gt;Here a loan can have multiple guarantees and a guarantee can be link to multiple loans.&lt;BR /&gt;&lt;BR /&gt;My objective is to allocate the guarantees to respective loans based on the rank given.&lt;BR /&gt;So we have loan rank and guarantee rank.&lt;BR /&gt;We start with the loan with lowest rank first then get all the guarantees allocated to it and start allocating guarantee to the loan based on the guarantee rank.&lt;BR /&gt;For example take the following dataset&lt;/P&gt;&lt;PRE&gt;Loan_id guarantee_id loan_rank guarantee_rank loan_amount gurantee_amount
A1 g1 1 1 1000 100
A1 g2 1 2 1000 50
A2 g1 2 1 500 100
A3 g3 3 1 400 200&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;Now starting from rank 1 loan we allocate g1 to it and upadte value of g1 in row 3 as 0.&lt;BR /&gt;&lt;BR /&gt;So we have:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;Loan_id guarantee_id loan_rank guarantee_rank loan_amount guarantee_amount allocated unallocated remaining_guarantee
A1 g1 1 1 1000 100 100 900 0
A1 g2 1 2 1000 50
A2 g1 2 1 500 0
A3 g3 3 1 400 200&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;Then g2 get allocated to a1 and results in:&lt;/P&gt;&lt;PRE&gt;A1 g1 1 1 1000 100 100 900 0
A1 g2 1 2 1000 50 50 850 0
A2 g1 2 1 500 0
A3 g3 3 1 400 200&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;Notice that unallocated=previous unallocated-guarantee amount&lt;BR /&gt;&lt;BR /&gt;Similarly proceding we have final output as&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;A1 g1 1 1 1000 100 100 900 0
A1 g2 1 2 1000 50 50 850 0
A2 g1 2 1 500 0 0 500 0
A3 g3 3 1 400 200 200 200 0&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;I have written the following code :&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data temp;
  Input loan_id $ guarantee_id $ loan_rank guarantee_rank loan_amount guarantee_amount;
datalines;
A1 g1 1 1 1000 100
A1 g2 1 2 1000 50
A2 g1 2 1 500 100
A3 g3 3 1 400 200
;
Run;

Proc sort data=temp;by loan_id guarantee_id;

Data temp1;
  Set temp;
  Retain unallocated;
  If guarantee_rank eq 1 and guarantee_amount&amp;gt;=loan_amount then do;
    Allocated=loan_amount;
    Unallocated=0;
    Remaining_guarantee=guarantee_amount-loan_amount;
  End;
  If guarantee_rank eq 1 and guarantee_amount&amp;lt;loan_amount then do;
    Allocated=guarantee_amount;
    Unallocated=loan_amount-guarantee_amount;
    Remaining_guarantee=0;
  End;
  If guarantee_rank ne 1 and guarantee_amount&amp;gt;=unallocated then do;
    Allocated=unallocated;
    Unallocated=0;
    Remaining_guarantee=guarantee_amount-unallocated;
  End;
  If guarantee_rank ne 1 and guarantee_amount&amp;lt;unallocated then do;
    Allocated=guarantee_amount;
    Unallocated=unallocated-guarantee_amount;
    Remaining_guarantee=0;
  End;
Run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;Here updation of guarantee_amount as the remaining_guarantee is not happening.&lt;BR /&gt;&lt;BR /&gt;Can any body help me with this?&lt;BR /&gt;&lt;BR /&gt;I really need this solved as soon as possible.&lt;BR /&gt;&lt;BR /&gt;Thank you in advance.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Apr 2017 12:17:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sas-data-set-inner-loop/m-p/349174#M80964</guid>
      <dc:creator>Bipasha</dc:creator>
      <dc:date>2017-04-12T12:17:09Z</dc:date>
    </item>
    <item>
      <title>Re: Sas data set inner loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sas-data-set-inner-loop/m-p/349193#M80973</link>
      <description>&lt;P&gt;Perhaps what you are asking about is the order of these three statements:&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Allocated=unallocated;&lt;BR /&gt;Unallocated=0;&lt;BR /&gt;Remaining_guarantee=guarantee_amount-unallocated;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe the second and third statements should be switched.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Apr 2017 16:53:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sas-data-set-inner-loop/m-p/349193#M80973</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-04-11T16:53:29Z</dc:date>
    </item>
    <item>
      <title>Re: Sas data set inner loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sas-data-set-inner-loop/m-p/349305#M80996</link>
      <description>Hi. Thank you for your input. But my problem is I wanl to update the&lt;BR /&gt;guarantee_amount to the remaining_guarantee for the total remaining rows&lt;BR /&gt;where it guarantee_id is g1. This has to be done for every guarantee.&lt;BR /&gt;&lt;BR /&gt;##- Please type your reply above this line. Simple formatting, no&lt;BR /&gt;attachments. -##</description>
      <pubDate>Wed, 12 Apr 2017 03:20:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sas-data-set-inner-loop/m-p/349305#M80996</guid>
      <dc:creator>Bipasha</dc:creator>
      <dc:date>2017-04-12T03:20:19Z</dc:date>
    </item>
    <item>
      <title>Re: Sas data set inner loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sas-data-set-inner-loop/m-p/349320#M81004</link>
      <description>proc sort data=have;by loan_id;run;&lt;BR /&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;by loan_id;&lt;BR /&gt;retain allocated unallocated;&lt;BR /&gt;allocated=guarantee_amount;&lt;BR /&gt;if first.loan_id then unallocated=loan_amount-allocated;&lt;BR /&gt;else unallocated=unallocated-allocated;&lt;BR /&gt;output;&lt;BR /&gt;if last.loan_id then do;&lt;BR /&gt;allocated=0;unallocated=0;&lt;BR /&gt;end;&lt;BR /&gt;run;</description>
      <pubDate>Wed, 12 Apr 2017 05:22:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sas-data-set-inner-loop/m-p/349320#M81004</guid>
      <dc:creator>lakshmi_74</dc:creator>
      <dc:date>2017-04-12T05:22:39Z</dc:date>
    </item>
    <item>
      <title>Re: Sas data set inner loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sas-data-set-inner-loop/m-p/349365#M81018</link>
      <description>Thanks for the efficient code.&lt;BR /&gt;The thing is this code does not update the guarantee_amount in the 3rd row&lt;BR /&gt;as g1 is already allocated to a1. Its value must be updated to 3rd row as 0.&lt;BR /&gt;This is the part I am strugging with.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;##- Please type your reply above this line. Simple formatting, no&lt;BR /&gt;attachments. -##</description>
      <pubDate>Wed, 12 Apr 2017 10:37:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sas-data-set-inner-loop/m-p/349365#M81018</guid>
      <dc:creator>Bipasha</dc:creator>
      <dc:date>2017-04-12T10:37:19Z</dc:date>
    </item>
    <item>
      <title>Re: Sas data set inner loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sas-data-set-inner-loop/m-p/349375#M81022</link>
      <description>&lt;P&gt;Are you sure you don't need to add some ELSE statements in there? Did you mean for those IF/THEN statements to run in series like that?&amp;nbsp;Currently if the first one is true and it modifies the variables then it could cause one of the later ones to be true also.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Apr 2017 11:26:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sas-data-set-inner-loop/m-p/349375#M81022</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-04-12T11:26:50Z</dc:date>
    </item>
    <item>
      <title>Re: Sas data set inner loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sas-data-set-inner-loop/m-p/349401#M81034</link>
      <description>&lt;P&gt;I the above code I mensioned it gives output as&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;a1 g1 1 1 1000 100 100 900 &amp;nbsp;0&lt;BR /&gt;a1 g2 1 2 1000 50 &amp;nbsp; 50 &amp;nbsp; 850 &amp;nbsp;0&lt;BR /&gt;a2 g1 2 1 500 &amp;nbsp; &lt;FONT color="#FF0000"&gt;100&amp;nbsp;100 400 &amp;nbsp;0&lt;/FONT&gt;&lt;BR /&gt;a3 g3 3 1 400 &amp;nbsp; 200 200 200 &amp;nbsp;0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem I am facing is in the third row. What I want is, once a gurantee is allocated to a particular loan its value should be updated in te rest of the rows. Hence in third row, gurantee_amount should be 0, thus allocated=0 and unallocated=500.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So my desired output is:&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;a1 g1 1 1 1000 100 100 900 &amp;nbsp;0&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;a1 g2 1 2 1000 50 &amp;nbsp; 50 &amp;nbsp; 850 &amp;nbsp;0&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;a2 g1 2 1 500 &amp;nbsp; 0&lt;/SPAN&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;FONT color="#000000"&gt;0&amp;nbsp; &amp;nbsp; &amp;nbsp;500 &amp;nbsp;0&lt;/FONT&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;SPAN&gt;a3 g3 3 1 400 &amp;nbsp; 200 200 200 &amp;nbsp;0&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can give me totally new code if that works.&lt;/P&gt;&lt;P&gt;But for now yes I need these if statements to run in sequence and i dont need any else statement.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I didnt get your last concern. Please elaborate.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Apr 2017 12:15:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sas-data-set-inner-loop/m-p/349401#M81034</guid>
      <dc:creator>Bipasha</dc:creator>
      <dc:date>2017-04-12T12:15:07Z</dc:date>
    </item>
  </channel>
</rss>

