SAS, a command-driven statistical analysis and data visualization tool, is one of the most widely used statistical software tools across industries. A few of its applications include application development, data warehousing, report writing, and data management. It is platform-agnostic, meaning it'll run on practically any operating system, including Ubuntu, Mac OS, Windows, Linux, and others.

In this tutorial, we'll look at how Loops are used in SAS to do programming tasks and operations.

The Ultimate Data Science Job Guarantee Program

6 Month Data Science Course With a Job GuaranteeJoin Today
The Ultimate Data Science Job Guarantee Program

What Is SAS Loop?

Loops are one of the most beginner concepts that every programmer needs to know, whether in C, C++, Python, or SAS. Loops are a crucial feature of any program because they allow us to perform calculations and operations for a given set of parameters the required number of times.

There are different types of Loops in SAS:

  • DO LOOP
  • DO WHILE
  • DO UNTIL

Now let us now dive deeper into the details of all these loops.

Types of SAS Loops 

As discussed earlier, there are three types of loops in SAS, namely - DO LOOP, DO WHILE, and DO UNTIL. 

SAS Loops - DO LOOP

Syntax

DO value = start TO stop

Example

data data_bin; 

do i = 1 to 4;   

y = i**2; 

output; 

end; 

run;

Output

2, 5, 9, 16, 25 

The SAS loop comes to a close with the END statement. Each iteration of a DO statement advances the counter value by one by default, but to increment the counter by other values, including non-integer values, we can use the BY option.

For instance, each iteration of the following DATA step increases the value of I by 0.3:

data data_bin;

do i = 1 to 5 by 0.3;  

y = i**2; 

output;

end; 

run;

Output

1, 2.25, 4, ..., 16, 20.25, 25

Free Course: Introduction to Data Science

Learn the Fundamentals of Data ScienceEnroll Now
Free Course: Introduction to Data Science

SAS Loops - DO WHILE

We can iterate while a condition is true by using the DO statement with a WHILE clause. Because the condition is tested before each iteration, we need to set up the stopping condition before starting the loop.

The following example builds on the DATA step example, iterating as long as k is less than 20. The WHILE condition is not satisfied when j=4; therefore, the loop iterates once more.

Syntax

DO <value> while(<condition>)...end;

Example

data data_bin;

 k = 0; 

do j = 1 to 5 by 0.5 while(k < 20);   

k = j**2;

output; 

end; 

run;

Output

1, 2.25, 4, ..., 16, 20.5

SAS Loops - DO UNTIL

You can iterate until a condition is true with a UNTIL clause along with the DO statement. The UNTIL condition does not need to be initialized because it is evaluated towards the end of the DO loop.

Since the condition evaluates towards the end of the loop, a DO loop with a UNTIL clause always runs at least once. To prevent this from happening, we can use a DO loop with a WHILE clause.

Syntax

Do until(<condition>)...end;

Example

data carloan_until;

 loan = 30000;

 payments = 0;

 do until (loan = 0);

  loan = loan - 500;

  payments = payments + 1;

 end;

run;

Output

SAS_Loops_1

DO WHILE VS DO UNTIL

The main difference between a DO WHILE and  DO UNTIL is typically this -

  • Using a WHILE clause, we iterate as long as the condition of the DO loop holds
  • Using a UNTIL clause, we iterate until a certain condition holds.

Now let us see how we can combine these different kinds of DO Loops in SAS to implement a powerful loop to get the best out of a program.

Data Scientist Master's Program

In Collaboration with IBMExplore Course
Data Scientist Master's Program

Combining Conditional and Iterative Do Loops

When both the iterative and conditional loops are combined into a united loop, the power of DO loops in SAS is multiplied by some factor. Let us look at a solid example below to understand this in a better way.

Example

To Determine the Number of Years in Order to Save $20,000 Within a College Fund.

In this case, we need to know how much money we need to keep aside each month and how long it will take to save $20,000 for college. An average person expects to attend college at the age of 18. We would not consider the number of years to surpass 18.

Considering that we do not have any money in our savings account yet, we will start by setting the “fund” value in the SAS program as zero. To generate currency-formatted output values, the dollar 8.2 format is applied to the fund variable. 

Since we do not want to loop longer than 18 years, we begin the DO loop by initializing the index variable “j” to 1 and ending after 18. We need to add the until condition since we only want to preserve $20,000.

do j = 1 to 18 until(fund >= 20000);

SAS runs the loop either until 18 years have passed or the fund has surpassed $20,000, whichever comes first.

We need to set aside $1000 each year in a savings account with a 1% yearly interest rate. To accommodate this, with each iteration, the value of "fund" is increased by 1000, followed by being multiplied by 1.01 to add 1% to the fund's value in interest. It can be easily tracked how many years it would take to reach $20,000 by setting the variable "Years" equal to the index variable i.

data college_fund;

 fund = 0;

 format fund dollar8.2;

 do j = 1 to 18 until(fund >= 20000);

  fund = (fund + 1000) * 1.01;

  years = j;

    output;

 end;

 drop j; 

 run;  

As seen in the result below, putting aside $1000 each year into a savings account earning a 1% return per year will give us just a little less than $20,000 after 18 years:

SAS_Loops_2

We can notice a different result if we change the annual deposit amount to 1200:

data data_bin;

 fund = 0;

 format fund dollar8.2;

 do j = 1 to 18 until(fund >= 20000);

  fund = (fund + 1200) * 1.01;

  years = j;

    output;

 end;

 drop j; 

run;  

The loop now stops execution after 16 iterations, as seen in the modified dataset below, because 16 years was enough for the education fund to go beyond the threshold of $20,000.

SAS_Loops_3

Are you considering a profession in the field of Data Science? Then get certified with the Data Science Bootcamp Program today!

Take Advantage of the World’s No.1 Data Science Program to Master SAS

In this article, we explored the loops in SAS and how each loop can be used in the execution of various types of programs. There are typically three types of loops in SAS - DO LOOP, DO WHILE, and DO UNTIL. 

We use the different loops in different contexts according to our usage. We use DO loops with the WHILE clause when we want the loop to run as long as the condition holds but with the UNTIL when we want the loop to run until the condition holds. The power of all three loops can also be combined to implement a better and more powerful loop in a program which we saw through the college fund program.

There are more such concepts in SAS that require learning and mastering in order to be a better professional in the various roles related to data science. Simplilearn offers a comprehensive Data Science Bootcamp for such enthusiasts to learn, explore, and be better prepared for SAS and data science. So, what are you waiting for? Check out our expert-crafted course now!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.