Iterative Processing

The %DO Statement

A Brief Overview

Many macro applications require iterative processing. With the iterative %DO statement you can do the following repeatedly:
  • execute macro programming code
  • generate SAS code
The iterative %DO and %END statements are valid only inside a macro definition. The index variable is created in the local symbol table if it does not appear in any existing symbol table.
The iterative %DO statement evaluates the value of the index variable at the beginning of each loop iteration. The loop stops processing when the index variable has a value that is outside the range of the start and stop values.

%DO Syntax

Syntax, iterative %DO statement with %END statement:
%DO index-variable=start %TO stop <%BY increment>;
text
%END;
index-variable
is either the name of a macro variable or a text expression that generates a macro variable name.
start and stop
specify either integers or macro expressions that generate integers to control how many times the portion of the macro between the iterative %DO and %END statements is processed.
increment
specifies either 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.
text
can be any of these elements:
  • constant text, possibly including SAS data set names, SAS variable names, or SAS statements
  • macro variables, macro functions, or macro program statements
  • any combination of the above

Example: Using the %DO Statement

You can use a macro loop to create and display a series of macro variables.
This example creates a series of macro variables named Teach1-Teachn, one for each observation in the Certadv.Schedule data set, and assigns teacher names to them as values. Then the Putloop macro uses a %DO statement and a %END statement to create a loop that writes these macro variables and their values to the SAS log.
proc sql noprint;
   select teacher 
      into :teach1-
      from certadv.schedule;
run;

%macro putloop;
   %local i;
   %do i=1 %to &sqlobs
      %put TEACH&i is &teach&i
   %end;
%mend;

%putloop
Tip
SQLOBS macro variable stores the number of rows in the previous SQL query.
Tip
It is a good idea to specifically declare the index variable of a macro loop as a local variable to avoid accidentally changing the value of a macro variable that has the same name in other symbol tables.
When the Putloop macro is executed, no code is sent to the compiler because the %PUT statements are executed by the macro processor.
The following messages are written to the SAS log.
Log 9.8 SAS Log
TEACH1 is Hallis, Dr. George
TEACH2 is Wickam, Dr. Alice
TEACH3 is Forest, Mr. Peter
TEACH4 is Tally, Ms. Julia
TEACH5 is Hallis, Dr. George
TEACH6 is Berthan, Ms. Judy
TEACH7 is Hallis, Dr. George
TEACH8 is Wickam, Dr. Alice
TEACH9 is Forest, Mr. Peter
TEACH10 is Tally, Ms. Julia
TEACH11 is Tally, Ms. Julia
TEACH12 is Berthan, Ms. Judy
TEACH13 is Hallis, Dr. George
TEACH14 is Wickam, Dr. Alice
TEACH15 is Forest, Mr. Peter
TEACH16 is Tally, Ms. Julia
TEACH17 is Hallis, Dr. George
TEACH18 is Berthan, Ms. Judy
You can also use a macro loop to generate statements that can be placed inside a SAS program step.

Example: Generating Complete Steps

You can use the iterative %DO statement to build macro loops that create complete SAS steps. Suppose you want to generate a roster for each of the 18 classes that you have. You can use a %DO statement to create a loop that creates a roster for each class.
%macro rosters;
   %do class=1 %to 18;
      title "Roster for Class #&class";
      proc print data=certadv.all;
         where Course_Number=&class
      run;
   %end;
%mend;

%rosters
The macro prints 18 rosters for each course titles.
Last updated: October 16, 2019
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset