Previous Page | Next Page

Macro Statements

%DO Statement



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

Syntax
Details
Example
Producing One of Two Reports

Syntax

%DO;
text and macro language statements
%END;


Details

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


Example 1: 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
      %do;
         proc means;
            title "Summary of All Numeric Variables";
         run;
      %end;
   %else %if %upcase(&request)=PRINTIT %then
      %do;
         proc print;
            title "Listing of Data";
         run;
      %end;
   %else %put Incorrect report type. Please try again.;
   title;
%mend reportit;

%reportit(stat)
%reportit(printit)

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.

Previous Page | Next Page | Top of Page