%DO Statement

Begins a %DO group.
Type: Macro statement
Restriction: Allowed in macro definitions only
See: %END Statement


text and macro language statements


The %DO statement designates the beginning of a section of a macro definition that is treated as a unit until a matching %END statement is encountered. This macro section is called a %DO group. %DO groups can be nested.
A simple %DO statement often appears in conjunction with %IF-%THEN/%ELSE statements to designate a section of the macro to be processed depending on whether the %IF condition is true or false.

Example: Producing One of Two Reports

This macro uses two %DO groups with the %IF-%THEN/%ELSE statement to conditionally print one of two reports.
%macro reportit(request);
   %if %upcase(&request)=STAT %then
         proc means;
            title "Summary of All Numeric Variables";
   %else %if %upcase(&request)=PRINTIT %then
         proc print;
            title "Listing of Data";
   %else %put Incorrect report type. Please try again.;
%mend reportit;
Specifying stat as a value for the macro variable REQUEST generates the PROC MEANS step. Specifying printit generates the PROC PRINT step. Specifying any other value writes a customized error message to the SAS log.