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.
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.
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;
10. When executing the above code, we get the below sample output.
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:
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.
- 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:
- 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:
- 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.
- 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 –