EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials SAS Tutorial SAS DO Loop
Secondary Sidebar
SAS Tutorial
  • Basic
    • What is SAS
    • Advantages of SAS
    • Career in SAS
    • SAS Command
    • SAS Operators
    • SAS Export to CSV
    • Loops in SAS
    • DO WHILE in SAS
    • SAS System Interview Questions
    • SSAS Interview Questions
    • SAS Visual Analytics
    • SAS Join
    • SAS Length
    • SAS INTNX
    • SAS Congruence
    • SAS LAG Function
    • SAS boxplot
    • SAS INTCK
    • SAS Find
    • SAS Grid
    • SAS Triangle Calculator
    • SAS KEEP
    • SAS YEAR Function
    • SAS Format
    • SAS Libname
    • SAS Rename
    • SAS Retain
    • SAS Base
    • SAS JMP
    • SAS Array
    • SAS Round
    • SAS DO Loop
    • SAS Index
    • SAS Datalines
    • SAS Model
    • SAS Logistic Regression
    • SAS Numeric to Character
    • SAS Label
    • SAS ODS
    • SAS Upcase
    • SAS Numeric Formats
    • SAS Nodupkey

SAS DO Loop

Introduction to SAS DO Loop

The SAS DO Loop is one of the statements that enables to execution of the group of statements that access between the do and end statement. It repetitively iterated till the value of the variable index. It may be the condition like < less than or greater than > for other variables in the multiple form iteration, like do until and do while statements in the SAS data set.

SAS DO Loop

What is SAS DO Loop?

The iterative do loops are the most widely used and are the simplest form of loop execution within the SAS data step. Iterative loop action will vary and other unconditional execution at the n number of times without stopping the errors during data execution for blocking the unwanted codes. At least once, it will execute and then repeatedly continue till the block or stops the loop execution. It depends upon the boolean condition the loop will execute and terminate out of the loop at the end of the block. In SAS, looping using the do statement with the SAS do index value of the variable till the loop is started and continues to integrate the index variable.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

SAS DO Loop in Data Step

The SAS do loop is the simplest form of the do loop, which can be executed within the SAS data step. The actions are iterated at the do loop conditional and unconditional execution. It has n number of times for data processing execution and provided the most straightforward way for data appropriation in the SAS data steps variable groups. In SAS, looping is accomplished using the do statement, also known as the do loop for processing the steps and the End keyword.

The code block will help to check and validate the condition like if the condition is true, then it executes the rest of the statements; else, if it is false, then it terminates the loop. Iterative do loops stands for worth until the clause is executed at least once for iterating the do loop statement for repeatedly or consecutively decrement the order values for each set of variables assigned on the input and output data set.

Steps to create DO Loop in SAS:

1. log in to the below URL.

2. https://odamid-apse1-2.oda.sas.com/SASStudio/main?locale=en_GB&zone=GMT%252B05%253A30&ticket=ST-74834-Ob2htQRJ6CqpMCnEGmef-cas

log in to the URL

3. Type the below code as an example using the do loop.

4. data First;

5. do inp = 1 to 10;

6. res = inp**2;

7. output;

8. end;

9. run;

SAS DO Loop 2

10. When executing the above code, we get the below sample output.

executing the above code

SAS DO Loop Iterative with Examples

The SAS do loop iteration is incremented at the values of the counter by using the user input values. The iterative statement also mainly enabled the execution of the group of statements and repeatedly decremented the values from the variable. Whenever we use the iterative index on the do statement, it allows us to execute the group of statements between the doing and end-based loop, repetitively working on the variable index. While putting the do loop within the do loop called the nesting key ideas, are most often useful for each data pass in the outer ring loop with repeated data actions in the inner loop.

Iterative do statement is mainly enabled for executing the statement group on the do and end keyword, similar to the repeat index. Do must enable the index variable, which helps to start the value and stop it even if it will increment the value for either numbers or expressions based on the user evaluation with a specific number of times during the particular intervals. When compared to while loop iteration, the clause will use long as certain conditions until it holds.

Example #1

Code:

data FirstSetofInputs;
do inp = 1 to 13 by 1.7;
res = inp**3;
output;
end;
run;

Output:

SAS DO Loop 4

Explanation:

  • Here we have declared the data as FirstSetofInputs, which will be initialized first.
  • Then by using the do loop, we can declare the input variable in a separate variable and assign some values.

data as FirstSetofInputs

  • After that, we can mention the condition like 1 to 13 divided by 1.7.
  • It will be performed the mathematical calculations, and finally, it will display the output results on the table.

Example #2

Code:

data second;
ins = 0;
do sec = 1 to 9 by 1.5 while(ins < 16);
ins = sec**3;
output;
end;
run;

Output:

while to check the conditions

  • The second example used the same as the first example.
  • But here, additionally, we used a while to check the conditions.
  • The do loop will iterate once, and the while loop will start the execution the second time.

SAS DO Loop Arrays

In SAS, do loop arrays are made easily to triage and transform the datas with index-based values. It is nothing but a set of variables of the same type that can be performed using the same operation referenced by the DATA keyword. Generally, the variables in the array are represented as the elements of the array. The do loop will iterate the values each time it visits the array elements, so it’s nothing but the traversing of the array. The index value is initially 0, and the loop will execute till the condition like the length of the array that the coder initializes.

Example:

Code:

data temp;
input a b c d$ e$;
cards;
1 2 3 AP BS
2 3 4 AQ BT
3 4 5 AR BU
;
run;
data thr;
set third;
array var (*) _numeric_;
do in = 1 to dim(var);
if var{i} > 5 then var{in} =.;
end;
drop in;
run;

Output:

do loop in the array statement

total rows

  • The above example used the do loop in the array statement.
  • Here we declared five columns and assigned the values with the help of variables.

and assigned the values

  • Next, we can write the do loop statement to validate the inputs using the conditions.

Conclusion

In SAS, many loop conditions and statements are used to perform data operations under certain situations. For that, do loop is one of the condition statements, and it will validate the user datas with the required input variables assigned and used for the data operations in SAS.

Recommended Articles

This is a guide to SAS DO Loop. Here we discuss the introduction, SAS DO loop in a data step, SAS DO loop iterative, and arrays. You may also have a look at the following articles to learn more –

  1. SAS Visual Analytics
  2. Data Architects
  3. Data Engineer Role
  4. SASS if()
Popular Course in this category
SAS Training (9 Courses, 10+ Projects)
  9 Online Courses |  10 Hands-on Projects |  123+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Data Visualization Training (15 Courses, 5+ Projects)4.9
Business Intelligence Training (12 Courses, 6+ Projects)4.8
Primary Sidebar
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