“Programming isn’t about what you know; it’s about what you can figure out.” – Chris Pine
Loops in SAS
Loops are fundamental to programming because they enable you to repeat a computation for various values 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.
Purpose of DO Loops
DO loops process groups of SAS statements repeatedly, reducing the number of statements required in repetitive calculations.
Syntax of Iterative DO Loops
To construct an iterative DO loop, specify an index variable and the conditions that will execute the loop. These conditions include a start value for the index variable, a stop value, and an increment value. Start, stop, and increment values can be any number, numeric variable, or SAS expression that results in a number.
DO Loop Execution
During each iteration of a DO loop, new values are created in the SAS program data vector. Once the loop’s index value exceeds the stop value, the DO loop stops, and processing continues with the following DATA step statement.
Counting Iterations of DO Loops
A simple way to track DO loop iterations is to create a temporary counting variable, then drop this variable from the data set. Or, include an OUTPUT statement within the DO loop to write an observation for each iteration. This overrides the automatic generation of output at the end of the DATA step.
Decrementing DO Loops
You can decrement a DO loop by specifying a negative value for the BY clause. The start value must be greater than the stop value.
Specifying a Series of Items
You can specify how many times a DO loop executes by listing items in a series; the DO loop will execute once for each item, with the index variable equal to the value of each item. A series can consist of all numeric values, all character values (enclosed in quotation marks), or all variable names (without quotation marks).
Nesting DO Loops
DO loops can run within DO loops, as long as you assign a unique index variable to each loop and terminate each DO loop with its own END statement.
Iteratively Processing Data That Is Read from a Data Set
You can use a DO loop to read a data set and compute the value of a new variable. DO loop start and stop values, for example, can be read from a data set.
Conditionally Executing DO Loops
The DO UNTIL statement executes a DO loop until a condition is true. Because the expression is not evaluated until the bottom of the loop, a DO UNTIL loop will execute at least once. The DO WHILE statement is used to execute a DO loop while a condition is true. Because the DO WHILE statement is evaluated at the top of the DO loop, if the expression is false the first time it is evaluated, the DO loop never executes.
Using Conditional Clauses with the Iterative DO Statement
DO WHILE and DO UNTIL statements can be used within iterative DO loops to combine conditional and unconditional execution.
DO loops provide an easy way to create samples from other data sets. Enclose the SET statement in a DO loop, using the start, stop, and increment values to select the observations. Add the POINT= option to the SET statement, setting it equal to the index variable of the DO loop. Then add a STOP statement to prevent DATA step looping, and add an OUTPUT statement to write DATA step output.
To go to the page where a statement or option was presented, select a link.
- DO index-variable=start TO stop BY increment;
- SAS statements
- DO UNTIL(expression);
- DO WHILE(expression);
- DO index-variable=start TO stop BY increment UNTIL(expression);
- DO index-variable=start TO stop BY increment WHILE(expression);
Simple DO Loop
data work.earn; Value=2000; do year=1 to 20; Interest=value*.075; value+interest; end; run;
Nested DO Loops
data work.earn; do year=1 to 20; Capital+2000; do month=1 to 12; Interest=capital*(.075/12); capital+interest; end; end; run;
data work.invest(drop=i); do i=1 to 10 until(Capital>=50000); Year+1; capital+2000; capital+capital*.10; end; run;
data work.subset; do sample=10 to 5000 by 10; set factory.widgets point=sample; output; end; stop; run;
Points to Remember
- If you do not specify a BY clause, the increment value for DO loops is 1.
- In most cases, the index variable is needed only for processing the DO loop and can be dropped from the data set.
- The index variable is always incremented by one value beyond the stop value unless you terminate the DO loop in some other manner.
- It’s easier to manage nested DO loops if you indent the statements in each loop.