General form, %IF-%THEN
and %ELSE statements:
%IF expression %THEN text;
<%ELSE text;>
expression
can be any valid macro
expression that resolves to an integer.
text
can be specified as
|
%IF-%THEN...
|
IF-THEN...
|
---|---|
is used only in a macro
program.
|
is used only in a DATA
step program.
|
executes during macro
execution.
|
executes during DATA
step execution.
|
uses macro variables
in logical expressions and cannot refer to DATA step variables in
logical expressions.
|
uses DATA step variables
in logical expressions.
|
determines what text
should be copied to the input stack.
|
determines what DATA
step statement(s) should be executed. When inside a macro definition,
it is copied to the input stack as text.
|
General form, %DO-%END
with %IF-%THEN and %ELSE statements:
%IF expression %THEN
%DO;
text
and/or macro language statements
%END;
%ELSE %DO;
text
and/or macro language statements
%END;
text and/or macro language statements
is either constant
text, a text expression, and/or a macro statement.
|
status
determines which variables
are included in the new data set. The value of the data set variable Location
determines
the value of the new data set variable Totalfee
.
%macro choice(status); data fees; set sasuser.all; %if &status=PAID %then %do; where paid='Y'; keep student_name course_code begin_date totalfee; %end; %else %do; where paid='N'; keep student_name course_code begin_date totalfee latechg; latechg=fee*.10; %end; /* add local surcharge */ if location='Boston' then totalfee=fee*1.06; else if location='Seattle' then totalfee=fee*1.025; else if location='Dallas' then totalfee=fee*1.05; run; %mend choice;
options mprint mlogic; %choice(PAID)
160 %choice(PAID) MLOGIC(CHOICE): Beginning execution. MLOGIC(CHOICE): Parameter STATUS has value PAID MPRINT(CHOICE): data fees; MPRINT(CHOICE): set sasuser.all; MLOGIC(CHOICE): %IF condition &status=PAID is TRUE MPRINT(CHOICE): where paid='Y'; MPRINT(CHOICE): keep student_name course_code begin_date totalfee; MPRINT(CHOICE): if location='Boston' then totalfee=fee*1.06; MPRINT(CHOICE): else if location='Seattle' then totalfee=fee*1.025; MPRINT(CHOICE): else if location='Dallas' then totalfee=fee*1.05; MPRINT(CHOICE): run; |
options mprint mlogic; %choice(OWED)
161 %choice(OWED) MLOGIC(CHOICE): Beginning execution. MLOGIC(CHOICE): Parameter STATUS has value OWED MPRINT(CHOICE): data fees; MPRINT(CHOICE): set sasuser.all; MLOGIC(CHOICE): %IF condition &status=PAID is FALSE MPRINT(CHOICE): where paid='N'; MPRINT(CHOICE): keep student_name course_code begin_date totalfee latechg; MPRINT(CHOICE): latechg=fee*.10; MPRINT(CHOICE): if location='Boston' then totalfee=fee*1.06; MPRINT(CHOICE): else if location='Seattle' then totalfee=fee*1.025; MPRINT(CHOICE): else if location='Dallas' then totalfee=fee*1.05; MPRINT(CHOICE): run; |
%macro printit;
%if &syslast ne _NULL_ then %do;
proc print data=_last_(obs=5);
title "Last Created Data Set Is &syslast";
run;
%end;
%mend;
10 %macro printit; 11 %if &syslast ne _NULL_ then %do; ERROR: Macro keyword DO appears as text. A semicolon or other delimiter may be missing. ERROR: Expected %THEN statement not found. A dummy macro will be compiled. 12 proc print data=_last_(obs=5); 13 title "Last Created Data Set Is &syslast"; 14 run; 15 %end; ERROR: There is no matching %DO statement for the %END. This statement will be ignored. 16 %mend; |
%macro-name
,
it executes compiled macro language statements such as %IF-%THEN.
The values of macro variables that are used within the %IF logical
expression are resolved during macro execution. The %IF logical expression
is automatically evaluated.
%macro attend(crs,start=01jan2001,stop=31dec2001); %let start=%upcase(&start); %let stop=%upcase(&stop); proc freq data=sasuser.all; where begin_date between "&start"d and "&stop"d; table location / nocum; title "Enrollment from &start to &stop"; %if &crs= %then %do; title2 "for all Courses"; %end; %else %do; title2 "for Course &crs only"; where also course_code="&crs"; %end; run; %mend;
%if &crs=
is true when crs
has
a value of null.
%attend(C003)
18 %attend(C003) MPRINT(ATTEND): proc freq data=sasuser.all; MPRINT(ATTEND): where begin_date between "01JAN2001"d and "31DEC2001"d; MPRINT(ATTEND): table location / nocum; MPRINT(ATTEND): title "Enrollment from 01JAN2001 to 31DEC2001"; MPRINT(ATTEND): title2 "for Course C003 only"; MPRINT(ATTEND): where also course_code="C003"; NOTE: WHERE clause has been augmented. MPRINT(ATTEND): run; NOTE: Writing HTML Body file: sashtml.htm NOTE: There were 2 observations read from the data set SASUSER.SCHEDULE. WHERE (Course_Code='C003') and (Begin_Date>='01JAN2001'D and Begin_Date<='31DEC2001'D); NOTE: There were 207 observations read from the data set SASUSER.STUDENTS. NOTE: There were 434 observations read from the data set SASUSER.REGISTER. NOTE: There were 1 observations read from the data set SASUSER.COURSES. WHERE Course_Code='C003'; NOTE: There were 50 observations read from the data set SASUSER.ALL. WHERE (begin_date>='01JAN2001'D and begin_date<='31DEC2001'D) and (course_code='C003'), |
%attend(start=01jul2001)
19 %attend(start=01jul2001)
MPRINT(ATTEND): options mprint;
MPRINT(ATTEND): proc freq data=sasuser.all;
MPRINT(ATTEND): where begin_date between "01JUL2001"d and "31DEC2001"d;
MPRINT(ATTEND): table location / nocum;
MPRINT(ATTEND): title "Enrollment from 01JUL2001 to 31DEC2001";
MPRINT(ATTEND): title2 "for all Courses";
MPRINT(ATTEND): run;
NOTE: There were 6 observations read from the data set SASUSER.SCHEDULE.
WHERE Course_Code in ('C001', 'C002', 'C003', 'C004', 'C005', 'C006') and
(Begin_Date>='01JUL2001'D and Begin_Date<='31DEC2001'D);
NOTE: There were 207 observations read from the data set SASUSER.STUDENTS.
NOTE: There were 434 observations read from the data set SASUSER.REGISTER.
NOTE: There were 6 observations read from the data set SASUSER.COURSES.
NOTE: There were 162 observations read from the data set SASUSER.ALL.
WHERE (begin_date>='01JUL2001'D and begin_date<='31DEC2001'D);
|
cols
parameter
is specified in the call. It creates a two-way table if the rows
parameter
is also specified. %macro counts (cols=_all_,rows=,dsn=&syslast);
title "Frequency Counts for %upcase(&dsn) data set";
proc freq data=&dsn;
tables
%if &rows ne %then &rows *;
&cols;
run;
%mend counts;
cols
and rows
: %counts(dsn=sasuser.all, cols=paid, rows=course_number)
28 %counts(dsn=sasuser.all, cols=paid, rows=course_number) MPRINT(COUNTS): title "Frequency Counts for SASUSER.ALL data set"; MPRINT(COUNTS): proc freq data=sasuser.all; MPRINT(COUNTS): tables course_number * paid; MPRINT(COUNTS): run; NOTE: There were 18 observations read from the data set SASUSER.SCHEDULE. WHERE Course_Code in ('C001', 'C002', 'C003', 'C004', 'C005', 'C006'), NOTE: There were 207 observations read from the data set SASUSER.STUDENTS. NOTE: There were 434 observations read from the data set SASUSER.REGISTER. NOTE: There were 6 observations read from the data set SASUSER.COURSES. NOTE: There were 434 observations read from the data set SASUSER.ALL. NOTE: PROCEDURE FREQ used (Total process time): |
cols
but
does not specify rows
: %counts(dsn=sasuser.all, cols=paid)
29 %counts(dsn=sasuser.all, cols=paid)
MPRINT(COUNTS): title "Frequency Counts for SASUSER.ALL data set";
MPRINT(COUNTS): proc freq data=sasuser.all;
MPRINT(COUNTS): tables paid;
MPRINT(COUNTS): run;
NOTE: There were 18 observations read from the data set SASUSER.SCHEDULE.
WHERE Course_Code in ('C001', 'C002', 'C003', 'C004', 'C005', 'C006'),
NOTE: There were 207 observations read from the data set SASUSER.STUDENTS.
NOTE: There were 434 observations read from the data set SASUSER.REGISTER.
NOTE: There were 6 observations read from the data set SASUSER.COURSES.
NOTE: There were 434 observations read from the data set SASUSER.ALL.
NOTE: PROCEDURE FREQ used (Total process time):
|
_null_
is specified in lowercase
but is always stored in SAS in uppercase: %macro prtlast;
%if &syslast=_null_ %then %do;
%put No data sets created yet.;
%end;
%else %do;
proc print;
title "Last Created Data Set is &syslast";
run;
%end;
%mend;
options mprint mlogic symbolgen;
%prtlast
29 %prtlast MLOGIC(PRTLAST): Beginning execution. SYMBOLGEN: Macro variable SYSLAST resolves to _NULL_ MLOGIC(PRTLAST): %IF condition &syslast = _null_ is FALSE NOTE: The SAS System stopped processing this step because of errors. NOTE: PROCEDURE PRINT used: real time 1:32.58 cpu time 0.05 seconds MPRINT(PRTLAST): proc print; ERROR: There is not a default input data set (_LAST_ is _NULL_). SYMBOLGEN: Macro variable SYSLAST resolves to _NULL_ MPRINT(PRTLAST): title "Last Created Data Set is _NULL_"; MPRINT(PRTLAST): run; NOTE: The SAS System stopped processing this step because of errors. NOTE: PROCEDURE PRINT used: real time 0.01 seconds cpu time 0.01 seconds MLOGIC(PRTLAST): Ending execution. |