Compound Statements

Several statements can be grouped together into a compound statement (also called a block or a DO group). You use a DO statement to define the beginning of a DO group and an END statement to define the end. DO groups have two principal uses:

  • to group a set of statements so that they are executed as a unit

  • to group a set of statements for a conditional (IF-THEN/ELSE) clause

DO groups have the following general form:

DO ;

    statements ;

END ;

As with IF-THEN/ELSE statements, you can nest DO groups to any number of levels. The following is an example of nested DO groups:

\begin{tabbing} \= {\small \verb!do;!} \= \\ \> \> \Emph{statements}{\small \verb!;!} \\ \> \> {\small \verb!do;!} \= \\ \> \> \> \Emph{statements}{\small \verb!;!} \\ \> \> {\small \verb!end;!} \\ \> \> \Emph{statements}{\small \verb!;!} \\ \> {\small \verb!end;!} \end{tabbing}

It is a good programming convention to indent the statements in DO groups as shown, so that each statement’s position indicates its level of nesting.

For IF-THEN/ELSE conditionals, DO groups can be used as units for either the THEN or ELSE clauses so that you can perform many statements as part of the conditional action, as shown in the 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;

An alternative formulation that requires less indented space 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;

For some programming applications, you must use either a DO group or a module. For example, LINK and GOTO statements must be programmed inside a DO group or a module.