<?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 loop (until ? while ?) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-loop-until-while/m-p/306046#M65384</link>
    <description>&lt;P&gt;Hi cynthia ,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i will try to explain myself better (and many thanks for your answers).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA HAVE;&lt;/P&gt;&lt;P&gt;VAL1 VAL2;&lt;/P&gt;&lt;P&gt;10;60&lt;/P&gt;&lt;P&gt;-100;60&lt;/P&gt;&lt;P&gt;50;100&lt;/P&gt;&lt;P&gt;-30;70&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA WANT;&lt;/P&gt;&lt;P&gt;50;10&lt;/P&gt;&lt;P&gt;-90;50&lt;/P&gt;&lt;P&gt;50;100&lt;/P&gt;&lt;P&gt;-30;70&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Q&amp;nbsp;:So how we got the two first rows ?&lt;/P&gt;&lt;P&gt;A: first we calculate the sum(val1) . the sum of this column must'nt be &amp;lt; 0 we&amp;nbsp;have to get a sum(val1) equal &amp;nbsp;0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The algorithm :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if sum(val1) &amp;lt; 0 then ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in our example the sum(val1) = -70;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;those -70 must be taken from the val2 column (with a loop ..)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;so we loop over the table :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;first step in the loop :&amp;nbsp;&lt;/P&gt;&lt;P&gt;- row _N_ = 1 : val1 = val1&amp;nbsp;+ val2 &amp;nbsp; &amp;nbsp; ====&amp;gt; but as the first row we can only have 60 from val2 we must go to the second row (we still have 10 to add)&lt;/P&gt;&lt;P&gt;- row _N_ = 2 : val1 = val1 + 10&lt;/P&gt;&lt;P&gt;=======&amp;gt; the loop finish as we added 70 to the sum(val1)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If the first val2 was 70 the loop must stop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if at the second row val2 was 5 we must go to the thirs row&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and so on till we have the sum(val1) = 0.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this time i was clear enough.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and many thanks cynthia.&lt;/P&gt;</description>
    <pubDate>Thu, 20 Oct 2016 19:12:50 GMT</pubDate>
    <dc:creator>othmane1984</dc:creator>
    <dc:date>2016-10-20T19:12:50Z</dc:date>
    <item>
      <title>SAS loop (until ? while ?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-loop-until-while/m-p/305958#M65352</link>
      <description>&lt;P&gt;Dear,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a little issue coding a loop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My Table looks like the following :&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;VAL1;VAL2&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;10;60&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;-100;60&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;50;100&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;-30;70&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;*****&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;What i want is :&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;- &amp;nbsp;Calculating the sum of val1 ==&amp;gt; DONE .(-70)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;- As this sum is &amp;lt; 0 &amp;nbsp;i must do a loop to :&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;* Compensate the negative sum of VAL1 by the values of VAL2&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;In my example i have to compensate -70 , so from the first line the loop must take 60 and add it to val1 (remain -10 = -70 +60) ,&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;the loop goes to the second line and take 10 from val2 and add it to val1 ..==&amp;gt; END as we compensate the -70.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Hopping my explanation was clear .&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Many thanks for your help&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2016 14:31:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-loop-until-while/m-p/305958#M65352</guid>
      <dc:creator>othmane1984</dc:creator>
      <dc:date>2016-10-20T14:31:41Z</dc:date>
    </item>
    <item>
      <title>Re: SAS loop (until ? while ?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-loop-until-while/m-p/306002#M65369</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Can you show a table of your desired results? What you want for row 1 seems clear, but not for the other rows.&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;cynthia&lt;/P&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/5384i7772C5270BA4E919/image-size/original?v=v2&amp;amp;px=-1" alt="show_desired_result.png" title="show_desired_result.png" border="0" /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2016 16:51:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-loop-until-while/m-p/306002#M65369</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2016-10-20T16:51:15Z</dc:date>
    </item>
    <item>
      <title>Re: SAS loop (until ? while ?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-loop-until-while/m-p/306006#M65371</link>
      <description>Hi Cynthia&lt;BR /&gt;For thz first row it Will be&lt;BR /&gt;70; 10 as thé sum is -70 from thé first row we could take just 60 so we have to go to thé second Line which Will be&lt;BR /&gt;-90; 50 and we stop at thé second Line as we affected the -70.&lt;BR /&gt;If thé sum was bigger lets say -120 we have to loop to thé other lines...&lt;BR /&gt;&lt;BR /&gt;Hope it was clear and many thanks in advance</description>
      <pubDate>Thu, 20 Oct 2016 17:11:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-loop-until-while/m-p/306006#M65371</guid>
      <dc:creator>othmane1984</dc:creator>
      <dc:date>2016-10-20T17:11:20Z</dc:date>
    </item>
    <item>
      <title>Re: SAS loop (until ? while ?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-loop-until-while/m-p/306013#M65373</link>
      <description>&lt;P&gt;Hi, Sorry, I still don't get what you want. What will the ACTUAL adjusted numbers be on every row? From your original explanation, it sounded like you only wanted to adjust the value in VAL2, but now it sounds like you want to adjust the value in VAL1, as well. This sounds like you might need a DATA step program.&lt;BR /&gt;&lt;BR /&gt; And, it's still not clear whether you want a new dataset as the output or whether you want a report. If you want a report, PROC REPORT might be able to do this, but what you want to see on the desired output report is still not clear. As an example, here is a report with &lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/5386i5D5AED57837E929C/image-size/original?v=v2&amp;amp;px=-1" border="0" alt="possible_desired_output.png" title="possible_desired_output.png" /&gt;VAL2 adjusted:&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt; &lt;BR /&gt;cynthia&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2016 17:34:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-loop-until-while/m-p/306013#M65373</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2016-10-20T17:34:56Z</dc:date>
    </item>
    <item>
      <title>Re: SAS loop (until ? while ?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-loop-until-while/m-p/306046#M65384</link>
      <description>&lt;P&gt;Hi cynthia ,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i will try to explain myself better (and many thanks for your answers).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA HAVE;&lt;/P&gt;&lt;P&gt;VAL1 VAL2;&lt;/P&gt;&lt;P&gt;10;60&lt;/P&gt;&lt;P&gt;-100;60&lt;/P&gt;&lt;P&gt;50;100&lt;/P&gt;&lt;P&gt;-30;70&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA WANT;&lt;/P&gt;&lt;P&gt;50;10&lt;/P&gt;&lt;P&gt;-90;50&lt;/P&gt;&lt;P&gt;50;100&lt;/P&gt;&lt;P&gt;-30;70&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Q&amp;nbsp;:So how we got the two first rows ?&lt;/P&gt;&lt;P&gt;A: first we calculate the sum(val1) . the sum of this column must'nt be &amp;lt; 0 we&amp;nbsp;have to get a sum(val1) equal &amp;nbsp;0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The algorithm :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if sum(val1) &amp;lt; 0 then ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in our example the sum(val1) = -70;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;those -70 must be taken from the val2 column (with a loop ..)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;so we loop over the table :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;first step in the loop :&amp;nbsp;&lt;/P&gt;&lt;P&gt;- row _N_ = 1 : val1 = val1&amp;nbsp;+ val2 &amp;nbsp; &amp;nbsp; ====&amp;gt; but as the first row we can only have 60 from val2 we must go to the second row (we still have 10 to add)&lt;/P&gt;&lt;P&gt;- row _N_ = 2 : val1 = val1 + 10&lt;/P&gt;&lt;P&gt;=======&amp;gt; the loop finish as we added 70 to the sum(val1)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If the first val2 was 70 the loop must stop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if at the second row val2 was 5 we must go to the thirs row&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and so on till we have the sum(val1) = 0.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this time i was clear enough.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and many thanks cynthia.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2016 19:12:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-loop-until-while/m-p/306046#M65384</guid>
      <dc:creator>othmane1984</dc:creator>
      <dc:date>2016-10-20T19:12:50Z</dc:date>
    </item>
    <item>
      <title>Re: SAS loop (until ? while ?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-loop-until-while/m-p/306123#M65415</link>
      <description>Hi, &lt;BR /&gt;  Perhaps someone else understands what you want to do. You indicate that "-70 must be taken from the VAL2 column", but then you adjust both VAL1 and VAL2.&lt;BR /&gt; &lt;BR /&gt;cynthia</description>
      <pubDate>Thu, 20 Oct 2016 22:51:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-loop-until-while/m-p/306123#M65415</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2016-10-20T22:51:13Z</dc:date>
    </item>
    <item>
      <title>Re: SAS loop (until ? while ?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-loop-until-while/m-p/306124#M65416</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
 input VAL1 VAL2;
 cards;
10 60
-100 6
50 100
-30 70
run;

data WANT;
  if _N_=1 then do until(LASTOBS);
    set HAVE end=LASTOBS;
    VAL3+VAL1;
  end;
  retain VAL3;
  set HAVE;
  if VAL3&amp;lt;0 then do;
   VAL3+VAL2;
   if VAL3&amp;gt;0 then VAL3=0;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellpadding="5" cellspacing="0"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r header" scope="col"&gt;VAL1&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;VAL2&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;VAL3&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;10&lt;/TD&gt;
&lt;TD class="r data"&gt;60&lt;/TD&gt;
&lt;TD class="r data"&gt;-10&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;-100&lt;/TD&gt;
&lt;TD class="r data"&gt;6&lt;/TD&gt;
&lt;TD class="r data"&gt;-4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;50&lt;/TD&gt;
&lt;TD class="r data"&gt;100&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;-30&lt;/TD&gt;
&lt;TD class="r data"&gt;70&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2016 23:16:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-loop-until-while/m-p/306124#M65416</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2016-10-20T23:16:38Z</dc:date>
    </item>
    <item>
      <title>Re: SAS loop (until ? while ?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-loop-until-while/m-p/306197#M65441</link>
      <description>&lt;P&gt;Hi &amp;nbsp;,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i will take a real example , hopping this time that it will be clear (and sorry again for bothering).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;--------------------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
 input day BANK1 BANK2;
 cards;
1 10 60
2 -100 60
3 50 100
4 -30 70
run;


Proc sql noprint ; 

select sum(val1) into: sum

from have ;

run ;

/* As the a bank-account can't be &amp;lt; 0 , we must take money from bank2 and put it in bank1*/
/* The total money to be taken is equal to -70*/


&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So , the SUM (=-70)&lt;/P&gt;&lt;P&gt;We must take this money from bank2 and put it in bank1 ==&amp;gt; So more money for BANK1 and less money for BANK2 &amp;nbsp;until we have the following dataset :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT;
 input day BANK1 BANK2 ValueRetrievedFromTheSUM;
 cards;
1 70 0 60
2 -90 50 10
3 50 100 0
4 -30 70 0
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Oct 2016 07:28:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-loop-until-while/m-p/306197#M65441</guid>
      <dc:creator>othmane1984</dc:creator>
      <dc:date>2016-10-21T07:28:24Z</dc:date>
    </item>
  </channel>
</rss>

