How can a DO WHILE statement be used in SAS? 2

How can a DO WHILE statement be used in SAS?

A DO WHILE statement in SAS is a programming construct that allows repeated execution of a set of SAS statements while a certain condition is met. This statement is used to control the flow of a program by repeating a set of statements until a certain condition is no longer true. The DO WHILE statement is useful for performing tasks such as data validation, data manipulation, and data analysis. It is commonly used in SAS programs to iterate through data sets, perform calculations, and generate reports. The structure of a DO WHILE statement consists of a DO statement with a condition, followed by a set of SAS statements to be executed, and an END statement to mark the end of the loop. This statement is an essential tool for creating efficient and robust programs in SAS.

Use a DO WHILE Statement in SAS


You can use a DO WHILE statement in SAS to do some loop over and over while some condition remains true.

The following examples show two different ways to use this statement in practice.

Example 1: DO WHILE Statement in SAS

The following code shows how to use a DO WHILE statement in SAS to create a dataset that contains two variables called var1 and var2 that continue to generate new values while the value of var1 is less than 100:

/*create dataset using DO WHILE statement*/
data my_data;

var1 = 1;
var2 = 1;

do while(var1 < 100);
    var1 = var1 + var2; 
    var2 = var1 * var2;
    var1 + 1;
    
output;

end;

run;

/*view dataset*/
proc printdata=my_data;

The DO WHILE statement continued to generate new values for var1 and var2whilethe value of var1 was less than 100.

Once the value of var1 exceeded 100, the DO WHILE statement stopped and new values stopped being added to the dataset.

Example 2: DO WHILE Statement with TO Statement in SAS

The following code shows how to use a DO WHILE statement with a TO statement in SAS to create a dataset that contains two variables called var1 and var2 that continue to generate new values while the value of var1 is less than 10:

/*create dataset using DO WHILE statement with TO statement*/
data my_data;

var1 = 0; 

do var2 = 1 to 5 while(var1 < 10);   
    var1 = var2**3;
    
output;

end;

run;

/*view dataset*/
proc printdata=my_data;

The TO statement told SAS to attempt to generate values for var2 ranging from 1 to 5 but only whilethe value of var1 was less than 10.

Once the value of var1 exceeded 10, the DO WHILE statement stopped and new values stopped getting added to the dataset.

The following tutorials explain how to perform other common tasks in SAS:

Cite this article

stats writer (2024). How can a DO WHILE statement be used in SAS?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-a-do-while-statement-be-used-in-sas/

stats writer. "How can a DO WHILE statement be used in SAS?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-a-do-while-statement-be-used-in-sas/.

stats writer. "How can a DO WHILE statement be used in SAS?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-a-do-while-statement-be-used-in-sas/.

stats writer (2024) 'How can a DO WHILE statement be used in SAS?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-a-do-while-statement-be-used-in-sas/.

[1] stats writer, "How can a DO WHILE statement be used in SAS?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can a DO WHILE statement be used in SAS?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top