<?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: Do loops in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Do-loops/m-p/701923#M25843</link>
    <description>Exactly as what you think, I am learning base SAS and wondering why the instructor codes like that rather than a simple one. I understand now. Thanks!</description>
    <pubDate>Thu, 26 Nov 2020 21:41:38 GMT</pubDate>
    <dc:creator>anming</dc:creator>
    <dc:date>2020-11-26T21:41:38Z</dc:date>
    <item>
      <title>Do loops</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Do-loops/m-p/701861#M25831</link>
      <description>&lt;P&gt;A quick question on simple investment with an annual interest of 3%:&lt;/P&gt;&lt;P&gt;data invest;&lt;/P&gt;&lt;P&gt;capital=2000;&lt;/P&gt;&lt;P&gt;do year =1 to 5;&lt;/P&gt;&lt;P&gt;capital +capital*0.03;&lt;/P&gt;&lt;P&gt;output;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;someone codes as follows,&lt;/P&gt;&lt;P&gt;data invest2 (drop=count);&lt;/P&gt;&lt;P&gt;capital=2000;&lt;/P&gt;&lt;P&gt;do count=1 to 5;&lt;/P&gt;&lt;P&gt;capital+capital*0.03;&lt;/P&gt;&lt;P&gt;year+1;&lt;/P&gt;&lt;P&gt;output;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for me, they are the same. Why bother to induce a 'count' variable and then drop it while having a "year+1" statement?&lt;/P&gt;</description>
      <pubDate>Thu, 26 Nov 2020 17:26:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Do-loops/m-p/701861#M25831</guid>
      <dc:creator>anming</dc:creator>
      <dc:date>2020-11-26T17:26:26Z</dc:date>
    </item>
    <item>
      <title>Re: Do loops</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Do-loops/m-p/701862#M25832</link>
      <description>&lt;P&gt;Both create the same result, but the first is simpler and easier to read and understand, and therefore to be preferred.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Nov 2020 17:44:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Do-loops/m-p/701862#M25832</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-26T17:44:04Z</dc:date>
    </item>
    <item>
      <title>Re: Do loops</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Do-loops/m-p/701878#M25835</link>
      <description>thanks!</description>
      <pubDate>Thu, 26 Nov 2020 19:36:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Do-loops/m-p/701878#M25835</guid>
      <dc:creator>anming</dc:creator>
      <dc:date>2020-11-26T19:36:54Z</dc:date>
    </item>
    <item>
      <title>Re: Do loops</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Do-loops/m-p/701905#M25839</link>
      <description>&lt;P&gt;Why do this?&lt;/P&gt;
&lt;P&gt;Style choice.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The version with the year+1 was copied from somewhere and the results were as desired, so don't fix what isn't broken.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The version with the year+1 might have been edited from something with a Do Until or Do While loop control instead of iteration. Or there was other code in the do loop that was removed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Following an instructors example that may be using that because the next lesson will use some feature that the other doesn't.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Lots of "why" possibilities. You would have to ask a specific programmer the reasons. Don't be surprised if the result is "didn't think of it".&lt;/P&gt;</description>
      <pubDate>Thu, 26 Nov 2020 21:01:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Do-loops/m-p/701905#M25839</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-11-26T21:01:57Z</dc:date>
    </item>
    <item>
      <title>Re: Do loops</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Do-loops/m-p/701923#M25843</link>
      <description>Exactly as what you think, I am learning base SAS and wondering why the instructor codes like that rather than a simple one. I understand now. Thanks!</description>
      <pubDate>Thu, 26 Nov 2020 21:41:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Do-loops/m-p/701923#M25843</guid>
      <dc:creator>anming</dc:creator>
      <dc:date>2020-11-26T21:41:38Z</dc:date>
    </item>
    <item>
      <title>Re: Do loops</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Do-loops/m-p/702103#M25858</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; In fact, the although the value of CAPITAL is the same after both loops, there is one value that is different at the end of each program, as highlighted below:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_0-1606504969027.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/52049i17CF62759769E658/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_0-1606504969027.png" alt="Cynthia_sas_0-1606504969027.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;We use programs like these to illustrate the point that in a DO loop like the first DO loop, the value of YEAR at the end of the program is the ending value+1, which is what causes the YEAR loop to end. And this shows in the second loop, where YEAR is incremented inside the COUNT loop, the last value of YEAR is 5 and the last value of COUNT is 6. Fully understanding how the DO LOOP works as far as what ends the loop is a basic concept that a student needs to understand before tackling DO WHILE and DO UNTIL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
      <pubDate>Fri, 27 Nov 2020 19:30:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Do-loops/m-p/702103#M25858</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2020-11-27T19:30:54Z</dc:date>
    </item>
    <item>
      <title>Re: Do loops</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Do-loops/m-p/702106#M25860</link>
      <description>Great! But why is "year" assigned to 1 in Loop 2?&lt;BR /&gt;&lt;BR /&gt;I test the codes as follows;&lt;BR /&gt;&lt;BR /&gt;data earning5;&lt;BR /&gt;year = -2;&lt;BR /&gt;capital = 2000,&lt;BR /&gt;do count = 1 to 5;&lt;BR /&gt;capital + capital*0.03;&lt;BR /&gt;year+1;&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;put _all_;&lt;BR /&gt;run;&lt;BR /&gt;the count is 6 and the year is 3. thanks!&lt;BR /&gt;</description>
      <pubDate>Fri, 27 Nov 2020 19:54:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Do-loops/m-p/702106#M25860</guid>
      <dc:creator>anming</dc:creator>
      <dc:date>2020-11-27T19:54:01Z</dc:date>
    </item>
    <item>
      <title>Re: Do loops</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Do-loops/m-p/702209#M25879</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; If you move your PUT statement, you'd understand what is happening. There is a HUGE difference between an ASSIGNMENT statement which assigns a fixed value:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;year = -2;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;and a SUM statement which increments a retained value by the number after the operator. Remember that a SUM statement causes an initial value of 0 for the retained variable and then increments that variable by the number in the statement:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;year + 1;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;&amp;nbsp; In the first example, you are forcing an initial value of -2 for the value of YEAR, so the initial value is then incremented by 1 and -2+1 = -1 -- you broke the counter by starting the value of YEAR at -2, which is why the ending value is only 3. You can prove this to yourself by moving the PUT/PUTLOG statements:&lt;/FONT&gt; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_0-1606586041541.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/52069i71AE75695158C4FE/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_0-1606586041541.png" alt="Cynthia_sas_0-1606586041541.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; On the other hand, if you take the default value used because of the SUM statement, the initial value of YEAR is 0, and incrementing by 1 for each time through the loop results in an ending value of 5, as shown below:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_1-1606586144711.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/52070iDCAD69280B5FF64C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_1-1606586144711.png" alt="Cynthia_sas_1-1606586144711.png" /&gt;&lt;/span&gt;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Understanding the way the SUM statements works is how you understand why the value of YEAR is only 3 in your example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; And you might want to revisit Programming 1 and Programming 2 so the difference is clear.&lt;/P&gt;</description>
      <pubDate>Sat, 28 Nov 2020 17:57:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Do-loops/m-p/702209#M25879</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2020-11-28T17:57:11Z</dc:date>
    </item>
    <item>
      <title>Re: Do loops</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Do-loops/m-p/702213#M25880</link>
      <description>really appreciated!</description>
      <pubDate>Sat, 28 Nov 2020 18:16:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Do-loops/m-p/702213#M25880</guid>
      <dc:creator>anming</dc:creator>
      <dc:date>2020-11-28T18:16:09Z</dc:date>
    </item>
  </channel>
</rss>

