Both DO WHILE and DO UNTIL loops in SAS Programming repeat a block of code based on a condition, but they differ fundamentally in when SAS evaluates that condition and what keeps the loop running.
Table of Contents
Difference between DO WHILE and DO UNTIL loops in SAS Programming
| Feature | DO WHILE | DO UNTIL |
| Condition Check | Top of the loop (before execution) | Bottom of the loop (after execution) |
| Minimum Executions | 0 (If false at start, never runs) | 1 (Always runs at least once) |
| Continuation Logic | Executes WHILE the condition is TRUE | Executes UNTIL the condition becomes TRUE (runs while FALSE) |
Conceptual Pseudocode DO WHILE and DO UNTIL Loops in SAS Programming
/* DO WHILE: Check first, then act */
DO WHILE (condition is TRUE);
/* SAS code */
END;
/* DO UNTIL: Act first, then check */
DO UNTIL (condition is TRUE);
/* SAS code */
END;
DO WHILE Example
Use DO WHILE loop when you want to loop only while a target threshold has not been met, and you want to prevent execution entirely if the initial state already meets the criteria.
data financial_projection;
balance = 5000;
target = 10000;
interest_rate = 0.05;
year = 0;
/* Loop continues WHILE balance is less than target */
do while (balance < target);
year + 1;
balance = balance * (1 + interest_rate);
output;
end;
run;
DO UNTIL Example in SAS
Use DO UNTIL loop when an operation must run at least once to calculate an updating value before evaluating whether to stop.
data iterative_process;
value = 100;
iteration = 0;
/* Runs at least once, stops UNTIL error tolerance is reached */
do until (value <= 10);
iteration + 1;
value = value * 0.70; /* Reduce value by 30% each iteration */
output;
end;
run;
Common Pitfalls in SAS Loops
Infinite Loops
If the condition in a DO WHILE loop never becomes FALSE, or the condition in a DO UNTIL loop never becomes TRUE, SAS will enter an infinite loop. Always ensure the key variable inside the loop is updated!
Evaluating Initial State
If balance starts at $12,000 with a $10,000 target:
DO WHILE (balance < target)executes 0 times.DO UNTIL (balance >= target)executes 1 time before evaluating the condition.
Real-Life SAS Applications
SAS (Statistical Analysis System) is heavily used in enterprise environments for large-scale data manipulation, advanced analytics, biostatistics, and regulatory reporting.
1. Clinical Trials & Pharmaceuticals (CDISC Compliance)
Pharmaceutical companies use SAS to format data according to CDISC standards (SDTM and ADaM models) for submission to regulatory bodies like the US FDA and EMA. Loops like DO UNTIL are often used in risk modeling or missing value imputation algorithms.
2. Banking & Credit Risk Modeling (CECL / IFRS 9)
Risk analysts run SAS macro loops (DO WHILE / DO UNTIL) to simulate thousands of economic scenarios (e.g., rising interest rates, unemployment spikes) across millions of credit card accounts to determine capital reserves required by law.
3. Health Insurance & Fraud Detection
Healthcare providers process millions of insurance claim lines. SAS algorithms parse patient history chronologically using DO loops to detect pattern anomalies, such as duplicate billing within a 24-hour window or improbable treatment sequences.
4. Retail & Supply Chain Forecasting
Retail giants use SAS to project how store inventory will depreciate over time. A DO WHILE loop can iteratively subtract daily sales projections from warehouse inventory until stock drops below safety stock levels, automatically triggering reorder points.
Learn R Programming Language
