MET MA 603: SAS Programming and Applications
MET MA 603:
SAS Programming and Applications
The Do Loop
1
1
The Do Loop with an Increment
The Do Loop is a structure used to execute statements over more than one iteration, based on an index variable.
For example, consider the summation notation used in mathematics:
Here, the index is i, which takes on the values from 1 to n, in increments of 1. In this example, each value of i is added together. The implementation of this type of calculation in SAS is done with the Do Loop with an Increment.
2
2
The Do While and Do Until Loops
The Do While Loop iterates statements as long as a specified condition is true.
The Do Until Loop iterates statements as long as a specified condition is false.
Note that with the Do While and Do Until Loops, there is no default increment, as there is with the Do Loop with an Increment. It is very important to specify an increment, or the resulting code may create an infinite loop!
The Do Loop with an Increment requires that the last index value be specified. Sometimes, this value isn’t known. In these situations, the Do While or Do Until Loop must be used.
3
3
Practice
Use SAS to calculate the sum of the even numbers between 1 and 100. Use the MOD function and a Do Loop with increments of 1. The MOD function is explained below:
MOD(number, divisor) Returns the remainder when number is divided by divisor. For example, MOD(10, 3) = 1, MOD(10, 4) = 2 and MOD(10, 5) = 0.
Repeat the calculation above, but using only a Do Loop with increments of 2.
4
4
Readings
https://v8doc.sas.com/sashtml/sclr/z1024090.htm
5
5
/docProps/thumbnail.jpeg