<?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: loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806482#M317751</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/416388"&gt;@HeatherNewton&lt;/a&gt;&amp;nbsp;wrote:
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;what about the value of bal being 1 always?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;because of this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if compbal_inc[i]=1 then con=con+1;else bal=1; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 07 Apr 2022 12:23:24 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2022-04-07T12:23:24Z</dc:date>
    <item>
      <title>loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806428#M317719</link>
      <description>&lt;PRE&gt;input apple melon orange;     
datalines;     
0 2 3     
5 3 4      
3 4 5     
0 5 5     
3 1 0     
6 1 0     
7 0 0     
9 2 7   
run;    

data test;
set Portoa;
con=0;
bal=0;
array curr_bal_P[*] curr_bal_P1 -curr_bal_P12;
array compbal_inc[*] compbal_inc1 - compbal_inc12;

do i=1 to 11;
	j=i+1;
	if curr_bal_P[j] not in (.) then do;
		if curr_bal_P[i] &amp;gt; curr_bal_P[j] then compbal_inc[i]=1; else compbal_inc[i]=0;
	end;

	flag_bal=i;
	if flag_bal&amp;lt;=5 then do;
		if bal=0 then do;
			if compbal_inc[i]=1 then con=con+1;else bal=1;
		end;
	end;
end;
run;&lt;/PRE&gt;
&lt;P&gt;a few questions:&lt;/P&gt;
&lt;P&gt;1. we have i but not j, why j is not in test? also why is flag_bal not in test?&lt;/P&gt;
&lt;P&gt;2. we realise bal is always 1, in theory, if i&amp;gt;5, flag_bal&amp;gt;5, bal should remain 0&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thanks&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2022 03:27:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806428#M317719</guid>
      <dc:creator>HeatherNewton</dc:creator>
      <dc:date>2022-04-07T03:27:15Z</dc:date>
    </item>
    <item>
      <title>Re: loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806430#M317721</link>
      <description>&lt;P&gt;Why do you think J is not in the dataset TEST?&amp;nbsp;Check the SAS log to make sure the data step actually ran.&lt;/P&gt;
&lt;P&gt;If the code RAN then it will be there.&amp;nbsp; I and J will have same value, 12 , for all observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that you appear to have not copied the DATA statement for the first data step that is creating the input dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;                                                                                                            c   c   c
                                                            c   c   c   c   c   c   c   c   c   c   c   c   o   o   o
                        c   c   c   c   c   c   c   c   c   u   u   u   o   o   o   o   o   o   o   o   o   m   m   m
                        u   u   u   u   u   u   u   u   u   r   r   r   m   m   m   m   m   m   m   m   m   p   p   p
                        r   r   r   r   r   r   r   r   r   r   r   r   p   p   p   p   p   p   p   p   p   b   b   b
                        r   r   r   r   r   r   r   r   r   _   _   _   b   b   b   b   b   b   b   b   b   a   a   a              f
                        _   _   _   _   _   _   _   _   _   b   b   b   a   a   a   a   a   a   a   a   a   l   l   l              l
            o           b   b   b   b   b   b   b   b   b   a   a   a   l   l   l   l   l   l   l   l   l   _   _   _              a
    a   m   r           a   a   a   a   a   a   a   a   a   l   l   l   _   _   _   _   _   _   _   _   _   i   i   i              g
    p   e   a           l   l   l   l   l   l   l   l   l   _   _   _   i   i   i   i   i   i   i   i   i   n   n   n              _
O   p   l   n   c   b   _   _   _   _   _   _   _   _   _   P   P   P   n   n   n   n   n   n   n   n   n   c   c   c              b
b   l   o   g   o   a   P   P   P   P   P   P   P   P   P   1   1   1   c   c   c   c   c   c   c   c   c   1   1   1              a
s   e   n   e   n   l   1   2   3   4   5   6   7   8   9   0   1   2   1   2   3   4   5   6   7   8   9   0   1   2    i    j    l

1   0   2   3   0   1   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   12   12   11
2   5   3   4   0   1   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   12   12   11
3   3   4   5   0   1   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   12   12   11
4   0   5   5   0   1   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   12   12   11
5   3   1   0   0   1   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   12   12   11
6   6   1   0   0   1   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   12   12   11
7   7   0   0   0   1   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   12   12   11
8   9   2   7   0   1   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   12   12   11

&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Apr 2022 03:39:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806430#M317721</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-07T03:39:40Z</dc:date>
    </item>
    <item>
      <title>Re: loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806436#M317725</link>
      <description>&lt;PRE&gt;Data Portoa;      
input apple melon orange;      
datalines;      
0 2 3      
5 3 4       
3 4 5      
0 5 5      
3 1 0      
6 1 0      
7 0 0      
9 2 7    
run;     

  

data test; 
set Portoa; 
con=0; 
bal=0; 
array curr_bal_P[*] curr_bal_P1 -curr_bal_P12; 
array compbal_inc[*] compbal_inc1 - compbal_inc12; 


do i=1 to 11; 
j=i+1; 
if curr_bal_P[j] not in (.) then do; 
if curr_bal_P[i] &amp;gt; curr_bal_P[j] then compbal_inc[i]=1; else compbal_inc[i]=0; 
end; 
  
flag_bal=i; 
if flag_bal&amp;lt;=5 then do; 
if bal=0 then do; 
if compbal_inc[i]=1 then con=con+1;else bal=1; 
end; 
end; 
end; 
run; &lt;/PRE&gt;
&lt;P&gt;sorry please see updated version with full details of input dataset.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;why I cannot see j and flat_bal in dataset test. log has no errors.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;what decide what shows on output dataset here, why show i and not j or flat_bal?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2022 06:17:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806436#M317725</guid>
      <dc:creator>HeatherNewton</dc:creator>
      <dc:date>2022-04-07T06:17:27Z</dc:date>
    </item>
    <item>
      <title>Re: loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806476#M317748</link>
      <description>&lt;P&gt;I see J and FLAG_BAL in data set TEST.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Where are you looking? Please be specific and detailed. Show us a screen capture.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2022 11:48:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806476#M317748</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-04-07T11:48:33Z</dc:date>
    </item>
    <item>
      <title>Re: loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806480#M317750</link>
      <description>&lt;P&gt;oh I can see now for some strange reason both were unticked&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;what about the value of bal being 1 always?&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2022 12:20:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806480#M317750</guid>
      <dc:creator>HeatherNewton</dc:creator>
      <dc:date>2022-04-07T12:20:26Z</dc:date>
    </item>
    <item>
      <title>Re: loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806482#M317751</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/416388"&gt;@HeatherNewton&lt;/a&gt;&amp;nbsp;wrote:
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;what about the value of bal being 1 always?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;because of this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if compbal_inc[i]=1 then con=con+1;else bal=1; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2022 12:23:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806482#M317751</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-04-07T12:23:24Z</dc:date>
    </item>
    <item>
      <title>Re: loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806509#M317769</link>
      <description>&lt;P&gt;but when i &amp;gt;5, that line is not run, so bal should be 0&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;right now bal =1 for all i&lt;/P&gt;
&lt;P&gt;why?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2022 13:39:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806509#M317769</guid>
      <dc:creator>HeatherNewton</dc:creator>
      <dc:date>2022-04-07T13:39:28Z</dc:date>
    </item>
    <item>
      <title>Re: loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806513#M317773</link>
      <description>&lt;P&gt;When i&amp;gt;5 that line is not run, and so BAL retains its value from previous iterations where i&amp;lt;=5, in which BAL has the value of 1&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2022 13:55:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806513#M317773</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-04-07T13:55:48Z</dc:date>
    </item>
    <item>
      <title>Re: loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806706#M317884</link>
      <description>&lt;PRE&gt;Data Portoa;      
input apple melon orange;      
datalines;      
0 2 3      
5 3 4       
3 4 5      
0 5 5      
3 1 0      
6 1 0      
7 0 0      
9 2 7    
run;     

data test; 
set Portoa; 
con=0; 
bal=0; 
array curr_bal_P[*] curr_bal_P1 -curr_bal_P12; 
array compbal_inc[*] compbal_inc1 - compbal_inc12; 

curr_bal_P[1]=1000;
curr_bal_P[2]=900;
curr_bal_P[3]=800;
curr_bal_P[4]=700;
curr_bal_P[5]=600;
curr_bal_P[6]=500;
curr_bal_P[7]=400;
curr_bal_P[8]=300;
curr_bal_P[9]=200;
curr_bal_P[10]=100;
curr_bal_P[11]=50;
curr_bal_P[12]=30;

do i=1 to 11; 
j=i+1; 
if curr_bal_P[j] not in (.) then do; 
if curr_bal_P[i] &amp;gt; curr_bal_P[j] then compbal_inc[i]=1; else compbal_inc[i]=0; 
end; 
  
flag_bal=i; 
if flag_bal&amp;lt;=5 then do; 
if bal=0 then do; 
if compbal_inc[i]=1 then con=con+1;else bal=1; 
end; 
end; 
end; 
run; &lt;/PRE&gt;
&lt;P&gt;I added values for the arrays and run again. it seems ok. I can see the i and j but I don't get why i equals 12, thought it is 11 as max.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Apr 2022 07:04:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806706#M317884</guid>
      <dc:creator>HeatherNewton</dc:creator>
      <dc:date>2022-04-08T07:04:00Z</dc:date>
    </item>
    <item>
      <title>Re: loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806717#M317894</link>
      <description>&lt;P&gt;The iterative DO loop works like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;i = 1;
start:
if i &amp;gt; 11 then go to end;
  /* loop code */
  i = i + 1:
go to start;
end:
/* next code */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or, written a s a DO WHILE:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;i = 1;
do while (i le 11);
  /* loop code */
  i = i + 1;
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The loop will terminate when i exceeds the end value, not when it reaches it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(my first code with the GO TOs is very near the machine code that the SAS compiler creates; in all programming languages, loops are done with conditional jumps, as that is what machine codes provide)&lt;/P&gt;</description>
      <pubDate>Fri, 08 Apr 2022 07:40:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806717#M317894</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-04-08T07:40:20Z</dc:date>
    </item>
    <item>
      <title>Re: loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806725#M317901</link>
      <description>&lt;P&gt;When you have a fairly simple value assigned as in your example you might consider using the power of the index value of an array to calculate the assignment. Maybe. This doesn't do ALL of the initial values but is an example:&lt;/P&gt;
&lt;PRE&gt;data example; 
   array curr_bal_P[*] curr_bal_P1 -curr_bal_P12 ;
/* instead of 
curr_bal_P[1]=1000;
curr_bal_P[2]=900;
curr_bal_P[3]=800;
curr_bal_P[4]=700;
curr_bal_P[5]=600;
curr_bal_P[6]=500;
curr_bal_P[7]=400;
curr_bal_P[8]=300;
curr_bal_P[9]=200;
curr_bal_P[10]=100;
*/
   do i= 1 to 10;
     curr_bal_p[i] = (11-i)*100;
   end; 

run;&lt;/PRE&gt;
&lt;P&gt;There are other ways such as using i= 10 to 1 by -1 ;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You really need to learn to indent code. Nested loops get a whole lot easier to debug/follow when properly indented.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Apr 2022 08:44:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806725#M317901</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-04-08T08:44:26Z</dc:date>
    </item>
    <item>
      <title>Re: loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806738#M317907</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/416388"&gt;@HeatherNewton&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;Data Portoa;      
input apple melon orange;      
datalines;      
0 2 3      
5 3 4       
3 4 5      
0 5 5      
3 1 0      
6 1 0      
7 0 0      
9 2 7    
run;     

data test; 
set Portoa; 
con=0; 
bal=0; 
array curr_bal_P[*] curr_bal_P1 -curr_bal_P12; 
array compbal_inc[*] compbal_inc1 - compbal_inc12; 

curr_bal_P[1]=1000;
curr_bal_P[2]=900;
curr_bal_P[3]=800;
curr_bal_P[4]=700;
curr_bal_P[5]=600;
curr_bal_P[6]=500;
curr_bal_P[7]=400;
curr_bal_P[8]=300;
curr_bal_P[9]=200;
curr_bal_P[10]=100;
curr_bal_P[11]=50;
curr_bal_P[12]=30;

do i=1 to 11; 
j=i+1; 
if curr_bal_P[j] not in (.) then do; 
if curr_bal_P[i] &amp;gt; curr_bal_P[j] then compbal_inc[i]=1; else compbal_inc[i]=0; 
end; 
  
flag_bal=i; 
if flag_bal&amp;lt;=5 then do; 
if bal=0 then do; 
if compbal_inc[i]=1 then con=con+1;else bal=1; 
end; 
end; 
end; 
run; &lt;/PRE&gt;
&lt;P&gt;I added values for the arrays and run again. it seems ok. I can see the i and j but I don't get why i equals 12, thought it is 11 as max.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;When an iterative DO loop ends, the value is always 1 greater than the last value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please indent your DO loops to make your code easier to read and easier to debug. This will help you and help us. It is worth the effort to do this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i=1 to 11; 
    j=i+1; 
    if curr_bal_P[j] not in (.) then do; 
        if curr_bal_P[i] &amp;gt; curr_bal_P[j] then compbal_inc[i]=1; 
        else compbal_inc[i]=0; 
    end; 
  
    flag_bal=i; 
    if flag_bal&amp;lt;=5 then do; 
         if bal=0 then do; 
            if compbal_inc[i]=1 then con=con+1;
            else bal=1; 
        end; 
    end; 
end; &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, 08 Apr 2022 10:08:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop/m-p/806738#M317907</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-04-08T10:08:22Z</dc:date>
    </item>
  </channel>
</rss>

