Development SAS VIYA CAS

DO WHILE vs. DO UNTIL: Choosing the Right Loop for Your SAS Data Step

This code is also available in: Deutsch Español

Difficulty Level
Intermediate
Published on :
Simon

Expert Advice

Simon
Expert SAS et fondateur.

The most critical distinction between DO WHILE and DO UNTIL is evaluation timing. A DO WHILE loop checks the condition at the top; if the condition isn't met initially, the code block is skipped entirely. In contrast, DO UNTIL checks the condition at the bottom, guaranteeing that the loop executes at least once.

When using these in SAS Viya or CAS, pay close attention to your "exit strategy." Because DO UNTIL evaluates at the end, an incorrect logical expression (like checking for balance=0 when calculations result in a tiny decimal like 0.00001) can trigger an infinite loop. This can hang your session and consume significant cloud resources. Always ensure your increment or decrement logic will eventually meet the exit criteria, especially when dealing with floating-point financial data.

DO loops in SAS© allow repetitive execution of a group of statements. There are several forms of iterative loops: simple, nested for complex processing (e.g., compound interest calculation), DOLIST for iterating over a list of values, DO TO for specifying an end value, and DO TO BY for defining a specific increment. Conditional loops like DO WHILE (executes as long as a condition is true) and DO UNTIL (executes until a condition becomes true) offer precise control over the flow of execution. The syntax and behavior of these loops are identical whether the DATA step runs on the SAS© Compute server or in the CAS environment.
Data Analysis

Type : INTERNAL_CREATION


Examples use generated data (datalines) or SASHELP.

1 Code Block
DATA STEP Data
Explanation :
This example uses an iterative DO statement to repeatedly decrement the 'balance' variable values and write these values to the output dataset. The DATA step runs on the SAS Compute server.
Copied!
1DATA loan;
2 balance=10000;
3 DO payment_number=1 to 10;
4 balance=balance-1000;
5 OUTPUT;
6 END;
7RUN;
8PROC PRINT DATA=loan;
9RUN;
2 Code Block
DATA STEP Data
Explanation :
This example uses the same iterative DO loop to execute the DATA step in CAS and create an output CAS table.
Copied!
1cas casauto sessopts=(caslib='casuser');
2LIBNAME mylib cas;
3 
4DATA mylib.loan;
5 balance=10000;
6 DO payment_number=1 to 10;
7 balance=balance-1000;
8 OUTPUT;
9 END;
10RUN;
11PROC PRINT DATA=mylib.loan;
12RUN;
Result
3 Code Block
DATA STEP Data
Explanation :
This example shows how to nest one DO loop within another to calculate the value of a one-year investment earning 7.5% annual interest, compounded monthly.
Copied!
1DATA earn;
2 Capital=2000;
3 DO Year=1 to 10;
4 DO Month=1 to 12;
5 Interest=Capital*(.075/12);
6 Capital+Interest;
7 OUTPUT;
8 END;
9 END;
10RUN;
11PROC PRINT DATA=earn; RUN;
Result
4 Code Block
DATA STEP Data
Explanation :
This example uses DOLIST syntax to iterate over a list of values, including single, multiple, expressions, and sequences with or without variables.
Copied!
1DATA do_list;
2 x=-5;
3 DO i=5, /*a single value*/
4 5, 4, /*multiple values*/
5 x + 10, /*an expression*/
6 80 to 90 BY 5, /*a sequence*/
7 60 to 40 BY x; /*a sequence with a variable*/
8 OUTPUT;
9 END;
10RUN;
11PROC PRINT DATA=do_list; RUN;
Result
5 Code Block
DATA STEP Data
Explanation :
This example uses iterative DO TO syntax to repeatedly increment the values of 'x'. The loop also uses the OUTPUT statement to write each incremented 'x' value to the output dataset.
Copied!
1DATA do_to;
2 x=0;
3 DO i=0 to 10;
4 x=x+1;
5 OUTPUT;
6 END;
7RUN;
8PROC PRINT DATA=do_to;
9RUN;
Result
6 Code Block
DATA STEP Data
Explanation :
This example uses iterative DO TO-value BY-increment syntax to repeatedly increment the values of 'x' by '1'. The BY-increment value specifies that the index variable increments by '2' with each loop execution. The loop also uses the OUTPUT statement to write each incremented 'x' value to the output dataset.
Copied!
1DATA do_to_by;
2 x=0;
3 DO i=0 to 10 BY 2;
4 x=x+1;
5 OUTPUT;
6 END;
7RUN;
8PROC PRINT DATA=do_to_by;
9RUN;
Result
7 Code Block
DATA STEP Data
Explanation :
The first DATA step in this example uses a DO WHILE loop to repetitively execute a group of statements as long as a condition is true. The program calculates the number of payments required for a specified loan amount by repeatedly iterating to decrement 'Balance' as long as the balance is greater than zero. The DATA step runs on the SAS Compute server.
Copied!
1DATA loan;
2 balance=10000;
3 payment=0;
4 DO while (balance>0);
5 balance=balance-1000;
6 payment=payment+1;
7 OUTPUT;
8 END;
9RUN;
10PROC PRINT DATA=mylib.loan;
11RUN;
Result
8 Code Block
DATA STEP Data
Explanation :
This DATA step loads the same data into a table in a different library, 'mylib.loan', and then calculates the number of payments that must be made for the specified loan. It does this by repeatedly iterating and decrementing 'Balance' until the balance is zero.
Copied!
1LIBNAME mylib;
2 
3DATA mylib.loan;
4 balance=10000;
5 payment=0;
6 DO until (balance=0);
7 balance=balance-1000;
8 payment=payment+1;
9 OUTPUT;
10 END;
11RUN;
12PROC PRINT DATA=mylib.loan;
13RUN;
Result
Pro Tip
The critical distinction between these loops lies in the timing of the evaluation: DO WHILE checks the condition at the top and may never execute if the condition is false initially, whereas DO UNTIL checks at the bottom and guarantees at least one iteration, which can cause logic errors or negative balances if your starting value (like 'balance') is already zero.
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.

Related Documentation

Aucune documentation spécifique pour cette catégorie.