EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials SAS Tutorial SAS DO Loop
 

SAS DO Loop

Updated March 16, 2023

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

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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.

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()

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

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

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
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
EDUCBA Login

Forgot Password?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW