Previous Page | Next Page

Language Reference

DO Statement

DO ;
statements ;
END ;

The DO statement specifies that the statements that follow the DO statement be executed as a group until a matching END statement appears. DO statements often appear in IF-THEN/ELSE statements, where they designate groups of statements to be performed when the IF condition is true or false.

For example, consider the following statements:

x=0;
y=1;
if x<y then
   do;
      z1 = abs(x+y);
      z2 = abs(x-y);
   end;

The statements between the DO and END statements (called the DO group) are executed only if x < y. That is, they are executed only if all elements of x are less than the corresponding elements of y. If any element of x is not less than the corresponding element of y, the statements in the DO group are skipped and the statement that follows the END statement is executed.

It is good practice to indent the statements in a DO group as shown in the preceding example. However, the DO and END statements do not need to be on separate lines. A popular indenting style is to write the DO statement on the same line as the THEN or ELSE clause, as shown in following statements:

if x<y then do;
   z1 = abs(x+y);
   z2 = abs(x-y);
end;
else do;
   z1 = abs(x-y);
   z2 = abs(x+y);
end;

DO groups can be nested. There is no limit imposed on the number of nested DO groups. The following statements show an example of nested DO groups:

if x<y then do;
   if z1>2 then do;
      z = z1 - z2;
      w = x # y;
   end;
end;
Previous Page | Next Page | Top of Page