<?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 Some question about DO Loop and Retain without assignment in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Some-question-about-DO-Loop-and-Retain-without-assignment/m-p/564818#M158465</link>
    <description>&lt;P&gt;Hi everyone. I am trying to understand more about DO Loop such as Do while, Do until and RETAIN.&lt;/P&gt;&lt;P&gt;What I want to know more is that the Counter for Do loop is not assigned in the code.&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data carloan_until;
 /*loan = 30000;*/
 payments = 0;
 
 do until (loan = 0);
  loan = loan - 500;
  payments = payments + 1;
 end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;As you can see, i commented LOAN out and hence LOAN variable is not defined in anywhere of the script. When i run the script, the DO LOOP run forever. What is the value of LOAN when it is not assigned? if it is NULL/MISSING, it will cause infinite loop? Why do i run into infinite loop?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As for RETAIN, If it is just a simple data step like below:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
retain LOAN;
set a;
LOAN=500;
run;


data test2;
set a;
retain LOAN;
LOAN=500;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What is the value of LOAN in test and test2?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes I know i can run them in my BASE SAS but i would like to know the value behind it. Don't get me wrong, i did spend some time reading guides online but I am just curious on the behaviour when we do not assign any value before hand.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 10 Jun 2019 07:20:44 GMT</pubDate>
    <dc:creator>imdickson</dc:creator>
    <dc:date>2019-06-10T07:20:44Z</dc:date>
    <item>
      <title>Some question about DO Loop and Retain without assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Some-question-about-DO-Loop-and-Retain-without-assignment/m-p/564818#M158465</link>
      <description>&lt;P&gt;Hi everyone. I am trying to understand more about DO Loop such as Do while, Do until and RETAIN.&lt;/P&gt;&lt;P&gt;What I want to know more is that the Counter for Do loop is not assigned in the code.&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data carloan_until;
 /*loan = 30000;*/
 payments = 0;
 
 do until (loan = 0);
  loan = loan - 500;
  payments = payments + 1;
 end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;As you can see, i commented LOAN out and hence LOAN variable is not defined in anywhere of the script. When i run the script, the DO LOOP run forever. What is the value of LOAN when it is not assigned? if it is NULL/MISSING, it will cause infinite loop? Why do i run into infinite loop?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As for RETAIN, If it is just a simple data step like below:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
retain LOAN;
set a;
LOAN=500;
run;


data test2;
set a;
retain LOAN;
LOAN=500;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What is the value of LOAN in test and test2?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes I know i can run them in my BASE SAS but i would like to know the value behind it. Don't get me wrong, i did spend some time reading guides online but I am just curious on the behaviour when we do not assign any value before hand.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jun 2019 07:20:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Some-question-about-DO-Loop-and-Retain-without-assignment/m-p/564818#M158465</guid>
      <dc:creator>imdickson</dc:creator>
      <dc:date>2019-06-10T07:20:44Z</dc:date>
    </item>
    <item>
      <title>Re: Some question about DO Loop and Retain without assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Some-question-about-DO-Loop-and-Retain-without-assignment/m-p/564825#M158468</link>
      <description>&lt;P&gt;&lt;STRONG&gt;1)&amp;nbsp;&lt;/STRONG&gt;Yes. loan is missing when it is not assigned a value. And when you subtract a value from a missing value, the result will be missing. See the small example below. This is also te reason that your Do Until loop is infinite. At the bottom of each iteration of the loop, SAS checks if the condition in parenthesis (loan=0) is true. This does not happen, because the value of loan will always be missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   put loan=;
   loan=loan-500;
   put loan=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;2)&amp;nbsp;&lt;/STRONG&gt;The value of Loan is both test and test2 is 500 for each observation, because you set it yourself at the end of the data step.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jun 2019 08:01:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Some-question-about-DO-Loop-and-Retain-without-assignment/m-p/564825#M158468</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-06-10T08:01:19Z</dc:date>
    </item>
    <item>
      <title>Re: Some question about DO Loop and Retain without assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Some-question-about-DO-Loop-and-Retain-without-assignment/m-p/564829#M158471</link>
      <description>One additional suggestion for avoiding infinite loops:&lt;BR /&gt;&lt;BR /&gt;When using DO WHILE or DO UNTIL, use an inequality to end the loop.  For example, this is dangerous:&lt;BR /&gt;&lt;BR /&gt;do until (loan = 0);&lt;BR /&gt;&lt;BR /&gt;This is much less likely to produce an infinite loop:&lt;BR /&gt;&lt;BR /&gt;do until (loan &amp;lt;= 0);</description>
      <pubDate>Mon, 10 Jun 2019 08:30:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Some-question-about-DO-Loop-and-Retain-without-assignment/m-p/564829#M158471</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-06-10T08:30:27Z</dc:date>
    </item>
    <item>
      <title>Re: Some question about DO Loop and Retain without assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Some-question-about-DO-Loop-and-Retain-without-assignment/m-p/564832#M158473</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can i understand this way.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For DO Loop, the only difference between WHILE and UNTIl is that WHILE is evaluated the statement before executing while UNTIL is evaluated after the whole loop ends. Therefore, UNTIL is best using = while WHILE is best using &amp;lt; or &amp;gt; ?&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jun 2019 08:47:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Some-question-about-DO-Loop-and-Retain-without-assignment/m-p/564832#M158473</guid>
      <dc:creator>imdickson</dc:creator>
      <dc:date>2019-06-10T08:47:29Z</dc:date>
    </item>
    <item>
      <title>Re: Some question about DO Loop and Retain without assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Some-question-about-DO-Loop-and-Retain-without-assignment/m-p/564884#M158495</link>
      <description>&lt;P&gt;Do until will always go through at least one iteration, while a do while may terminate immediately without executing the loop body at least once.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In both cases, the loop (while) or termination (until) condition needs to be formulated with care to avoid infinite loops.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jun 2019 12:47:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Some-question-about-DO-Loop-and-Retain-without-assignment/m-p/564884#M158495</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-06-10T12:47:31Z</dc:date>
    </item>
    <item>
      <title>Re: Some question about DO Loop and Retain without assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Some-question-about-DO-Loop-and-Retain-without-assignment/m-p/564890#M158497</link>
      <description>&lt;P&gt;No!&lt;BR /&gt;&lt;BR /&gt;DO WHILE ends when its condition becomes false. It is your responsibility to make sure that the condition eventually becomes false.&lt;BR /&gt;&lt;BR /&gt;DO UNTIL ends when its condition becomes true. It is your responsibility to make sure that the condition eventually becomes true.&lt;BR /&gt;&lt;BR /&gt;In both cases, use inequality (including &amp;gt;= or &amp;lt;=) to improve the chances of avoiding an infinite loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a case in point, an infinite loop:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data carloan_until;
 loan = 3100;
 payments = 0;
 do until (loan = 0);
   loan = loan - 500;
   payments = payments + 1;
 end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;With a starting point of 3100, LOAN will never be 0.&amp;nbsp; But it will get to below zero.&amp;nbsp; Changing &amp;lt; to &amp;lt;= will cause the loop to end.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jun 2019 19:02:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Some-question-about-DO-Loop-and-Retain-without-assignment/m-p/564890#M158497</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-06-10T19:02:32Z</dc:date>
    </item>
    <item>
      <title>Re: Some question about DO Loop and Retain without assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Some-question-about-DO-Loop-and-Retain-without-assignment/m-p/565102#M158587</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your explanation. Appreciate it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also come across another DO WHILE loop and this got me confused again.&lt;BR /&gt;The result of n is 6. But why? Do While evaluate the condition before executing. Initially, n=1 and it will not loop. I was expecting n is 7 since the condition is (n lt 6) which comes to my mind is 7. What is the reason behind it?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
n=1;
do while(n lt 6);
n+1;
end;
run;&amp;nbsp;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2019 03:15:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Some-question-about-DO-Loop-and-Retain-without-assignment/m-p/565102#M158587</guid>
      <dc:creator>imdickson</dc:creator>
      <dc:date>2019-06-11T03:15:12Z</dc:date>
    </item>
    <item>
      <title>Re: Some question about DO Loop and Retain without assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Some-question-about-DO-Loop-and-Retain-without-assignment/m-p/565103#M158588</link>
      <description>&lt;P&gt;DO WHILE checks the condition at the top, as you indicated.&amp;nbsp; However, it performs the loop as long as the condition is TRUE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can test some of the actions yourself, by adding PUT statements to the DATA step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
n=1;
do while(n lt 6);
n+1;
put "Inside:  "  n=;
end;
put "Outside:  " n=;
run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Jun 2019 03:25:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Some-question-about-DO-Loop-and-Retain-without-assignment/m-p/565103#M158588</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-06-11T03:25:17Z</dc:date>
    </item>
    <item>
      <title>Re: Some question about DO Loop and Retain without assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Some-question-about-DO-Loop-and-Retain-without-assignment/m-p/565104#M158589</link>
      <description>&lt;P&gt;Thanks for the info. If i understand correctly, the reason why n is 6 is due to:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1) The very last loop counter(n) is 5&lt;/P&gt;&lt;P&gt;2) The reason why n=5 counter but n is 6 at the end is because of n+1 in the loop of counter n=5.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hence the result is 6.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do i understand correctly?&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2019 03:39:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Some-question-about-DO-Loop-and-Retain-without-assignment/m-p/565104#M158589</guid>
      <dc:creator>imdickson</dc:creator>
      <dc:date>2019-06-11T03:39:54Z</dc:date>
    </item>
    <item>
      <title>Re: Some question about DO Loop and Retain without assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Some-question-about-DO-Loop-and-Retain-without-assignment/m-p/565111#M158594</link>
      <description>&lt;P&gt;It&amp;nbsp;&lt;EM&gt;will&lt;/EM&gt; loop,as the while condition is met.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But since you are testing for&amp;nbsp;&lt;EM&gt;less than&lt;/EM&gt; instead of&amp;nbsp;&lt;EM&gt;less or equal&lt;/EM&gt;, the loop terminates as soon as n is incremented to 6.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Whenever you have such wonderings, put statements are great to clear up doubts:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
n = 1;
cond = (n lt 6);
put "before: " cond=;
do while (n lt 6);
 n + 1;
 cond = (n lt 6);
 put "end of loop: " n= cond=;
end;
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Run this code and take a close look at the log.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2019 05:35:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Some-question-about-DO-Loop-and-Retain-without-assignment/m-p/565111#M158594</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-06-11T05:35:29Z</dc:date>
    </item>
    <item>
      <title>Re: Some question about DO Loop and Retain without assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Some-question-about-DO-Loop-and-Retain-without-assignment/m-p/565249#M158676</link>
      <description>&lt;P&gt;That's correct.&amp;nbsp; One more piece to add to your thinking ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once n increases to 6, the loop is ALMOST over.&amp;nbsp; SAS checks the DO WHILE condition, and finds that it is false.&amp;nbsp; That's when SAS decides that the loop is over and should not execute again.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2019 14:05:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Some-question-about-DO-Loop-and-Retain-without-assignment/m-p/565249#M158676</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-06-11T14:05:19Z</dc:date>
    </item>
  </channel>
</rss>

