DATA Step, Macro, Functions and more

DO WHILE vs. DO UNTIL

Reply
Occasional Contributor
Posts: 6

DO WHILE vs. DO UNTIL

[ Edited ]

 Differences between DO WHILE and DO UNTIL statements with examples ?

Super User
Posts: 6,971

Re: DO WHILE vs. DO UNTIL

[ Edited ]

DO WHILE Statement

DO UNTIL Statement

 

Maxim 1: Read the Documentation

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 6,971

Re: DO WHILE vs. DO UNTIL

PS and use a more descriptive subject line in the future. "Base SAS" in an exclusively SAS-oriented forum is, ahem, not very bright.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,424

Re: DO WHILE vs. DO UNTIL

What is the question here?  What you have posted appears to be a demand for examples of some SAS syntax.  If you require examples of something specific the manual is fully encompassing, or there are several books out there.  If you have a specific question, please formulate a full explanation, provide example test data in the form of a datastep, what you want out etc.  

SAS Super FREQ
Posts: 685

Re: DO WHILE vs. DO UNTIL

Something to remember:

 

DO WHILE, the loop executes 0 - n times

DO UNTIL, the loop executes 1 - n times

 

 

Super User
Posts: 10,539

Re: DO WHILE vs. DO UNTIL


Sheelapolakonda wrote:

 Differences between DO WHILE and DO UNTIL statements with examples ?


Not a SAS specific question even. These are generic programming concepts.Wikipedia https://en.wikipedia.org/wiki/Do_while_loop  has examples in something like 19 different programming languages.

Super User
Super User
Posts: 6,502

Re: DO WHILE vs. DO UNTIL

[ Edited ]

Two main differences.  

  1. The DO WHILE() will test before executing the code in the loop and DO UNTIL() will test after executing the code.
  2. The tests are backwards. DO WHILE() continues when the condition is TRUE and DO UNTIL() continues when the condition is FALSE.

You use the DO UNTIL () form if you want to insure that the loop always runs at least once.

 

Not sure if these are good examples for you, but they are things that I use frequently.

Process a dataset by groups of records needs to use DO UNTIL() so that the SET statement always runs at least once.

do until (last.id);
   set mydata ;
   by id ;
   ....
end;

Read all records from a text file needs to test if the file has anymore records to prevent SAS from stopping the data step when the INPUT statement reads past the end of the file.

infile 'myfile.csv' dsd firstobs=2 truncover end=eof ;
do while (not eof);
   input a b c ;
   ...
end;

 

Regular Contributor
Posts: 200

Re: DO WHILE vs. DO UNTIL

Tom has covered the basics;
this page has several examples:

http://www.sascommunity.org/wiki/Do_which_loop_until_or_while
Ask a Question
Discussion stats
  • 7 replies
  • 341 views
  • 2 likes
  • 7 in conversation