SAS DO loops and Group Statements

In the previous blog, we have explained a few control statements with examples.In this blog, aim is to explain Do Loops and Groups Statements with examples.

DO - End Groups

When we use IF  - THEN statement, it allows for only statement to be executed when the condition is true.  In a few scenarios, we may want multiple statements to be executed for a TRUE if condition(s). In such scenarios, we have to use DO-End group statements.

Scenario 1: You are working on an insurance portfolio and want to find distribution of advisor tenure. We have first active date (advisor start date) and last active date (advisor close date). If last active date is null means advisor is still with the insurance provider. So, we need to calculate tenute and also create a flag variable based on whether a IF condition is TRUE or FALSE.

In the above example,  based on whether  last active date is missing or not , two different statement values are assigned. When condition was TRUE, advisor tenure is calculated considering today's date and active advisor flag is assigned value as 1.

DO - End Interative

Do-End executes statements between DO and END repetitively based on the value of an index variable. It is similar to FOR loop in some of the other programming languages.

In the above example index i is initialised with 1 and incremented by 2 in each iteration. The loop will end when i value is more than 100. Since we are assigning value to odd variable and outputting in each iteration, the output dataset has odd numbers between 1 and 100.

Scenario: Create a dataset while has two variables - custID and Spend. CustID takes value from 10001 to 10200, and spend follows normal distributions ( 50+20*normal(3434);) value mean 50 and standard deviation as 20.


Do While loop executes statements repetitively till while a condition is true. The condition is evaluated at the top of the loop before the statements in  the DO loop are executed. When the condition is true, the DO loop iterates, otherwise  it does not enter into the loop, the DO loop does not  iterate even once.


It works similar to Do While loop. DO-UNTIL executes statements in a DO loop repetitively until a condition is true. The condition or expression is evaluated at the bottom of the loop after the statements in the DO loop have been executed, so the DO-UNTIL loop always iterate at least once. If the expression is true, the DO UNTIL loop does not iterate again .

SAS Array and Loops Scenarios. 

Leave a Comment