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
The Output Statement within a Do Loop
Combining the Output statement with the Do Loop allows for a single observation to generate multiple observations in the output dataset.
data odd_numbers;
do Number = 1 to 100;
output;
end;
run;
4
4
Calculating Sums
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.
5
5
Investments
Starting in 2019 you invest $1000 at the beginning of the year. Each subsequent year you increase your investment by $100. Your investments earn a 7% return, paid at the end of the year and you reinvest the gain.
After three years, your investment will be worth $1000:
1000 * 1.07 = 1070.00
(1070 + 1100) * 1.07 = 2221.90
(2221.90 + 1200) * 1.07 = 3768.43
Using this investment plan, calculate the number of years it will take for you to exceed $25,000.
6
6
Readings
Textbook section (6th edition) 3.12
https://v8doc.sas.com/sashtml/sclr/z1024090.htm
7
7
/docProps/thumbnail.jpeg