<?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: Iterative Do Loops - Summining in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Iterative-Do-Loops-Summining/m-p/743388#M232734</link>
    <description>&lt;P&gt;If you really want to keep MONTH to indicate how many months ahead the calculated amount represents then use a different variable for the loop counter.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;months=12 ;
do i=1 to months ;
  ....
end;
drop i;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 24 May 2021 15:30:58 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-05-24T15:30:58Z</dc:date>
    <item>
      <title>Iterative Do Loops - Summining</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-Do-Loops-Summining/m-p/743318#M232701</link>
      <description>&lt;P&gt;&lt;EM&gt;Lets say I have a table called: pg2. savings looking like this:&amp;nbsp;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Obs&lt;/TD&gt;&lt;TD&gt;Name&lt;/TD&gt;&lt;TD&gt;Amount&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;James&lt;/TD&gt;&lt;TD&gt;250&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-odd ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;Linda&lt;/TD&gt;&lt;TD&gt;300&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;Mary&lt;/TD&gt;&lt;TD&gt;275&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row ui-state-default dgrid-row-odd dgrid-selected ui-state-active"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;Robert&lt;/TD&gt;&lt;TD&gt;350&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class=" dgrid-row ui-state-default dgrid-row-odd dgrid-selected ui-state-active"&gt;If I now run the code:&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE&gt;data YearSavings;
	set pg2.savings;
	*savings = 0; *This will solve the problem of question 1 below.;
		do Month=1 to 12;
			Savings+Amount;
			Savings+(Savings*0.02/12);
		end;
	format Savings comma12.2;
	*Month = Month-1; *Related to question 2; 
run;&lt;/PRE&gt;I get the following output:&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row ui-state-default dgrid-row-odd dgrid-selected ui-state-active"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row ui-state-default dgrid-row-odd dgrid-selected ui-state-active"&gt;&lt;DIV class="dgrid-scroller"&gt;&lt;DIV class="dgrid-content ui-widget-content"&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Obs&lt;/TD&gt;&lt;TD&gt;Name&lt;/TD&gt;&lt;TD&gt;Amount&lt;/TD&gt;&lt;TD&gt;Month&lt;/TD&gt;&lt;TD&gt;Savings&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;James&lt;/TD&gt;&lt;TD&gt;250&lt;/TD&gt;&lt;TD&gt;13&lt;/TD&gt;&lt;TD&gt;3,032.70&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-odd ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;Linda&lt;/TD&gt;&lt;TD&gt;300&lt;/TD&gt;&lt;TD&gt;13&lt;/TD&gt;&lt;TD&gt;6,733.15&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;Mary&lt;/TD&gt;&lt;TD&gt;275&lt;/TD&gt;&lt;TD&gt;13&lt;/TD&gt;&lt;TD&gt;10,205.03&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row ui-state-default dgrid-row-odd dgrid-selected ui-state-active"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;Robert&lt;/TD&gt;&lt;TD&gt;350&lt;/TD&gt;&lt;TD&gt;13&lt;/TD&gt;&lt;TD&gt;14,656.79&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row ui-state-default dgrid-row-odd dgrid-selected ui-state-active"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row ui-state-default dgrid-row-odd dgrid-selected ui-state-active"&gt;Notice how the Savings column clearly is wrong (for example, Mary saves less than Linda but still ends up with more savings).&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;The explanation for this is:&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;EM&gt;"Notice that the&amp;nbsp;&lt;STRONG&gt;Savings&lt;/STRONG&gt;&amp;nbsp;value keeps increasing for each row. This is because the value of&amp;nbsp;&lt;STRONG&gt;Savings&lt;/STRONG&gt;&amp;nbsp;is automatically retained because it's part of a sum statement. So each time we start a new iteration of the data step, reading a new person, the value of&amp;nbsp;&lt;STRONG&gt;Savings&lt;/STRONG&gt;&amp;nbsp;is retained, but it represents the savings from the previous person."&lt;BR /&gt;&lt;BR /&gt;&lt;/EM&gt;I have though about it but do not understand it.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;The solution is to un-comment the row:&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row ui-state-default dgrid-row-odd dgrid-selected ui-state-active"&gt;&lt;PRE&gt;*savings = 0; *This will solve the problem of question 1 below.;&lt;/PRE&gt;&lt;BR /&gt;So my questions is:&lt;BR /&gt;&lt;BR /&gt;1. Any explanations of why the problem occurs? I do not understand this nor the explanation given above.&amp;nbsp;&lt;BR /&gt;2. Despite running 12 months in the iterations, it shows up as 13 which is pretty uggly. One solution would be to un-comment&amp;nbsp; the row:&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row ui-state-default dgrid-row-odd dgrid-selected ui-state-active"&gt;&lt;PRE&gt;*Month = Month-1;&lt;/PRE&gt;Is there any best practices for this?&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Thanks.&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row ui-state-default dgrid-row-odd dgrid-selected ui-state-active"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row ui-state-default dgrid-row-odd dgrid-selected ui-state-active"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row ui-state-default dgrid-row-odd dgrid-selected ui-state-active"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Mon, 24 May 2021 09:17:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-Do-Loops-Summining/m-p/743318#M232701</guid>
      <dc:creator>SasStatistics</dc:creator>
      <dc:date>2021-05-24T09:17:47Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative Do Loops - Summining</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-Do-Loops-Summining/m-p/743329#M232706</link>
      <description>Regarding question 1 I now understand it...&lt;BR /&gt;&lt;BR /&gt;From the line "set pg2.savings;" a new row is read after each loop and after that we need to put savings = 0; .&lt;BR /&gt;&lt;BR /&gt;So question 1 is Done!</description>
      <pubDate>Mon, 24 May 2021 10:12:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-Do-Loops-Summining/m-p/743329#M232706</guid>
      <dc:creator>SasStatistics</dc:creator>
      <dc:date>2021-05-24T10:12:10Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative Do Loops - Summining</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-Do-Loops-Summining/m-p/743380#M232732</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/381436"&gt;@SasStatistics&lt;/a&gt;,&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/381436"&gt;@SasStatistics&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;DIV class=" dgrid-row ui-state-default dgrid-row-odd dgrid-selected ui-state-active"&gt;2. Despite running 12 months in the iterations, it shows up as 13 which is pretty uggly. One solution would be to un-comment&amp;nbsp; the row:&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class=" dgrid-row ui-state-default dgrid-row-odd dgrid-selected ui-state-active"&gt;
&lt;PRE&gt;*Month = Month-1;&lt;/PRE&gt;
Is there any best practices for this?&amp;nbsp;&lt;/DIV&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It is this additional incrementation of the index variable beyond the stop value (here: 12) which causes the loop to stop iterating. Sometimes this can be useful, e.g., because the final value of the index variable shows that the loop did not stop during the last iteration or earlier (&lt;A href="https://communities.sas.com/t5/SAS-Programming/Using-array-to-create-a-new-variable/m-p/743236/highlight/true#M232650" target="_blank" rel="noopener"&gt;example&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are several reasons why in practice those "ugly" values are rarely an issue (that requires a statement like &lt;FONT face="courier new,courier"&gt;Month = Month-1&lt;/FONT&gt;)&lt;FONT face="helvetica"&gt;:&lt;/FONT&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The index variables of many if not most iterative DO loops (of the form DO &lt;EM&gt;variable&lt;/EM&gt;=&lt;EM&gt;start&lt;/EM&gt; TO &lt;EM&gt;stop&lt;/EM&gt;) are only used within the loop and not written to any output dataset. In these cases their names (typically &lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;j&lt;/FONT&gt;, etc.) often occur in DROP statements or DROP= dataset options:&lt;BR /&gt;
&lt;PRE&gt;data ...(drop=i);
...
do i=1 to ...;
  ...
end;
...
run;&lt;/PRE&gt;
&lt;/LI&gt;
&lt;LI&gt;In many cases where the value of the index variable &lt;EM&gt;is&lt;/EM&gt; written to the output dataset, this is done by an &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1lltvbis7ye1an1eryo4leh2mck.htm" target="_blank" rel="noopener"&gt;OUTPUT statement&lt;/A&gt; within the loop. This, in turn,&amp;nbsp;&lt;SPAN&gt;overrides the implicit OUTPUT at the end of the DATA step iteration, so that the "ugly" last value is automatically left out. Insert an OUTPUT statement into your DO loop and you'll get 4*12=48 observations from your sample data and the assignment statement for &lt;FONT face="courier new,courier"&gt;Month&lt;/FONT&gt; can be deleted.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;Other common types of DO loops use UNTIL or WHILE conditions (or a &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n03wnjww9jjpm8n1q16exvgtoae9.htm" target="_blank" rel="noopener"&gt;LEAVE statement&lt;/A&gt;) preventing the index variable (if any) from being incremented beyond the stop value. In your example you could change the DO statement to either of the following forms and again omit the final assignment statement for&amp;nbsp;&lt;FONT face="courier new,courier"&gt;Month&lt;/FONT&gt;.&lt;BR /&gt;
&lt;PRE&gt;do Month=1 by 1 until(Month=12);&lt;/PRE&gt;
&lt;PRE&gt;do Month=1 by 1 while(Month&amp;lt;=12);&lt;/PRE&gt;
&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Mon, 24 May 2021 14:37:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-Do-Loops-Summining/m-p/743380#M232732</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-05-24T14:37:21Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative Do Loops - Summining</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-Do-Loops-Summining/m-p/743388#M232734</link>
      <description>&lt;P&gt;If you really want to keep MONTH to indicate how many months ahead the calculated amount represents then use a different variable for the loop counter.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;months=12 ;
do i=1 to months ;
  ....
end;
drop i;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 May 2021 15:30:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-Do-Loops-Summining/m-p/743388#M232734</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-24T15:30:58Z</dc:date>
    </item>
  </channel>
</rss>

