Introduction

Welcome to Edition 15 of Making SAS Accessible to Everyone. In this edition, we turn our attention to one of the most fundamental programming tools in SAS: the DO loop. Repetitive tasks are ubiquitous in data preparation and simulation workflows, and SAS provides flexible and powerful ways to automate such tasks using looping constructs.

DO loops allow programmers to:

  • Iterate over a range of values.

  • Control when and how code blocks are executed.

  • Minimize redundancy by avoiding manual repetition of code.

  • Dynamically generate observations or computations within DATA steps.

Whether you are simulating multiple outcomes, restructuring datasets, or producing tabulated values programmatically, understanding DO loops is essential for efficient and scalable SAS programming.

In this edition, we will cover:

  • Iterative DO loops

  • Conditional DO WHILE and DO UNTIL loops

  • Nesting and combining loops

  • Use of OUTPUT for controlling dataset creation

  • Practical applications to reinforce learning


1. Iterative DO Loops

Basic Structure

An iterative DO loop is used when the number of repetitions is known in advance.

Syntax:

DO index = start TO stop <BY increment>;
    ...statements...
END;

Simple Example

DATA example1;
    DO i = 1 TO 5;
        square = i**2;
        OUTPUT;
    END;
RUN;

This code creates a dataset with 5 observations, each containing a value of i and its square.

Using BY

DO i = 10 TO 50 BY 10;

This iterates through 10, 20, 30, 40, 50. The BY option controls the step size.

Reverse Iteration

DO i = 5 TO 1 BY -1;

Used for descending sequences.


2. DO Loops Within a DATA Step

DO loops can be embedded within a DATA step to create multiple rows from one input row, or to perform repeated calculations.

Example:

DATA example2;
    SET input_data;
    DO trial = 1 TO 3;
        Result = trial * Score;
        OUTPUT;
    END;
RUN;

This replicates each observation 3 times, modifying the Result value in each iteration.

OUTPUT Placement

The placement of OUTPUT determines how many rows are written:

  • Inside the loop: multiple rows per observation.

  • Outside the loop: one row, reflecting the final state of the loop variables.


3. Conditional DO Loops

Conditional loops are useful when the number of iterations depends on dynamic conditions.

DO UNTIL

Executes at least once.

DO UNTIL (balance >= 10000);
    balance + 1000;
    OUTPUT;
END;

Used when an action must happen first, then be checked.

DO WHILE

Checks condition before execution.

DO WHILE (balance < 10000);
    balance + 1000;
    OUTPUT;
END;

May not execute at all if the initial condition is false.


4. Nested and Combined DO Loops

Nested Iteration

Useful for generating grids, cross-tabulations, or multi-dimensional simulations.

Example: Multiplication Table

DATA mult_table;
    DO row = 1 TO 5;
        DO col = 1 TO 5;
            Product = row * col;
            OUTPUT;
        END;
    END;
RUN;

Iteration with Conditions

DATA budget_plan;
    total = 0;
    DO month = 1 TO 12 UNTIL (total > 10000);
        income = 1000;
        total + income;
        OUTPUT;
    END;
RUN;

Blends iteration with early exit logic.


5. Practical Example: Simulating Test Scores

A realistic scenario often involves repeated sampling or scoring.

Simulate two test attempts for each of three students:

DATA test_scores;
    DO student_id = 1 TO 3;
        DO attempt = 1 TO 2;
            score = 50 + RANUNI(1234) * 50;
            OUTPUT;
        END;
    END;
RUN;

The result is a dataset with 6 observations (2 per student), each with a random score.


Conclusion

In Edition 15, you have learned:

  • How to implement iterative DO loops for fixed repetitions.

  • How to apply conditional DO WHILE and DO UNTIL loops for logic-based repetition.

  • The significance of OUTPUT in shaping the structure of your datasets.

  • How to use nested loops to simulate two-dimensional data relationships.

DO loops are a core tool in any SAS programmer’s toolkit, enabling you to build flexible, repeatable data processes with minimal code. When used effectively, they can greatly enhance both performance and code readability.


What’s Next

In Edition 16, we move from repetitive code to restructuring tables in SAS.


Loop smart, loop clean – with 3 D Statistical Learning.

Special thanks to Dr. Dany Djeudeu for his ongoing contributions to simplifying the SAS learning experience for all.