Chapter Contents |
Previous |
Next |
SAS Component Language: Reference |
There are four forms of the DO statement:
For more information about DO statements, in addition to the information in this documentation, refer to SAS Language Reference: Dictionary.
DO Statement |
DO; |
. . .SAS statements. . . |
END; |
if years>5 then do; months=years*12; put years= months=; end;
Iterative DO Loops |
DO index-variable = start TO stop <BY increment>; |
Note: In SCL applications, both start and stop
are required, and start, stop, and increment must be numbers or expressions that yield a number.
The TO and BY clauses cannot be reversed, and start
cannot be a series of items separated by commas. You can use only one start TO stop specification (with
or without the BY clause) in a DO loop.
If increment is not specified, then index-variable is increased by 1. If increment is positive, then start must be the lower bound and stop must the be upper bound for the loop. If increment is negative, then start must be the upper bound and stop must be the lower bound for the loop.
The values of start, stop, and increment are evaluated before the first execution of the loop. Any changes made to stop or increment within the DO group do not affect the number of times that the loop executes.
For example, the following code prints the numbers 20, 18, 16, 14, 12, and 10.
dcl num k=18 n=11; do i=k+2 to n-1 by -2; put i; end;
The following code uses the DOPEN and DNUM functions to execute SAS statements once for each file in the current directory:
rc=filename('mydir','.'); dirid=dopen('mydir'); do i=1 to dnum(dirid); ...SAS statements... end; rc=dclose(dirid);
You can add either an UNTIL clause or a WHILE clause to your DO statements.
DO index-variable = start TO stop <BY increment> |
<WHILE (expression)> | <UNTIL (expression)>; |
If index-variable is still in the range between start and stop, then if you specify an UNTIL clause, the DO group will execute until the UNTIL expression is true. If you specify a WHILE clause, the loop will execute as long as the WHILE expression is true.
The following example uses an UNTIL clause to set a flag, and then it checks the flag during each iteration of the loop:
flag=0; do i=1 to 10 until(flag); ...SAS statements... if expression then flag=1; end;
The following loop executes as long as I is within the
range of 10 to 0 and MONTH is equal to JAN
.
do i=10 to 0 by -1 while(month='JAN'); ...SAS statements... end;
DO WHILE Statement |
DO WHILE (expression); |
. . .SAS statements. . . |
END; |
For example, the following DO loop is executed once for each value of N: 0, 1, 2, 3, and 4.
n=0; do while(n<5); put n=; n+1; end;
DO UNTIL Statement |
DO UNTIL (expression); |
. . .SAS statements. . . |
END; |
Whether the loop executes is based solely on whether the expression that you specify evaluates to true or false. The loop is always executed at least once, and the expression is evaluated after the loop executes.
For example, the following DO loop is executed once for each value of N: 0, 1, 2, 3, and 4.
n=0; do until(n>=5); put n=; n+1; end;
Controlling DO Loops (CONTINUE and LEAVE) |
You can use the CONTINUE and LEAVE statements to control the flow of execution through DO loops.
The CONTINUE statement stops the processing of the current
DO loop iteration and resumes with the next iteration of the loop. For example,
the following code reads each row in the DEPT table, and if the status is
not PT
, it displays a frame that enables the
user to update the full-time employee's salary.
deptid=open('dept'); call set(deptid); do while (fetch(deptid) ne -1); if (status='PT') then continue; newsal=display('fulltime.frame'); end;
The LEAVE statement stops processing the current DO loop and resumes with the next statement after the DO loop. With the LEAVE statement, you have the option of specifying a label for the DO statement:
LEAVE <label>; |
myloop: do i=1 to 10; do j=1 to 10; if j=5 then leave myloop; put i= j=; end; end; put 'this statement executes next'; return;
In SCL applications, the LEAVE statement can be used only within DO loops, not in SELECT statements (unless it is enclosed in a DO statement).
For more information, refer to CONTINUE, LEAVE, and SAS Language Reference: Dictionary.
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.