BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
saskapa
Quartz | Level 8

Hi All,

I don't understand the logic of using the a do until loop in this context.

 

here is a data:

 

 

data ab;
input a  b ;
cards;
1    1
1    1  
1    2  
2    1  
2    1   
2    2  
2    2 
;

 

The following code works fine, it uses a do Until loop before the SET statment :

 

 

data out ;               
   do until (last.a) ;   
      do until (last.b) ;
         set ab ;        
         by a b ;   
      end ;                     
   end ;                 
run ;

it output last.a and last.b.

 

 

a   b

-   -

1  2

2  2

 

 

Can someone explain me the difference with this code ?  In this code the loop is after the SET statment.

 

data out ;  
    set ab ; 
    by a b ;              
     do until (last.a) ;   
        do until (last.b) ;    
put a= b=; end ; end ; run ;

The loop keeps iterating and stays at value value a=1 and b=1. Why does the loop stuck  on those valueand does not iterate to the next records as in the first case above ? And why does the loop in the first case not stuck ?

 

Thanks in advance

 

saskapa

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

The longer answer:  you need to understand the SET statement.  When it executes, it is not merely a label that tells SAS where to find observations.  Rather, it is an instruction to read the next observation.  That's just one observation, not a bunch of them.

 

So in the top program, each iteration through the loop reads a single observation.  Eventually, the UNTIL condition becomes true.

 

In the bottom program, the SET statement reads the first observation.  Then the loop begins, and the data values never change because no additional observations are being read.

 

At an introductory level of using SAS, students tend to think of the SET statement as a label to indicate the source of observations.  The real process is much more complex than that.

View solution in original post

3 REPLIES 3
data_null__
Jade | Level 19

The short answer is when you DO UNTIL you need logic in the loop that will eventually cause the until expression to become true.  Otherwise infinite loop.

Astounding
PROC Star

The longer answer:  you need to understand the SET statement.  When it executes, it is not merely a label that tells SAS where to find observations.  Rather, it is an instruction to read the next observation.  That's just one observation, not a bunch of them.

 

So in the top program, each iteration through the loop reads a single observation.  Eventually, the UNTIL condition becomes true.

 

In the bottom program, the SET statement reads the first observation.  Then the loop begins, and the data values never change because no additional observations are being read.

 

At an introductory level of using SAS, students tend to think of the SET statement as a label to indicate the source of observations.  The real process is much more complex than that.

saskapa
Quartz | Level 8

Thanks Astounding. I understand better know..Besides, it is  looks obvious now  why my second code leads to a contiuous loopwhile in my first code not.

 

saskapa

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
  • 3 replies
  • 2319 views
  • 2 likes
  • 3 in conversation