Академический Документы
Профессиональный Документы
Культура Документы
MACRO DO(UMENTATION
DOCUMENTED IN:
THE SAS USER'S GUIDE: BASICS,
1982 EDITION, PAGES 431-475.
TECHNICAL REPORT P-127:
"ENHANCEMENTS TO THE SAS MACRO FACILITY"
I
I
SAS
I macro
I
I
I--
BASE SAS
--------~
I I DATA
I I step
I I compiler
I mainstream
I PROCe.dures
I
I 1----
1---------
1---------------------I
I -----------------I
1 graphics
1__ 1 PROCedures
ISAS/GRAPH
I
1 I
1
I -----------------I -----------------1__ 1 PROCedures
I ETS
I SAS/ETS
I
I SAS/FSP
I
I
1 ------------------
I __ IOR
I PROCedures
ISAS/OR
I
\
357
APPLICATIONS OF MACRO
I
IMACRO LANGUAGE
1----I
I
I
I
I
I
I
I
I
I S I
I A I
w
I S I
ISAS
I
I
I
D I~-------~ISUPERVISOR
I<--------~I D I
A I
I
I
I A I
T I
IT I
I
I
I A I
t
t
I A I
I
I
1--1
I-I
I
I
I
I
I
I
I
I IFORMAT LIBRARY I IPROCEDURE
IDATA STEP I I
I ILIBRARIES
ICOMPILER
1 1
1 I
I
IBASE
I
I
I
R
A
._-
--.
1-------------1
lOR
I
1-------------1
IGRAPH
I
1-------------1
IFSP
lETS. . .
.1-------------1
358
In most cases, the major uses of the macro language are to:
conditionally execute PROC or DATA steps
repeatedly execute a group of statements
transfer information across SAS steps
package SAS tasks into systems.
2. Macro statements
start with a percent sign (%)
and a macro keyword and end
with a semicolon (;)
3. Model text
usually SAS statements or parts
of SAS statements
359
.,
.".
Macro variables
Macro statements
manipulate variables
are used for assignment and substitution
are used for branching and conditional execution.
360
SAS CODE
1
1
---+
SUPERVISOR
1 MACRO
I----~I
1
-----1
t
FACILITY
1----1
1
. . -------1- + - - - - -
1 SAS CODE
1----COMPILE
-:----+---~
EXECUTE
!'
~.'
&
361
,
, MACRO VARIABLES
, ,
'(symbolic variables),
,,
,,,
_t _ _ __
USER-DEFINED
__t __
SAS DEFINED
AUTOMATIC
362
Automatic variables:
are created when SAS is invoked
retain their values for the duration of the SAS job
reflect information about the SAS job.
Name
SYSDA Y
SYSENV
*
*
SYSPARM
SYSRC
SYSSCP
,~
SYSTIME
~,
SYSVER
363
I
I
the reference
is resolved
I
I
the reference
is NOT resolved
%lET SYNTAX
1LET - used to define macro variables and assign
values to them.
SYNTAX:
1.
2.
364
XLET EXAMPLE
Using these statements in a SAS job:
%LET MONTH=MAY;
TITLE "&MONTH";
produces:
TITLE "MAY";
MAY
THE BIRD OF PARADISE
%GLOBAL STATE;
%LOCAL
list of variables;
%MACRO SOLOCALj
%LOCAL I j
%MEND SOLOCALj
365
%MACRO TEST(IN);
%GLOBAL IN;
%MEND TEST;
%MACRO DOlT;
TITLE "THE VARIABLE &IN IS GLOBAL";
%MEND DOlT;
However, calling test
%TEST(INVALUE)
yields
1501:
GLOBAL/LOCAL FEATURES
Global macro variables:
are defined any time during a SAS job
are available globally once defined
retain their values across DATA and PROC steps
have values that may be changed any time during the SAS
are a method of communication among macros
include automatic macro variables.
Local macro variables:
can only be defined by or in a macro
are avaUable only inside the macro that defines them
can retain their values across DATA and PROC steps
if the DATA or PROC steps are all contained within
the macro
are primarily used as index or counting variables.
366
%MACRO macroname(parameters)/options;
macrotext
%MEND macroname;
%MACRO .
The macro name must be a valid SAS name.
1 to 8 characters, letters, or _ (CMS: 7 character
maximum)
Macro parameters are macro variables for
which values are supplied by the macro call
or in the macro itself .
The STMT option is the only option currently
available with the %MACRO statement. This
option changes the form of the macro call .
%MEND is used to end the macro definition.
macroname is not required BUT STRONGLY
RECOMMENDED.
MACROTEXT is generally SAS statements or
parts of SAS statements along with macro
programming statements.
PARAMETER RULES
Parameter definition rules:
367
PARAMETER DEFINITIONS
(SASname.SASname.SASname .. )
keyword parameters
(SASname.SASname.SASname=string ... )
PARAMETER DEFINITIONS
Example of positional parameters
%MACRO CLINT(GOOD.BAD.UGLY);
DATA &GOOD;
SET &BAD;
BY &UGLY;
IF FIRST.MOVIE;
%MEND CLINT;
%MACRO CLINT(GOOD=BEST,BAD=WESTERN.UGLY=MOVIE);
DATA &GOOD;
SET &BAD;
BY &UGLY;
IF FIRST.MOVIE;
%MEND CLINT;
368
ABORT
ACT
BY
CLOSE
COPY
DO
DEL
EVAL
ELSE
EDIT
GO
INDEX
INFILE
INC
LET
LIST
MACRO
NRQUOTE
OPEN
PUT
QUOTE
RETURN
SUBSTR
STOP
STR
THEN
UNQUOTE
UPCASE
WHILE
ABEND
ACTIVATE
CLEAR
CMS
DISPLAY
DEACT
DELETE
FILE
END
GLOBAL
GOTO
INPUT
IF
INCLUDE
LOCAL
LISTM
MEND
NRSTR
ON
PAUSE
RESOLVE
RUN
SCAN
SAVE
TSO
TO
UNSTR
UNTIL
369
1
MACRO _VARS
1
STATE RANK70 RANK79 POP79 DENSITY MIGRATE; 1
1
1
1
MACRO _PROCESS
DATA;
INFILE STATESIN;
INPUT _VARS
1
1
1
1
_PROCESS
-----------------------------1
%MACRO VARS;
STATE RANK70 RANK79 POP79 DENSITY MIGRATE;
%MEND VARS;
%MACRO PROCESS;
DATA;
INFILE STATESIN;
INPUT %VARS
%MEND PROCESS;
%PROCESS
%INDEX(argumentl,argument2)
%LENGTH(argument)
t.SCAN(argument,n,delimiter)
t.SUBSTR(argument,start,stop)
substrings a character
string from an argument beginning
with start for how many.
%UPCASE(argument).
translates lowercase
characters to uppercase.
I
$
)
370
XSCAN-SPECIAL DELIMITERS
%SCAN uses these default delimiters:
*)
~ -
XEVAl FUNCTION
Use the %EVAL function to perform computations ..
The SAS macro facility is a string (character)
handling language.
It does not compute.
The %EVAL function is used to allow the macro
language to do integer arithmetic.
Macro issues an error message if you attempt .to
perform a calculation on non-integer values.
If you do %LET X=%EVAL(1I3), the result is
truncated.
%EVAL(expression or argument)
The statements:
The statements:
%LET X=2j
%LET Y=%EVAL(&X+l)j
%LET X=2j
%LET Y=&X+l j
Assign:
&X a value of
&Y a value of
Assign:
2
3.
&X a value of
&Y a value of
\
371
2
2+1.
%STRO
%NRSTRO
%QUOTEO
%UNQUOTE()
7.STR FUNCTI ON
%STR hides the meaning of: C *
) / = from the macro facility.
+ - &
semicolons
A semicolon
Cp
START=%UNQUOTE(%QUOTE(%'&SYSDATE%')D);
372
%CMS
%'~COMMENT
%DO %END
iterative %DO %END
%DO %UNTIL %END
%DO %WHILE %END
%GOTO label;/%label:
%GLOBAL
%IF - %THEN %ELSE
%INPUT
%LET
%LOCAL
%MACRO
%MEND
%PUT
%TSO
91
%LET L2=%STR( );
101
%INC 8;
FIRST LINE
TlIIRD LINE
373
1:PUT SYNTAX
%PUT is used to write a line to the SAS log:
%PUT string;
STRING is a list of characters (including macrovariables)
which does not contain a semicolon
It is very useful for showing values of macro variables
as a debugging aid.
NOTE:
1:INPUT SYNTAX
374
TALKING MACRO
%MACRO PHREO;
%PUT HI, MY NAME IS PHREO. I AM A TALKING MACRO.;
%PUT WHAT IS YOUR NAME?;
%INPUT NAMEj
%PUT WELL, &NAME THIS HAS BEEN FUN BUT I HAVE TO GO.j
%MENO PHREOj
375
376
%MACRO SUBSETS(HOWMANY,ROOTNAME=MONTH);
%LET SETS = &ROOTNAME.l;
%DO C = 2 %TO &HOWMANY;
%LET SETS=&SETS &ROOTNAME&C;
%END;
DATA &SETS;
SET YEARLY;
%LET ELSE;
%DO I = 1 %TO &HOWMANY;
&ELSE IF MONTH = &1 THEN OUTPUT &ROOTNAME&I;
%LET ELSE=ELSE;
%END;
RUN;
%MEND SUBSETS;
The macro call,
SYMPUT function
used in a SAS DATA step
to create a macro variable
whose value is that of a
SAS DATA step variable.
377
1
1
PROGRAM
1
1 DATA
1
1 VECTOR
1
1
SYMBOL TABLE
1 write
------------~
1 SYMPUTI
1
1 read
1
1
~------------
SYNGETI
1
1
1
1_ _ _ __
SYMPUT SYNTAX
CALL SYMPUT(argumentl,argument2);
378
'"
-.J
<0
DATA DATES;
RETAIN START STOP;
INFILE CARDS EOF=EOF;
INPUT DATE M~1DDYY6. SALES;
IF _N_=l THEN DO;
START=DATE;
STOP=DATE;
END;
IF START> DATE THEN START=DATE;
IF STOP < DATE THEN STOP=DATE;
RETURN;
EOF: CALL Sn!PUT('START' ,PUT(START,DATE7.));
CALL SY11PUT(' STOP' ,PUT(STOP ,DATE7.));
STOP;
CARDS;
010183 1000.00
011583 500.00
020183 1000.00
022383 10000
041083 o
040283 100
053083 255.00
RUN;
PROC PRINT;
TITLE "SALES DATA FROH &START TO &STOP";
RUN;
3
4
5
6
7
8
9
10
11
12
13
14
15
DATA DATES;
RETAIN START STOP;
INFILE CARDS EOF=EOF;
INPUT @6 DATE MMDDYY6. SALES;
IF _N_=l THEN DO;
START=DATE;
STOP=DATE;
END;
IF START> DATE THEN START=DATE;
IF STOP < DATE THEN STOP=DATE;
RETURN;
EOF: CALL Sn!PUT('START',PUT(START,DATE7.));
CALL SYMPUT('STOP' ,PUT(STOP,DATE7.));
STOP;
CARDS;
RUN;
PROC PRINT;
TITLE . "SALES DATA FROM &START TO &STOP";
VH/CHS
START
STOP
DATE
SALES
1
2
3
4
5
8401
8415
8432
8454
8500
8500
8550
8401
8401
8401
8401
8401
8401
8401
8401
8415
8432
8454
8500
8492
8550
1000
500
1000
10000
0
100
255
6
7
NUMBER-OBSERVATIONS IN DATASET
In the example below a statement style macro is used to test
an empty SAS data set and return a message or the number of
observations in the data set.
THE IMPLMAC AND DQUOTE OPTION NUST BE TURNED ON TO
USE THE MACRO IN STATEMENT STYLE FORM.
To use the macro enter:
NOBS
datasetname
380
"i
SYMGET EXAMPLES
SYMGET returns the value of a macro variable
identified by the argument.
SYNGET(argument);
x=
SYMGET(argument)j
2.
MACRO EFFECT
MACRO jNmlACRO
MACROGENjNOMACROGEN
MCOMPILEjNOHCOMPILE
MERROR/NOMERROR
MLEAVE=
MLOGIC/NOMLOGIC
trace execution
MSI2E=
k of macro memory
MSYHSI2E=
HWORK=
SYMBOLGEN/NOSYMBOLGEN
SERROR/NOSERROR
SYSPARM='
pass text to
&SYSPAR~1
381
MERROR/NOMERROR
MERROR/NOMERROR causes a warning message to be issued for
a name that looks like a macro carr when no macro of
t~at name has been defined. It is a good debugging
aid for locating misspelled macro keywords and
names.
SERROR/NOSERROR
;ERROR/NOSERROR ~auses a warning message to be issued
or. names that look like macro variable references for
which ~o symbolic variable can be found. This
debugging tool helps find misspelled symbolic names
?r ref.erences to a symbolic variable before the variable
IS defined.
NOMACROGEN/MACROGEN
NOMACROGEN was deSigned for old macros to show the
text substitution that was done when an old
macro name was used. In SAS82, MACROGEN causes the
constant text (the SAS statements before symbolic
substitution) that a macro prouuces to be displayed
or printed. It does not display the actual macro
definition statements or. the macro programming
statements. The MPRINT option often provides a
better way to see the res u Its of macro execution.
NOMLOG IC/MLOG IC
MLOGIC directs the macro processor to trace its
execution. All of the intermediate steps that
macro execution performs are displayed on the log.
MLOGIC should only be used as an in-depth debugging
tool, since the resulting output is bulky and hard
to read.
MPRINT/NOMPRINT
MPRINT causes the fully resolved result of a macro
execution to be displayed on the SAS log. MPRINT
allows you to see what the SAS compiler sees
without any extraneous information. The MPRINT
option overrides MACROGEN, SYMBOLGEN, and MLOGIC for
macro-produced text.
NOSYMBOLGEN/SYMBOLGEN
SYMBOLGEN causes the results of symbolic
resolution to be written to the SAS log. SYMBOLGEN
should be used primarily ali a debugging tool.
382
DEBUGGING OPTIONS
These SAS system options help in debugging macros:
1
Option
Effect
--------- ----------------------1
1
MPRINT
SYMBOLGEN
1
1
1
1
SOURCE2
--------------------1
NOMPRINT
MLOGIC
~lACROGEN
DEBUG MACRO
~lACRO
DEBUG(x) / STMT;
%,., Macro from Septe'Dber, 1983
%,.,
No
On
All ?
DEFAULT - NO SHOW;
MPRINT SYMBOLGEN
~ILOGIC ETC
HELP
%else
%if %upcase(%substr(&x,l,l=N %then %do;
OPTIONS NmlLOGIC NmlPRINT NOSHmOLGEN
NmlACROGEN NOSOURCE2 NOSOURCE NONOTES NODATE;
%end;
~else
'kdo;
clear; run;
~"put ~~nrstr(
~~put ~'nrs tr (
~~put
%nrstr(
~~put
;{nrstr(
j~Ptlt ~~nrstr(
~~put ;~nrstr(
~:,:,put '~nrstr(
:~put '~nrstr(
~~put
%nrstr(
%nrstr(
~~put
~oend
'lEND DEBUG;
383
) ;
) ;
) ;
);
)
)
)
)
)
;
;
;
;
;