%DO, Iterative Statement

Executes a section of a macro repetitively based on the value of an index variable.
Type: Macro statement
Restriction: Allowed in macro definitions only
See: %END Statement

Syntax

%DO macro-variable=start %TO stop <%BY increment> ;
text and macro language statements
%END;

Required Arguments

macro-variable
names a macro variable or a text expression that generates a macro variable name. Its value functions as an index that determines the number of times the %DO loop iterates. If the macro variable specified as the index does not exist, the macro processor creates it in the local symbol table.
You can change the value of the index variable during processing. For example, using conditional processing to set the value of the index variable beyond the stop value when a certain condition is met ends processing of the loop.
startstop
specify integers or macro expressions that generate integers to control the number of times the portion of the macro between the iterative %DO and %END statements is processed.
The first time the %DO group iterates, macro-variable is equal to start. As processing continues, the value of macro-variable changes by the value of increment until the value of macro-variable is outside the range of integers included by start and stop.
increment
specifies an integer (other than 0) or a macro expression that generates an integer to be added to the value of the index variable in each iteration of the loop. By default, increment is 1. Increment is evaluated before the first iteration of the loop. Therefore, you cannot change it as the loop iterates.

Example: Generating a Series of DATA Steps

This example illustrates using an iterative %DO group in a macro definition.
%macro create(howmany);
   %do i=1 %to &howmany;
      data month&i;
         infile in&i;
         input product cost date;
      run;
   %end;
%mend create;
%create(3)
When you execute the macro CREATE, it generates these statements:
DATA MONTH1;
   INFILE IN1;
   INPUT PRODUCT COST DATE;
RUN;
DATA MONTH2;
   INFILE IN2;
   INPUT PRODUCT COST DATE;
RUN;
DATA MONTH3;
   INFILE IN3;
   INPUT PRODUCT COST DATE;
RUN;