SAS Loop – Do Loop, Do While & Do Until loop in SAS
In this SAS Programming Tutorial, we discuss SAS Loop, like all other programming languages, SAS also has Loops.
Moreover, we see three important types of loops in SAS: SAS DO Loop, SAS DO WHILE loop, and SAS DO UNTIL Loop with their Syntax and examples for programming. Let us understand SAS Loop with some example.
What is SAS Loop
Whenever we hear the word programming, the first thought that comes to our mind is large blocks of code, written using loops. Looping is the first step that a programming beginner learns.
Loops are an important part of a program because they help us to repeat calculations multiple numbers of times for a given set of parameters.
Different languages use different keywords to define the iteration statement. The most well-known statement is the “for loop,” which is used by C/C++, MATLAB, R, and other languages. Older languages, such as FORTRAN and SAS, call the iteration statement a “do loop,” but it is exactly the same concept.
Stay updated with latest technology trends
Join DataFlair on Telegram!!
Types of Loops in SAS
There are 3 types of SAS Loops, let’s discuss them one by one:
a. SAS DO Loop
It has the syntax of Do Loop in SAS
DO value = start TO stop
SAS Do Loop Example:-
data A; do i = 1 to 4; y = i**2; /* values are 2, 5, 9, 16, 25 */ output; end; run;
The END statement marks the end of the SAS loop.
By default, each iteration of a DO statement increments the value of the counter by 1, but you can use the BY option to increment the counter by other amounts, including non-integer amounts.
For example, each iteration of the following DATA step increments the value I by 0.3:
data A; do i = 1 to 5 by 0.3; y = i**2; /* values are 1, 2.25, 4, ..., 16, 20.25, 25 */ output; end; run;
b. SAS DO WHILE and DO UNTIL
Sometimes we will want to stop iterating if a certain condition is met. There are two ways to do this: you can use the WHILE clause to iterate as long as a certain condition holds, or you can use the UNTIL clause to iterate until a certain condition holds.
You can use the DO statement with a WHILE clause to iterate while a condition is true. The condition is checked before each iteration, which implies that you should intialize the stopping condition prior to the loop.
The following statements extend the DATA step example and iterate as long as the value of y is less than 20. When i=4, the WHILE condition is not satisfied, so the loop iterates again.
data A; y = 0; do i = 1 to 5 by 0.5 while(y < 20); y = i**2; /* values are 1, 2.25, 4, ..., 16, 20.5 */ output; end; run;
You can use the DO statement with a UNTIL clause to iterate until a condition becomes true. The UNTIL condition is evaluated at the end of the loop so it does not need to be initialized.
It is worth noting that a DO loop with a UNTIL clause always executes at least one time because the condition evaluates at the end of the loop. To prevent such behavior, you can use a DO loop with a WHILE clause.
Hence, we learn SAS Loop, types of Loops in SAS: SAS Do Loop, SAS Do While loop, SAS Do Until Loop with their example and syntax.
In conclusion, we think these were similar to the loops you learned in other languages, and easier too. Such is the beauty of this language. For any doubts, please comment in the comment section below.