SAS MACRO TUTORIAL
S. Morton and Phil Winters, SAS Institute
MACRO DO(UMENTATION
SAS users have a new facility available in
SAS82:
The SAS macro language.
DOCUMENTED IN:
THE SAS USER'S GUIDE: BASICS,
1982 EDITION, PAGES 431-475.
TECHNICAL REPORT P-127:
"ENHANCEMENTS TO THE SAS MACRO FACILITY"
MACRO FACILITY AND OTHER IPPS
The family of Institute Program Products (lPPs):
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 -----------------I -----------------I I FSP
1__ 1 PROCedures
I I
I SAS/FSP
I
I
1 ------------------
I __ IOR
I PROCedures
ISAS/OR
I
The macro facility is part of the base SAS product.
\
\
357
APPLICATIONS OF MACRO
The Macro language is intended to allow a user to:
extract system information known
to the SAS system
perform conditional execution of
SAS DATA and PROC steps
generate data-dependent SAS
statements (SAS code) and SAS steps
generate repetitive SAS code
develop user-friendly systems
develop line-prompted and full-screen
menu systems
communicate information between
SAS steps
develop_production SAS systems.
FUNCTIONAL VIEW OF IPPS
How the SAS macro language is related to
the Institute Program Products:
ISAS SOURCE STATEMENTS I
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
MAJOR USES OF MACRO
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.
WHAT IS THE MACRO LANGUAGE?
The SAS macro language is
a programming language for
string manipulation .
Strings are sequences of characters.
Everything is a character string
to the macro facility .
The strings will typically be SAS code
or pieces of SAS code.
WHAT IS THE MACRO LANGUAGE?
The macro language has
three basic constructs:
1. Symbolic macro variables
SAS naming rules apply
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
.,
.".
WHAT IS THE MACRO LANGUAGE
Macro variables
store string values
manipulate string values
act as parameters.
Macro statements
manipulate variables
are used for assignment and substitution
are used for branching and conditional execution.
NOTE: macro variables are not SAS DATA step variables.
SIMPLE VIEW OF MACRO WORKINGS
When SAS macro statements are used in a SAS
program, they are first processed by the macro
.. preprocessor ."
1. The macro preprocessor resolves the macro
statements and macro variables and produces
standard SAS code.
2. This generated code becomes part of the code
that makes up your SAS program.
3. The resulting SAS program is then passed to
the SAS compiler for execution.
NOTE: The macro processor doesn't preprocess the whole
SAS job but it is more tightly coupled to SAS.
360
ADDITION OF THE MACRO FACILITY
When necessary the supervisor transfers control to
the macro facility.
SAS CODE
1
1
---+
SUPERVISOR
1 MACRO
I----~I
1
-----1
t
FACILITY
1----1
1
. . -------1- + - - - - -
1 SAS CODE
1----COMPILE
-:----+---~
EXECUTE
!'
~.'
The macro facility emits SAS code that is then
returned to the supervisor.
ROLE OF THE SAS WORDSCANNER
TWO tokens trigger the wordscanner
to notify the macro facility to look
at the INPUT STREAM:
&
If either of these tokens is followed by
a non-blank, the macro facility takes
control of the wordscanner.
361
MACRO VARIABLE RULES
Each macro variable has a NAME. SAS naming rules apply
(1 to 8 characters beginning with a letter or
underscore)
A macro variable is REFERENCED (called) anywhere
in a SAS job by placing an ampersand (&) immediately
before the macro variable name.
A macro variable reference tells SAS to replace
the reference with the value of the macro
variable .
An optional period (.) delimits a macro
variable reference.
NOTE: The length of a macro (symbolic) variable value range is
zero to the maximum defined for your installation.
Maximum length ranges from 1024 to 28672 characters.
(See your computing center staff for the maximum length
at your installation.)
TYPES OF MACRO VARIABLES
Macro variables are defined:
by the SAS supervisor
by the USER.
,
, MACRO VARIABLES
, ,
'(symbolic variables),
,,
,,,
_t _ _ __
USER-DEFINED
__t __
SAS DEFINED
AUTOMATIC
362
AUTOMATIC VARIABLES: TRAITS
Automatic variables:
are created when SAS is invoked
retain their values for the duration of the SAS job
reflect information about the SAS job.
Some automatic variables can be changed by the user.
AUTOMATIC VARIABLES: LIST
Value
Name
SYSBUFFR holds a string entered in response
to a %INPUT statement.
SYSDATE
date the SAS job executed.
SYSDA Y
day of the week the SAS job began
execution.
SYSDEVIC name of the current graphics device.
SYSDSN
name of the most recently created SAS
data set in the current SAS job.
SYSENV
mode of execution - FORE (as in foreground) or BACK (as in background).
SYSINDEX number of macros that have started
execution in the current SAS job.
*
*
SYSPARM
same as DATA step function SYSPARHO.
SYSRC
last return code set by %TSO %CHS and macros
SYSSCP
operating system acronym--OS, CMS, or
DOS.
,~
SYSTIME
time the SAS job began execution.
~,
SYSVER
version of SAS being used.
Automatic variables can be
referenced anywhere in a SAS job.
NOTE:
indicates a variable that cannot be changed.
363
AUTOMATIC VARIABLES: USING
DOUBLE QUOTES:
SAS code:
TITLE "THIS IS THE REPORT FOR &SYSDATE"j
I
I
the reference
is resolved
SAS compiler sees:
TITLE "THIS IS THE REPORT FOR IJAN84"j
SINGLE QUOTES:
SAS code:
TITLE 'THIS IS THE REPORT FOR &SYSDATE'j
I
I
the reference
is NOT resolved
SAS compiler sees:
TITLE 'THIS IS THE REPORT FOR &SYSDATE'j
%lET SYNTAX
1LET - used to define macro variables and assign
values to them.
SYNTAX:
1.
%LET macrovariablename = string
ex: %LET MONTH=MAYj
Th is defines a macro variable named MONTH.
It has the value MAY.
2.
%LET ¯ovariablename = string j
ex: %LET &MONTH=THE BIRD OF PARADISEj
This defines a macro variable named MAY.
It has the value THE BIRD OF PARADISE.
The difference between these two forms
is that a macro variable reference (&month)
will be resolved before the %LET statement
is executed.
364
XLET EXAMPLE
Using these statements in a SAS job:
%LET MONTH=MAY;
TITLE "&MONTH";
produces:
TITLE "MAY";
Adding these statements to the same SAS job:
%LET &MONTH=THE BIRD OF PARADISE;
TITLE2 "&MAY";
produces:
TITLE2 "THE BIRD OF PARADISE";
SAS uses the two titles for the top
of the printed pages:
MAY
THE BIRD OF PARADISE
XGLOBAL XLOCAL STATEMENTS
Two statements can be used to explicitly define
a macro variable's reference environment
%GLOBAL list of variables;
The %GLOBAL statement is used to make macro variables
available to all referencing environments of a job.
%GLOBAL can be used anywhere in a SAS job.
%GLOBAL STATE;
%LOCAL
list of variables;
The %LOCAL statement is used to specify macro variables
that are to be available to a single macro.
%LOCAL can be used only inside a macro definition.
%LOCAL is used to prevent resetting the value of a
global variable or to prevent contaminating an index'
variable with a value from another macro. Useful in
recursive calling of a macro.
%MACRO SOLOCALj
%LOCAL I j
%MEND SOLOCALj
365
7.GLOBAL 7.LOCAL STATEMENTS
A situation occasionally arises that requires
a macro variable that is by definition local
(that is, a macro parameter) to be globally
referenced (its value used by another macro).
A simple solution would seem to be:
%MACRO TEST(IN);
%GLOBAL IN;
%MEND TEST;
%MACRO DOlT;
TITLE "THE VARIABLE &IN IS GLOBAL";
%MEND DOlT;
However, calling test
%TEST(INVALUE)
yields
1501:
ATTEMPT TO %GLOBAL A NAME WHICH EXISTS IN A LOCAL ENVIRONMEN1
Macro parameters are local.
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 DEFINITION SYNTAX
The general form of a macro definition is:
%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:
The parameter list is enclosed in parentheses
Parameters are separated by commas.
Parameters may be positional or keyword.
When the two parameter types are mixed,
positional parameters must be defined before
keyword parameters.
367
PARAMETER DEFINITIONS
Positional parameters and keyword parameters
define macro variables that can be used
with the macro text.
SYNTAX of parameter list:
positional parameters
(SASname.SASname.SASname .. )
keyword parameters
"(SASname=string of characters.SASname=string ... )
mixed 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;
Example of keyword parameters
%MACRO CLINT(GOOD=BEST,BAD=WESTERN.UGLY=MOVIE);
DATA &GOOD;
SET &BAD;
BY &UGLY;
IF FIRST.MOVIE;
%MEND CLINT;
368
STATEMENT OPTION OF A MACRO
When the STMT option is specified, the macro call:
may NOT require a leading %
DOES require a semicolon
cannot have ( or ) for parameter specifications- no commas
REQUIRES OPTIONS IMPLMAC.
NOTE: the STMT option is documented in
TECHNICAL REPORT P-127.
RESERVED WORD TABLE CONTENTS
These are the reserved words currently in the list
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
These keywords MAY NOT be used as macro names.
369
CHANGING OLD MACROS
Changing SAS79 macros to new SAS82 macros,
If you have existing programs with old SAS
macros, you can improve efficiency by changing
to the new macro syntax.
SAS79 macro syntax
SAS82 macro syntax
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
STRING FUNCTIONS: LIST
The folloWing macro functions are currently available.
%INDEX(argumentl,argument2)
finds the first occurrence of
argument2 in the string argumentl
%LENGTH(argument)
finds the length of an
argument
t.SCAN(argument,n,delimiter)
scans for "word" in an argument
using a 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.
Special string handling function:
%EVAL(expression)
evaluates arithmetic and
logical expressions.
I
$
)
370
XSCAN-SPECIAL DELIMITERS
%SCAN uses these default delimiters:
blank . < ( + I & I $
*)
~ -
There are two special cases:
Blank delimiter only
%LET NAME = SAMUEL T. GOMPERS j
%LET FIRST = %SCAN(&NAME,l,)j
%LET LAST = %SCAN(&NAME,3,%STR( j
Comma delimiter only
%LET NAME = RONALD R. RAYGUN, JR.;
%LET TITLE = %SCAN(&NAME,2,%STR(,j
WRONG:
%LET'TITLE=%SCAN(&NAME,2,,)
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.
The %EVAL function has the form:
%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.
QUOTING FUNCTIONS: LIST
The following quoting functions are currently available:
%STRO
EXPRESS STRINGS CONTAINING
SPECIAL CHARACTERS
%NRSTRO
%QUOTEO
%UNQUOTE()
These functions allow you
use characters that have a special
meaning to the macro language without
invoking their special meaning.
7.STR FUNCTI ON
%STR hides the meaning of: C *
) / = from the macro facility.
+ - &
semicolons
%STR is a compile time function
of the wordscanner and should
be used with literal values.
A semicolon
Cp
ends a macro program statement.
%STR allows you to use semicolons as string characters.
%STR does not affect the percent sign (% lor the
ampersand C&) references.
DATETIME CONSTANTS AND MACRO
%UNQUOTE is also needed to make datetime constants
because of a wordscanner bug.
START=%UNQUOTE(%QUOTE(%'&SYSDATE%')D);
372
PROGRAM STATEMENTS: LIST
Many SAS DATA step statements also exist as macro
program statements but begin with a percent sign (%).
%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
7.PUT A BLANK LINE
%STR is the answer.
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.
%PUT Hello there gringo;
%LET WHO=Napoleon Flamingo ;
%PUT Hello there gringo &WHO
%PUT ignores leading and trailing blanks.
If you want to line up messages, use %STR.
NOTE:
%PUT strings are truncated
to current log file length.
1:INPUT SYNTAX
%INPUT is used to input a string from the terminal:
%INPUT variable_list;
Variable_list is a list of macro variables
separated by blanks.
%INPUT is documented in TECHNICAL REPORT P-127
%PUT and %INPUT can be used to make interactive systems
%PUT writes a line to the terminal.
%INPUT reads a lipe from the terminal.
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
7-PUT 7-INPUT MENU
%MACRO FSCALC/STMTj
% ------------------------------------j
% THIS MACRO LOOPS TO 00 CONDITIONAL j
% EXECUTION or DATA AND PROC
% STEPS UNTIL THE USER WANTS TO STOP.j
% ------------------------------------;
%LET DONE = OJ
%00 %WHILE(&DONE = 0);
CLEARjRUN;
%PUT PROC rSCALC: SPREAD SHEET APPLICATIONSj
%PUT --------------------------------------;
%PUT %STR( ) Please Select a demo:j
%PUT %STR(
) 0
Enter 0 to quitj
%PUT %STR(
) 1
New Product Analysisj
%PUT %STR(
) 2
Mortgage Schedulej
%INPUT CMDINj
%Ir &CMOIN = 1 %THEN %DOj
PROC,FSCALC SCREEN=SAVE.DEMOlj
RUNj
%ENDj
%ELSE %Ir &CMDIN = 2 %THEN %DOj
PROC rSCALC SCREEN=SAVE.DEM02j
RUNj
%END;
%ELSE %Ir &CMDIN = 0 %THEN %DO;
%LET DONE=l j
%PUT %STR( );
%PUT Thank you for your interest in PROC rSCALCj
%ENOj
%ELSE %00;
%PUT %STR( )j
%PUT Command &CMOIN not recognized. Please reenter;
%PUT %STR( )j
%ENOj
%ENDj %*--- or %00 %WHILE LOOP ---;
%~IEND rSCALC j
375
MACRO HELP FRAMEWORK
%macro helper(which);
%***************1rn*****************,~*,~*******;
%* framework of a general helper macro
%* for a SAS MACRO
%* it can be called whenever necessary by:
%*
%,~
%helper(A)
%*
I
%,',
--------~ which selection to do
%if &which eq %then %do;
%put %nrstr(
);
%put %nrstr(
general help information) ;
%put %nrstr(
);
%put %nrstr(
);
%end;
%else
%if %upcase(&which) eq A %then %do;
%put %nrstr(
);
%put %nrstr(
);
%put %nrstr(
detailed help information) ;
%put %nrstr(
);
%end;
%else
%if %upcase(&which) eq B %then %do;
%put %nrstr(
);
%put %nrstr(
);
%put %nrstr(
);
%put %nrstr(
);
%put %nrstr(
);
%end;
%else %put no help available;
%mend helper;
376
CREATE MULTIPLE DATA SETS
Create a variable number of subsets with a single
SET statement.
In the following example, a SAS data set named
YEARLY contains a variable called MONTH with
values 1 to 12.
%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,
%SUBSETS(4, ROOTNAME MONTH)
emits this code to SAS:
DATA MONTHl MONTH2 MONTH3 MONTH4;
SET YEARLY;
IF MONTH = 1 THEN OUTPUT MONTH1;
ELSE IF MONTH = 2 THEN OUTPUT MONTH2
ELSE IF MONTH = 3 THEN OUTPUT MONTH3
ELSE IF MONTH = 4 THEN OUTPUT MONTH4
RUN;
DATA STEP REVIEW
SYMGET function
used to assign the value
of a macro variable to a
SAS DATA step variable.
SYMPUT function
used in a SAS DATA step
to create a macro variable
whose value is that of a
SAS DATA step variable.
These functions operate at DATA step execution time.
377
SYMPUT. SYMGET OVERVIEW
These two new DATA step functions
can read and write from th.e symbol table
to the program data vector (pdv) during
execution of a DATA step.
ISAS DATA STEP 1
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
SYMPUT creates macro variables
from DATA step variables.
CALL SYMPUT(argumentl,argument2);
Argumentl names the macro variable.
Argument2 assigns the macro variable value.
378
SYMPUT AND FORMATING EXAMPLE
This example illustrates using a DATA step to
determine the beginning and ending date in a set of
data and then using SYMPUT to create a formatted macro variable
'"
-.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;
SYMPUT AND FORMATING EXAMPLE
S A S LOG
CMS SAS 82.3
VH/CMS
NOTE: CMS SAS RELEASE 82.3 AT SAS INSTITUTE INC. (00000000).
NOTE: SAS OPTIONS SPECIFIED ARE:
CENTER LS=72 PS=40 NODATE NONUHBER
2
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;
NOTE: DATA SET WORK. DATES HAS 7 OBSERVATIONS AND 4 VARIABLES.
NOTE: THE DATA STATEMENT USED 0.36 SECONDS AND 960K.
23
24
25
26
29
RUN;
PROC PRINT;
TITLE . "SALES DATA FROM &START TO &STOP";
NOTE: THE PROCEDURE PRINT USED 0.24 SECONDS AND 960K
AND PRINTED PAGE 1.
29
RUN;
NOTE: SAS USED 960K MEMORY.
S A S LOG
CMS SAS 82.3
NOTE: SAS INSTITUTE INC.
SAS CIRCLE
PO BOX 8000
CARY, N.C. 27511-8000
VH/CHS
CMS USER BOWFIN
SYMPUT AND FORMATING EXAMPLE
SALES DATA FROM 01JAN83 TO .30MAY83
OBS
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
OPTIONS NONPRINT NONACROGEN NOSYMBOLGEN NOMLOGIC DQUOTE H1PWACj
%MACRO NOBS(dsn)/STMTj
%GLOBAL NOBS;
DATA _NULL...;
1=1;
IF _N_=l AND NOBS=O THEN DO;
PUT "THE DATA SET &DSN IS EMPTY. PROCESSING WILL CONTINUE.
PUT 'ERROR STATUS HAS NOT BEEN SET. ';
CALL SYMPUT('NOBS', '0');
STOP;
END;
ELSE DO;
SET &DSN POINT=I NOBS=NOBS;
PUT "THE DATA SET &DSN CONTAINS" NOBS :5. " OBSERVATIONS.
CALL SYMPUT(' NOBS' ,LEFT(PUT(NOBS, BEST.)) j
STOP;
END;
RUN;
%MEND NOBS;
%NOBS
380
"i
SYMGET EXAMPLES
SYMGET returns the value of a macro variable
identified by the argument.
SYNGET(argument);
x=
SYMGET(argument)j
The argument (name of the macro variable)
can have two forms:
1.
the actual name of the macro variable enclosed
in single quotes without an ampersand (&)
X=SYMGET('STATE')j
2.
the name of a DATA step character variable
whose value is the name of a macro variable.
X=SYMGET(PLACE)j
___ ~ where PLACE is a variable in
the PDV with the value STATE.
MACRO SYSTEM OPTIONS: FUNCTION
SAS SYSTEM OPTIONS
MACRO EFFECT
MACRO jNmlACRO
permit macro execution
MACROGENjNOMACROGEN
print macro expansion
MCOMPILEjNOHCOMPILE
load macro compiler by default
MERROR/NOMERROR
unmatched macro names
MLEAVE=
leave k unallocated memory
MLOGIC/NOMLOGIC
trace execution
MSI2E=
k of macro memory
MSYHSI2E=
size of macro symbol table
HWORK=
macro work area size
SYMBOLGEN/NOSYMBOLGEN
print expansion of macro variable(s)
SERROR/NOSERROR
unknown macro variable
SYSPARM='
pass text to
&SYSPAR~1
NOTE: see the SAS USERS GUIDE: BASICS, 1982 EDITION
for complete documentation of system options and
the OPTIONS statement.
381
WARNING MESSAGE OPTIONS
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.
LOG INFORMATION OPTIONS
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
the most useful option--showsl
the end result of macro
1
processing (the statements
1
as the SAS compiler sees
1
them) .
1
SYMBOLGEN
shows resolution of macro
symbolic variables.
1
1
1
1
SOURCE2
displays lines from %INC as 1
they are included, useful if 1
making a macro library.
1
--------------------1
NOMPRINT
must be specified for MLOGIC
to work.
MLOGIC
tells the macro executor
to show internal resolution
of macro variables and calls.
~lACROGEN
specifies whether statements
generated by macro execution
are printed.
DEBUG MACRO
~lACRO
DEBUG(x) / STMT;
%,., Macro from Septe'Dber, 1983
%,.,
No
On
All ?
DEFAULT - NO SHOW;
MPRINT SYMBOLGEN
~ILOGIC ETC
HELP
%if &x= %then %let x=no;
%if %upcase(%substr(&x,l,l=A %then %do;
OPTIONS MLOGIC NO~lPRINT SOURCE
SYMBOLGEN MACROGEN SOURCE2;
%end;
%else
%if %upcase(%substr(&x,l,l)=O %then %do;
OPTIONS NmlLOGIC ~lPRINT SnmOLGEN
NOMACROGEN SOURCE2 SOURCE;
%end;
%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(
DEBUG is a STMT macro to set MACRO options. );
~~put ~'nrs tr (
~~put
%nrstr(
~~put
;{nrstr(
j~Ptlt ~~nrstr(
~~put ;~nrstr(
~:,:,put '~nrstr(
:~put '~nrstr(
~~put
%nrstr(
%nrstr(
~~put
~oend
To use it enter: DEBUG level;,
where level is:
No
Show Nothing %(default%)
On
MPRINT SnIBOLGEN
All - HLOGIC etc.
HELP
First character is all you need
'lEND DEBUG;
383
) ;
) ;
) ;
);
)
)
)
)
)
;
;
;
;
;