EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials SAS Tutorial Loops in SAS

Loops in SAS

By Priya PedamkarPriya Pedamkar

Loops in SAS

Introduction to loops in SAS

SAS as an analytical solution was created at North Carolina State University in the 1970s, the upcoming decades saw the development of further statistical procedures, the inclusion of better object-oriented component libraries. In the present context, SAS occupies almost 80 percent of the total Statistical analytics market.

A Typical SAS program can be divided into two steps namely the DATA step and PROC step. The DATA Step retrieves and helps in the manipulation of the data whereas the PROC step has functionalities for analyzing the data and perform analytical activities on the data. The Data step can further be divided into two phases which are compilation and Execution phases.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

What is a loop?

In fundamentals of computer programming languages, a loop can be considered as a chain or sequence of executive instructions which is continually performed/repeated/or iterated till a certain condition is achieved. Loops are used for repeatedly execute a block of statements, in the classification of flow in loops we can determine if a loop is an entry controlled loop or if it is exit controlled loop, The programming compiler before Executing the Statements checks for if all conditions associated with the beginning of the loops executions are validated and true, this activity is  Performed by Entry controlled loops.

For Exit Controlled Loop the compiler validates the  Conditions associated with the termination of Loop. During the flow of the loops a certain process is completed, such as getting a parameter or any item from the input data, analysis of the parameters or changing it, followed by a condition which checks the limiting value such as whether a counter(a flag value associated with the loop). For the conditions, if it is satisfied the subsequent instruction directs the sequence to return to the first instruction in the sequence and repeat the sequence. Once the condition has reached, the flow of the compiler moves to programmed instruction branches present outside the loop. Thus a loop is a programming concept that is commonly used for reducing code redundancy and complexity.

Loops in SAS

Below are a number of loops that are as follows:

1. Iterative Do Loops

Do Loop also knowns as Iterative Do Loops are the most basic form of loops that are executed on a SAS dataset in the SAS Data Step. This loop is totally unconditional in nature and executed as per the definition to iterate over the fixed number of counts or until it receives an error resulting in the flow to skip from the data step. This Loop finds its utility in programs used for iterative counting and programs associated with repetitive mathematical or statistical calculations.

Syntax:

do i = n to m;

n and m are counter variables.

2. Conditional Loops

Conditional loops in SAS are the other do loops that are executed over in data steps. These are basically two loops which are Do While and Do until. The difference between the loops is based on the fact that the Do While loops continue executing until the condition for the loop is true, whereas the Do Until loop will execute till the specified condition remains false and the flow moves away from the loop as soon as the condition becomes false. The Do Until loop is executed at the bottom of the loop whereas the Do While Loop is executed at the top of the loop. This gives the major difference between the two loops that are Do Until will execute at least once when used in the program whereas the Do While may not be executed at all as if the condition is not true the flow will not enter the Loops iteration.

Syntax:

do until (condition);
do while (condition);

Examples of SAS Loops

Let us try to understand the concept of loops in SAS using programs:

Example #1: Do loop

code:

data Test;
money = 10000
do i = 1 to 10;
money = money - 1000;
output;
end;
run;

Output

Money i
1 9000 1
2 8000 2
3 7000 3
4 6000 4
5 5000 5
6 4000 6
7 3000 7
8 2000 8
9 1000 9
10 0 10

Explanation:

In the above example, we declare a variable (money) initialized with a value of 10000, the program loops through the counter loaded with an initial value of 0 to iterate over 10 times. Each iteration reduces the value for the variable with 1000. The output statement is executed before the loop ends.

Example #2: Do While Loop

code:

run;
data Test;
money = 10000;
newCounter = 0;
do while (money > 0);
money = money - 1000;
newCounter = newCounter + 1;
end;

Output

money newCounter
1 0 10

Explanation

In the above example, we declare two variables money and newCounter and initialize them with values 10000 and 0 respectively. The data step executes until the money variable value reaches 0 which in this case takes 10 iterations, over each iteration the newCounter variable is incremented by a value of 1. We are using the newCounter as a counter variable in this program.

Example #3: Do Until Loop

code:

data Test;
money = 10000;
newCounter = 0;
do until (money > 0);
money = money - 1000;
newCounter = newCounter + 1;
end;
run;
Output

money newCounter
1 0 10

Explanation

In this example, we are trying to replicate the functionality of the Example of the Do while loop illustrated above however using the do until loop. The variable money and newCounter are initialized with an initial value of 10000 and 0 respectively. The do until the loop is executed over the condition that the money variable is not equal to zero, the flow execution moves out of the loop as soon as the variable value reaches zero which in this case takes 10 iterations.

Conclusion

Thus we have defined the different types of loops in SAS and explained their functionalities based on their flow of control. However, there is an additional loop that has been developed recently to scan over an indexed array. However, it can not be considered a regular loop as it is a composite loop for executing iterative statement over-indexed variables such as an array.

Recommended Articles

This has been a guide to Loops in SAS. Here we discussed the introduction of Loops in SAS, Different types of loops with examples. You can also go through our other suggested articles to learn more –

  1. What is a Programming Language?
  2. Introduction to Loops in C++
  3. Guide to What is SAS
  4. Loops in R
  5. Different Types of Loops with its Advantages
  6. Loops in PowerShell | Types and Benefits
EVIEWS Training
32+ Hours of HD Videos
11 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
MYSQL Certification Course
115+ Hours of HD Videos
18 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
CLOUD COMPUTING Certification Course
141+ Hours of HD Videos
23 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
SPLUNK Training Program
70+ Hours of HD Videos
11 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
SAS PROGRAMMING for Statistics & Data Analysis Course
 161+ Hours of HD Videos
18 Courses
8 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more