Processing Statements Iteratively

Overview

Many macro applications require iterative processing. With the iterative %DO statement that you can repeatedly
  • execute macro programming code
  • generate SAS code.
General form, iterative %DO statement with %END statement:
%DO index-variable=start %TO stop <%BY increment>;
text
%END;
Here is an explanation of the syntax:
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
  • 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.
%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.

Example

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 Sasuser.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, as follows:
data _null_;
   set sasuser.schedule end=no_more; 
   call symput('teach'||left(_n_),(trim(teacher))); 
   if no_more then call symput('count',_n_); 
run; 

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

%putloop
Tip
It is a good idea to specifically declare the index variable of a macro loop as a local variable to avoid the possibility of 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.
Table 11.20 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

The following macro generates a series of statements within a DATA step. On each iteration, the macro writes a message to the SAS log that puts the current value of the index variable into HEX6. format.
%macro hex(start=1,stop=10,incr=1);
   %local i;
   data _null_;
      %do i=&start %to &stop %by &incr;
         value=&i;
         put "Hexadecimal form of &i is " value hex6.;
      %end;
   run;
%mend hex;
Note: The HEX6. format converts a number to hexadecimal format.
Suppose you submit the following call:
options mprint mlogic;
%hex(start=20,stop=30,incr=2)
Some of the messages that are written to the SAS log when Hex executes are shown below. Notice that according to the MLOGIC messages, the loop stops processing when the value of the index variable is 32 (which is beyond the value that is specified for Stop).
Table 11.21 SAS Log
MLOGIC(HEX): %DO loop index variable I is now 30; loop will
               iterate again.
MPRINT(HEX): value=30;
MPRINT(HEX): put "Hexadecimal form of 30 is " value hex6.;
MLOGIC(HEX): %DO loop index variable I is now 32; loop will
               not iterate again.
MPRINT(HEX): run;

Hexadecimal form of 20 is 000014
Hexadecimal form of 22 is 000016
Hexadecimal form of 24 is 000018
Hexadecimal form of 26 is 00001A
Hexadecimal form of 28 is 00001C
Hexadecimal form of 30 is 00001E
NOTE: DATA statement used:
      real time           0.06 seconds
      cpu time            0.06 seconds

MLOGIC(HEX): Ending execution.

Generating Complete Steps

You can use the iterative %DO statement to build macro loops that create complete SAS steps.

Example

Suppose course offerings for several years are stored in a series of external files that are named by year, such as Raw1999.dat and Raw2000.dat. All the files have the same record layout. Suppose you want to read each file into a separate SAS data set.
The following macro uses a %DO statement to create a loop that creates a data set from each of the specified external files:
%macro readraw(first=1999,last=2005);
   %local year;
   %do year=&first %to &last;
      data year&year;
         infile "raw&year..dat";
         input course_code $4.
         location $15.
         begin_date date9.
         teacher $25.;
      run;

      proc print data=year&year;
         title "Scheduled classes for &year";
         format begin_date date9.;
      run;
   %end;
%mend readraw;
Suppose you submit the following call to the Readraw macro:
%readraw(first=2000,last=2002)
The macro creates three data sets named Year2000, Year2001, and Year2002. Remember that in order for this program to run properly, the raw data files must be named appropriately, and they must be stored in the location that the program specifies. The generated DATA step is shown in the log.
Table 11.22 SAS Log
336  %readraw(first=2000,last=2002)
MLOGIC(READRAW):  Beginning execution.
MLOGIC(READRAW):  Parameter FIRST has value 2000
MLOGIC(READRAW):  Parameter LAST has value 2002
MLOGIC(READRAW):  %LOCAL  YEAR
MLOGIC(READRAW):  %DO loop beginning; index variable YEAR; start value is 2000; 
      stop value is 2002; by value is 1.
MPRINT(READRAW):   data year2000;
MPRINT(READRAW):   infile "raw2000.dat";
MPRINT(READRAW):   input course_code $4. location $15. begin_date date9. 
                          teacher $25.;
MPRINT(READRAW):   run;

..................Content has been hidden....................

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