Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
A DO loop in SAS repeats the statements between DO and END inside a DATA step. The iterative form, do i=1 to 10;, runs the group once for each value of an index variable; adding WHILE or UNTIL makes the loop depend on a condition as well, and LEAVE exits it early. This guide shows the syntax for each form, how to write the loop’s results to a dataset with OUTPUT, and where loops usually go wrong. The same loop is also called a SAS do loop, and the plural “do loops in SAS” refers to the same statement.
What the DO loop in SAS is used for in research
The SAS DO statement comes in several forms. The SAS 9.4 Statements Reference describes the simple DO, which only groups statements (typically after IF/THEN), and the iterative DO, which “executes statements between the DO and END statements repetitively, based on the value of an index variable”. The iterative DO is available in the DATA step only. The documentation also describes DO WHILE and DO UNTIL, which run a group while or until a condition holds.
In research programming, loops are the usual way to build a dataset row by row inside a DATA step: a projected value for each year, a grid of parameter values, or repeated draws in a simulation. Looping across the elements of an array is a closely related use; it is covered separately in the guide to the SAS array statement, so this page concentrates on the loop statement itself. For a wider orientation to the language, see coding in SAS.
How to install or access SAS to run a DO loop
DO loops need no add-on: they are part of the base DATA step. SAS describes SAS OnDemand for Academics as a free, cloud-based way to learn and teach SAS, with SAS Studio as the browser-based option for writing programs, working with data and running statistical analysis without installing software locally. Students whose professor has registered a class need a code from the instructor; independent learners can get free SAS Studio access without any college or university affiliation. To start, go to welcome.oda.sas.com, create a SAS profile and sign in. Details of the program are on SAS’s SAS OnDemand for Academics page; the older desktop-style route is described in the guide to the SAS University Edition.
How to write a DO loop in SAS step by step
Iterative DO loop: do i=1 to 10
The documented specification is start [TO stop] [BY increment] [WHILE(expression) | UNTIL(expression)] (square brackets mark optional parts), and at least one specification is required. Work through the steps in order.
- Open a program editor in SAS Studio (or your SAS session) and begin a DATA step:
data growth; - Set the starting value that is not part of the loop:
balance=1000; - Write the DO statement with an index variable:
do year=1 to 3; - Put the statements to repeat between DO and END:
balance=balance*1.05; - Add
output;inside the loop so that each pass writes one observation. The SAS documentation’s own looping example uses OUTPUT inside the loop in the same way. - Close the loop with
end;, finish the DATA step withrun;and submit the program.
data growth; balance=1000; do year=1 to 3; balance=balance*1.05; output; end; run;
Run in order, the loop sets year to 1, 2 and 3, so the dataset growth holds three observations with balances 1050, 1102.5 and 1157.625. Without the OUTPUT statement the DATA step would write only one observation, after the loop has finished, because OUTPUT is what writes an observation each pass.
BY, negative BY and lists of values
BY sets the increment. Its default is 1, and it may be negative: with a negative BY, the start value is the upper bound and the stop value the lower. TO and BY can appear in either order. Examples taken from the documentation:
do count=2 to 8 by 2; do i=n to 1 by -1; do i=.1 to .9 by .1, 1 to 10 by 1, 20 to 100 by 10;
Without TO or BY, the start value can be a list of numeric or character values or variables, and the group runs once per item. When TO or BY is used, the values must be numeric.
do month='JAN','FEB','MAR'; do count=2,3,5,7,11,13,17; do i=var1, var2, var3;
DO WHILE and DO UNTIL
Both forms are conditional. DO WHILE tests its condition before each pass; DO UNTIL tests after each pass. Both can also be attached to an iterative loop, as in do i=1 to 10 while(x lt y); or do i=2 to 20 by 2 until((x/3) gt y); (the documentation writes these with the symbols less-than and greater-than symbols; the mnemonic operators lt and gt are equivalent). In a list, WHILE or UNTIL applies only to the last item in its clause: in DO I=1,2,3 WHILE (condition); the loop runs for I=1 and I=2, and the condition is checked at I=3.
data until_demo; x=1; do until (x ge 100); x=x*2; output; end; run;
Starting at 1, this loop writes x=2, 4, 8, 16, 32, 64 and 128, then stops because the test x ge 100 is checked only after each pass. Because the test comes after the pass, a DO UNTIL group always runs at least once; if x started at 200, it would still write one observation (400), whereas the same loop written as do while (x lt 100); would write none.
Which form to use
| Form | When the condition is checked | Minimum passes | Use it when |
|---|---|---|---|
do i=1 to n; |
No condition; the index runs from start to stop | Zero if start is already past stop | You know how many repetitions you need |
do while(expr); |
Before each pass | Zero | The group should not run at all when the condition starts out false |
do until(expr); |
After each pass | One | The group must run once before the condition makes sense |
do i=1 to n while(expr); |
Index range plus a test before each pass | Zero | You need both a cap on the repetitions and a stopping condition |
The check timing in the second and third rows is stated in the SAS documentation; the minimum passes follow from it, and “Zero if start is already past stop” follows from the start and stop values being evaluated before the loop begins.
Exiting a loop early with LEAVE
The documentation gives a pattern in which LEAVE ends the loop and a following statement reports the exit:
do i=1 to n by m; /* more SAS statements */ if i=10 then leave; end; if i=10 then put 'EXITED LOOP';
The same documentation shows a second way to stop early: assigning the index a value past the stop value, as in if y gt 25 then i=exit;. This works because the stop value is evaluated once, before the loop begins; changing the stop variable inside the loop does not change the number of iterations. The documentation says the preferred form is the plain TO loop, with the index left unmodified.
How to read the output of a DO loop
The result of a DO loop is not a printed value but the observations the DATA step writes. Check three things after a run: the log, for the note giving the number of observations in the new dataset; the dataset itself (proc print data=growth; lists it); and whether the number of rows equals the number of passes you expected. If you wrote OUTPUT inside the loop, expect one row per pass; if you did not, expect one row per DATA step iteration. In the growth example above, three passes with OUTPUT give three rows. For checking the numbers a loop produced, summary procedures such as the one described in PROC UNIVARIATE in SAS are the usual next step.
Reproducibility note for research code: keep the loop bounds in the program (or in a macro variable defined at the top) rather than typing numbers into the middle of the logic, so that a reader can see exactly how many iterations produced a result. The wider practice is discussed under computational reproducibility.
Troubleshooting a DO loop in SAS
- The loop never ends. The SAS documentation identifies modifying the index variable inside the loop as a common cause of infinite loops, and recommends the simple TO loop without changing the index. If you must stop early, use
LEAVEor move the index past the stop value deliberately. - Changing the stop variable does nothing. The stop value is evaluated once before the loop begins, so editing it inside the loop does not change the iteration count.
- A WHILE or UNTIL seems to ignore part of a list. It applies only to the last item in its clause.
- A negative BY counts the wrong way. With a negative increment, start must be the upper bound and stop the lower.
- An error on a list with TO or BY. Lists of character values work only without TO/BY; once you use TO or BY, the values must be numeric.
- The DO statement is rejected. The iterative DO is a DATA step statement, and at least one specification is required after the index variable.
- The dataset has one row instead of many. The loop ran but nothing wrote an observation each pass; add
OUTPUTinside the loop.
Free alternatives to SAS for looping
If you are not using SAS, R and Python have equivalent loop statements. The dataset published with this guide compares the SAS DO loop with the loop statements in the R base manual and the Python tutorial, each limited to what those pages state. For SAS-to-dataset tasks other than looping, see SAS merge.
The R for loop iterates over each element of seq, and if seq has length zero the loop body is skipped. In Python, range(0, 10, 3) gives 0, 3, 6, 9, the end point never being part of the sequence, and range(-10, -100, -30) counts down with a negative step, much like a negative BY in SAS.
Sources
Data: Loop statements in SAS, R and Python (counted, conditional, early exit)
Side-by-side list of the counted-loop, conditional-loop and early-exit statements of SAS, R and Python, compiled from the SAS 9.4 Statements Reference, the R base manual and the Python tutorial.
| Loop feature | SAS | R | Python |
|---|---|---|---|
| Counted loop | do i=1 to 10; | for(var in seq) expr | for i in range(5) |
| Loop over an explicit list of values | do month='JAN','FEB','MAR'; | for(var in seq) expr (each element of seq) | for w in words: |
| Condition tested before each pass | do while(expr); | while(cond) expr | while |
| Exit the loop early | leave; | break | break |
Sources
- SAS 9.4 Statements Reference: DO Statement, Iterative (accessed 2026-11-17)
- R base manual: Control Flow (accessed 2026-11-17)
- The Python Tutorial: More Control Flow Tools (accessed 2026-11-17)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







