# SAS Loop – Do Loop, Do While & Do Until loop in SAS

Placement-ready Courses: Enroll Now, Thank us Later!

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.

## 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.

## Conclusion

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.

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

DataFlair Team

The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.