DATA Step, Macro, Functions and more

Do Until with a SET statement logic

Accepted Solution Solved
Reply
Contributor
Posts: 54
Accepted Solution

Do Until with a SET statement logic

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

 

 

 

 


Accepted Solutions
Solution
‎04-07-2016 12:32 PM
Super User
Posts: 5,081

Re: Do Until with a SET statement logic

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


All Replies
Respected Advisor
Posts: 3,777

Re: Do Until with a SET statement logic

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.

Solution
‎04-07-2016 12:32 PM
Super User
Posts: 5,081

Re: Do Until with a SET statement logic

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.

Contributor
Posts: 54

Re: Do Until with a SET statement logic

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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 292 views
  • 2 likes
  • 3 in conversation