General form, 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
|
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
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.
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 |
%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;
options mprint mlogic; %hex(start=20,stop=30,incr=2)
Stop
).
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. |
%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;
Readraw
macro:
%readraw(first=2000,last=2002)
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; |