BookmarkSubscribeRSS Feed
HeatherNewton
Quartz | Level 8
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] > curr_bal_P[j] then compbal_inc[i]=1; else compbal_inc[i]=0;
	end;

	flag_bal=i;
	if flag_bal<=5 then do;
		if bal=0 then do;
			if compbal_inc[i]=1 then con=con+1;else bal=1;
		end;
	end;
end;
run;

a few questions:

1. we have i but not j, why j is not in test? also why is flag_bal not in test?

2. we realise bal is always 1, in theory, if i>5, flag_bal>5, bal should remain 0

 

thanks

11 REPLIES 11
Tom
Super User Tom
Super User

Why do you think J is not in the dataset TEST? Check the SAS log to make sure the data step actually ran.

If the code RAN then it will be there.  I and J will have same value, 12 , for all observations.

 

Note that you appear to have not copied the DATA statement for the first data step that is creating the input dataset.

 

                                                                                                            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

HeatherNewton
Quartz | Level 8
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] > curr_bal_P[j] then compbal_inc[i]=1; else compbal_inc[i]=0; 
end; 
  
flag_bal=i; 
if flag_bal<=5 then do; 
if bal=0 then do; 
if compbal_inc[i]=1 then con=con+1;else bal=1; 
end; 
end; 
end; 
run; 

sorry please see updated version with full details of input dataset. 

why I cannot see j and flat_bal in dataset test. log has no errors.

 

what decide what shows on output dataset here, why show i and not j or flat_bal?

 

PaigeMiller
Diamond | Level 26

I see J and FLAG_BAL in data set TEST.


Where are you looking? Please be specific and detailed. Show us a screen capture.

--
Paige Miller
HeatherNewton
Quartz | Level 8

oh I can see now for some strange reason both were unticked

 

what about the value of bal being 1 always?

PaigeMiller
Diamond | Level 26

@HeatherNewton wrote:

 

what about the value of bal being 1 always?


because of this:

 

 

if compbal_inc[i]=1 then con=con+1;else bal=1; 

 

 

--
Paige Miller
HeatherNewton
Quartz | Level 8

but when i >5, that line is not run, so bal should be 0

 

right now bal =1 for all i

why?

 

PaigeMiller
Diamond | Level 26

When i>5 that line is not run, and so BAL retains its value from previous iterations where i<=5, in which BAL has the value of 1

--
Paige Miller
HeatherNewton
Quartz | Level 8
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] > curr_bal_P[j] then compbal_inc[i]=1; else compbal_inc[i]=0; 
end; 
  
flag_bal=i; 
if flag_bal<=5 then do; 
if bal=0 then do; 
if compbal_inc[i]=1 then con=con+1;else bal=1; 
end; 
end; 
end; 
run; 

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.

 

Kurt_Bremser
Super User

The iterative DO loop works like this:

i = 1;
start:
if i > 11 then go to end;
  /* loop code */
  i = i + 1:
go to start;
end:
/* next code */

or, written a s a DO WHILE:

i = 1;
do while (i le 11);
  /* loop code */
  i = i + 1;
end;

The loop will terminate when i exceeds the end value, not when it reaches it.

 

(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)

ballardw
Super User

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:

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;

There are other ways such as using i= 10 to 1 by -1 ; 

 

You really need to learn to indent code. Nested loops get a whole lot easier to debug/follow when properly indented.

PaigeMiller
Diamond | Level 26

@HeatherNewton wrote:
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] > curr_bal_P[j] then compbal_inc[i]=1; else compbal_inc[i]=0; 
end; 
  
flag_bal=i; 
if flag_bal<=5 then do; 
if bal=0 then do; 
if compbal_inc[i]=1 then con=con+1;else bal=1; 
end; 
end; 
end; 
run; 

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.

 


When an iterative DO loop ends, the value is always 1 greater than the last value.

 

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.

 

do i=1 to 11; 
    j=i+1; 
    if curr_bal_P[j] not in (.) then do; 
        if curr_bal_P[i] > curr_bal_P[j] then compbal_inc[i]=1; 
        else compbal_inc[i]=0; 
    end; 
  
    flag_bal=i; 
    if flag_bal<=5 then do; 
         if bal=0 then do; 
            if compbal_inc[i]=1 then con=con+1;
            else bal=1; 
        end; 
    end; 
end; 

 

 

--
Paige Miller

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 11 replies
  • 1003 views
  • 0 likes
  • 5 in conversation