Published on :
Language Fundamentals CREATION_INTERNE

Demonstration of DO Loops and Iterative Processing

This code is also available in: Deutsch Español Français
Awaiting validation
This program presents a series of progressive examples to understand how loops work in SAS©. It covers the basic syntax of an iterative loop, the impact of the OUTPUT statement to control observation writing, increment variations (positive, negative, decimal step), iteration over a list of specific values, nested loops, as well as conditional DO WHILE and DO UNTIL loops.
Data Analysis

Type : CREATION_INTERNE


All tables are dynamically created using loops and variable assignments within DATA steps.

1 Code Block
DATA STEP Data
Explanation :
Creation of table 'simple1' with a simple loop from 1 to 4. Without an explicit OUTPUT statement inside the loop, writing to the table occurs implicitly at the end of the DATA step (a single final observation containing the last values of i and answer).
Copied!
1/*
2Demonstration: Simple Illustration of How DO Loops Work
3*/
4DATA simple1;
5 DO i = 1 to 4;
6 answer = 1 + i;
7 END;
8RUN;
9 
10PROC PRINT DATA=simple1 noobs;
11RUN;
2 Code Block
DATA STEP Data
Explanation :
Creation of 'simple2'. Adding the OUTPUT statement inside the loop forces an observation to be written at each iteration. The resulting table will therefore have 4 rows.
Copied!
1/*
2Demonstration: Using the Output Statement
3*/
4DATA simple2;
5 DO i = 1 to 4;
6 answer = 1 + i;
7 OUTPUT;
8 END;
9RUN;
10 
11PROC PRINT DATA=simple2 noobs;
12RUN;
3 Code Block
DATA STEP Data
Explanation :
Similar to the previous example, but the iteration variable 'i' is dropped from the final table using the (drop=i) option.
Copied!
1*Simple example #3;
2DATA simple3 (drop=i);
3 DO i = 1 to 4;
4 answer = 1 + i;
5 OUTPUT;
6 END;
7RUN;
8 
9PROC PRINT DATA=simple3 noobs;
10RUN;
4 Code Block
DATA STEP Data
Explanation :
Demonstration of a loop with an increment (BY) of 2. The loop iterates for i = 0, 2, 4, ..., 10.
Copied!
1*Simple example #4;
2DATA simple4 (drop=i);
3 DO i = 0 to 10 BY 2;
4 answer = 1 + i;
5 OUTPUT;
6 END;
7RUN;
8 
9PROC PRINT DATA=simple4 noobs;
10RUN;
5 Code Block
DATA STEP Data
Explanation :
Using a decimal increment (0.25). The loop iterates more finely between 0 and 10.
Copied!
1*Simple example #5;
2DATA simple5 (drop=i);
3 DO i = 0 to 10 BY .25;
4 answer = 1 + i;
5 OUTPUT;
6 END;
7RUN;
8 
9PROC PRINT DATA=simple5 noobs;
10RUN;
6 Code Block
DATA STEP Data
Explanation :
Example of a decremental loop (countdown) using a negative step (BY -1), ranging from 10 to 1.
Copied!
1*Simple example 6;
2DATA simple6 (drop=i);
3 DO i = 10 to 1 BY -1;
4 answer = 1 + i;
5 OUTPUT;
6 END;
7RUN;
8 
9PROC PRINT DATA=simple6 noobs;
10RUN;
7 Code Block
DATA STEP Data
Explanation :
Iteration over an explicit list of non-sequential values (10, 12, 23, 147).
Copied!
1*Simple example 7;
2DATA simple7 (drop=i);
3 DO i = 10, 12, 23, 147;
4 answer = 1 + i;
5 OUTPUT;
6 END;
7RUN;
8 
9PROC PRINT DATA=simple7 noobs;
10RUN;
8 Code Block
DATA STEP Data
Explanation :
Illustration of nested loops. Note: the inner loop 'do j = i**2;' is not a classic iterative loop (no 'to'), it executes once by assigning the calculated value to j. The OUTPUT is triggered at each pass.
Copied!
1*Nesting do loops;
2DATA nested;
3 DO i = 1 to 10;
4 DO j = i**2;
5 OUTPUT;
6 END;
7 END;
8RUN;
9 
10PROC PRINT DATA=nested noobs;
11RUN;
9 Code Block
DATA STEP Data
Explanation :
Usage of DO WHILE. The condition is evaluated at the beginning of the loop. If 'answer' is missing initially, the condition might be false immediately or require initialization (here, SAS implicitly initializes to 0 for the sum instruction 'answer + 1', so the loop works).
Copied!
1*Example of using do while;
2DATA simple8;
3 DO while (answer < 11);
4 answer + 1;
5 OUTPUT;
6 END;
7RUN;
8 
9PROC PRINT DATA=simple8 noobs;
10RUN;
10 Code Block
DATA STEP Data
Explanation :
Usage of DO UNTIL. The condition is evaluated at the end of the loop, ensuring that the code inside executes at least once.
Copied!
1*Example of using do until;
2DATA simple9;
3 DO until (answer > 9);
4 answer + 1;
5 OUTPUT;
6 END;
7RUN;
8 
9PROC PRINT DATA=simple9 noobs;
10RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.