Published on :
SAS Programming CREATION_INTERNE

Examples: DO Loops

This code is also available in: Deutsch Español Français
Awaiting validation
Detailed functional analysis of DO loops. DO loops are flow control structures used to repeatedly execute a group of statements. They can be iterative (with an index, a list, or a sequence), or conditional (as long as a condition is true or until a condition becomes true). The presented examples cover these different forms, demonstrating their application for data manipulation, interest calculation, and other scenarios. It is emphasized that the syntax and behavior are identical whether the DATA step runs on the SAS© compute server or in the CAS environment, although execution in CAS may sometimes occur in a single thread if no input table is specified.
Data Analysis

Type : CREATION_INTERNE


Examples use generated data (datalines) or SASHELP.

1 Code Block
DATA STEP Data
Explanation :
This example uses an iterative DO statement to repeatedly decrement the values of the 'balance' variable and write these values to the output dataset. The DATA step executes on the SAS compute server.
Copied!
1DATA loan;
2 balance=10000;
3 DO payment_number=1 to 10;
4 balance=balance-1000;
5 OUTPUT;
6 END;
7RUN;
8PROC PRINT DATA=loan;
9RUN;
2 Code Block
DATA STEP Data
Explanation :
This example uses the same iterative DO loop to execute the DATA step in CAS and create a CAS output table. Although the DATA step executes in CAS, it runs in a single thread because no input table is specified.
Copied!
1cas casauto sessopts=(caslib='casuser');
2LIBNAME mylib cas;
3 
4DATA mylib.loan;
5 balance=10000;
6 DO payment_number=1 to 10;
7 balance=balance-1000;
8 OUTPUT;
9 END;
10RUN;
11PROC PRINT DATA=mylib.loan;
12RUN;
3 Code Block
DATA STEP Data
Explanation :
This example shows how to nest a DO loop within another to calculate the value of a one-year investment with an annual interest rate of 7.5%, compounded monthly. Nesting is useful when you need to repeat an internal action for each pass of the outer loop.
Copied!
1DATA earn;
2 Capital=2000;
3 DO Year=1 to 10;
4 DO Month=1 to 12;
5 Interest=Capital*(.075/12);
6 Capital+Interest;
7 OUTPUT;
8 END;
9 END;
10RUN;
11PROC PRINT DATA=earn; RUN;
4 Code Block
DATA STEP Data
Explanation :
This example uses DOLIST syntax to iterate over a list of values, expressions, or sequences. The DO loop index successively takes the values specified in the list, which are separated by commas.
Copied!
1DATA do_list;
2 x=-5;
3 DO i=5, /*a single value*/
4 5, 4, /*multiple values*/
5 x + 10, /*an expression*/
6 80 to 90 BY 5, /*a sequence*/
7 60 to 40 BY x; /*a sequence with a variable*/
8 OUTPUT;
9 END;
10RUN;
11PROC PRINT DATA=do_list; RUN;
5 Code Block
DATA STEP Data
Explanation :
This example uses the iterative DO TO-<value> syntax to repeatedly increment the values of 'x'. The OUTPUT statement is used to write each incremented value of 'x' to the output dataset. The 'TO' value specifies the final value of the index variable.
Copied!
1DATA do_to;
2 x=0;
3 DO i=0 to 10;
4 x=x+1;
5 OUTPUT;
6 END;
7RUN;
8PROC PRINT DATA=do_to;
9RUN;
6 Code Block
DATA STEP Data
Explanation :
This example uses the iterative DO TO-<value> BY-<increment> syntax to repeatedly increment the values of 'x'. The 'BY-<increment>' value specifies that the index variable is incremented by '2' with each execution of the loop. The OUTPUT statement writes each incremented value of 'x' to the output dataset.
Copied!
1DATA do_to_by;
2 x=0;
3 DO i=0 to 10 BY 2;
4 x=x+1;
5 OUTPUT;
6 END;
7RUN;
8PROC PRINT DATA=do_to_by;
9RUN;
7 Code Block
DATA STEP Data
Explanation :
This first DATA step uses a DO loop to repeatedly execute a group of statements as long as a condition is true. The program calculates the number of payments required for a specified loan amount by repeatedly decrementing the 'Balance' as long as the balance is greater than zero. The DATA step executes on the SAS compute server.
Copied!
1DATA loan;
2 balance=10000;
3 payment=0;
4 DO while (balance>0);
5 balance=balance-1000;
6 payment=payment+1;
7 OUTPUT;
8 END;
9RUN;
10PROC PRINT DATA=mylib.loan;
11RUN;
8 Code Block
DATA STEP Data
Explanation :
This DATA step loads the same data into a new table in a different library, 'mylib.loan', and then calculates the number of payments required for the specified loan. It does this by repeatedly iterating and decrementing the 'Balance' values until the balance is zero. The DATA step executes in CAS but in a single thread because there is no input table.
Copied!
1LIBNAME mylib cas;
2 
3DATA mylib.loan;
4 balance=10000;
5 payment=0;
6 DO until (balance=0);
7 balance=balance-1000;
8 payment=payment+1;
9 OUTPUT;
10 END;
11RUN;
12PROC PRINT DATA=mylib.loan;
13RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Copyright © SAS Institute Inc. All Rights Reserved