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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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