proc contents;
run;
proc freq;
tables _numeric_;
run;
%quick: proc print data=_last_(obs=10); /* Use % here */
run;
%mend info;
%info(short)
Invoking the macro INFO with TYPE equal to short generates these statements:
PROC PRINT DATA=_LAST_(OBS=10);
RUN;
%LET Statement
Creates a macro variable and assigns it a value.
Type: Macro statement
Restriction: Allowed in macro definitions or open code
See: “%STR and %NRSTR Functions” on page 272
Syntax
%LET macro-variable=<value>;
Required Arguments
macro-variable
is either the name of a macro variable or a text expression that produces a macro
variable name. The name can refer to a new or existing macro variable.
value
is a character string or a text expression. Omitting value produces a null value (0
characters). Leading and trailing blanks in value are ignored. To make them
significant, enclose value with the %STR function.
Details
If the macro variable that is named in the %LET statement already exists in any
enclosing scope, the %LET statement updates the value. If the macro variable that is
named in the %LET statement does not exist, it is created in the closest enclosing scope
and it is assigned the specified value. A %LET statement can define only one macro
variable at a time.
Example: Sample %LET Statements
This example illustrates several %LET statements:
%macro title(text,number);
%put TITLE&number "&text";
%mend;
%let topic= The History of Genetics ; /* Leading and trailing */
/* blanks are removed */
322 Chapter 19 Macro Statements
%title(&topic,1)
%let subject=topic; /* &subject resolves */
%let &subject=Genetics Today; /* before assignment */
%title(&topic,2)
%let subject=The Future of Genetics; /* &subject resolves */
%let topic= &subject; /* before assignment */
%title(&topic,3)
When you submit these statements, the TITLE macro generates the following
statements:
13 %macro title(text,number);
14 %put TITLE&number "&text";
15 %mend;
16 %let topic= The History of Genetics ;
17
18 %title(&topic,1)
TITLE1 "The History of Genetics"
19 %let subject=topic;
20 %let &subject=Genetics Today;
21 %title(&topic,2)
TITLE2 "Genetics Today"
22 %let subject=The Future of Genetics;
23 %let topic= &subject;
24 %title(&topic,3)
TITLE3 "The Future of Genetics"
%LOCAL Statement
Creates macro variables that are available only during the execution of the macro where they are defined.
Type: Macro statement
Restriction: Allowed in macro definitions only
See: “%GLOBAL Statement” on page 314
Syntax
%LOCAL macro-variable(s);
Or
%LOCAL / READONLY macro-variable=value;
Required Argument
macro-variable(s)
is the name of one or more macro variables or a text expression that generates one or
more macro variable names. You cannot use a SAS variable list or a macro
expression that generates a SAS variable list in a %LOCAL statement.
Optional Argument
READONLY macro-variable=value
creates a new read only local macro variable.
%LOCAL Statement 323
Note: The READONLY option can be used to create a single new macro variable
(local or global).
macro-variable
is the name of a macro variable or a text expression that produces a macro
variable name. The name must be a new macro variable name.
value
is a character string or a text expression.
T I P Omitting value produces a null value (0 characters).
T I P Leading and trailing blanks in the value are ignored. To have leading and
trailing blanks contained in the value, you must enclose the value within the
%STR function.
Details
The %LOCAL statement creates one or more local macro variables. A macro variable
created with %LOCAL has a null value until you assign it some other value. Local
macro variables are variables that are available only during the execution of the macro in
which they are defined.
Use the %LOCAL statement to ensure that macro variables created earlier in a program
are not inadvertently changed by values assigned to variables with the same name in the
current macro. If a local macro variable already exists and you specify that variable in a
%LOCAL statement, the existing value remains unchanged.
%LOCAL statements that use the READONLY option create a new local macro variable
and assign the specified value. Existing macro variables cannot be made read-only. The
value of the local macro variable cannot be changed and the variable cannot be deleted.
A macro variable that is declared with the READONLY option cannot be re-declared in
the same scope or any enclosed scope. All read-only macro variables persist until the
scope in which they exist is deleted.
Comparisons
Both the %LOCAL statement and the %GLOBAL statement create macro variables
with a specific scope. However, the %LOCAL statement creates local macro
variables that exist only during the execution of the macro that contains the variable,
and the %GLOBAL statement creates global macro variables that exist for the
duration of the session or job.
If you define a local macro variable and a global macro variable with the same name,
the macro facility uses the value of the local variable during the execution of the
macro that contains that local variable. When the macro that contains the local
variable is not executing, the macro facility uses the value of the global variable.
Example: Using a Local Variable with the Same Name as
a Global Variable
%let variable=1;
%macro routine;
%put ***** Beginning ROUTINE *****;
%local variable;
%let variable=2;
%put The value of variable inside ROUTINE is &variable;
%put ***** Ending ROUTINE *****;
324 Chapter 19 Macro Statements
%mend routine;
%routine
%put The value of variable outside ROUTINE is &variable;
Submitting these statements writes these lines to the SAS log:
***** Beginning ROUTINE *****
The value of variable inside ROUTINE is 2
***** Ending ROUTINE *****
The value of variable outside ROUTINE is 1
%MACRO Statement
Begins a macro definition.
Type: Macro statement
Restriction: Allowed in macro definitions or open code
See: “%MEND Statement” on page 331 and “SYSPBUFF Automatic Macro Variable” on
page 220
Syntax
%MACRO macro-name <(parameter-list)> </ option(s)>;
Required Arguments
macro-name
names the macro. A macro name must be a SAS name, which you supply; you
cannot use a text expression to generate a macro name in a %MACRO statement. In
addition, do not use macro reserved words as a macro name. (For a list of macro
reserved words, see Appendix 1, “ Reserved Words in the Macro Facility,” on page
391.)
parameter-list
names one or more local macro variables whose values you specify when you invoke
the macro. Parameters are local to the macro that defines them. You must supply
each parameter name; you cannot use a text expression to generate it. A parameter
list can contain any number of macro parameters separated by commas. The macro
variables in the parameter list are usually referenced in the macro.
parameter-list can be
<positional-parameter-1><,positional-parameter-2 ...>
<keyword-parameter=<value> <,keyword-parameter-2=<value> ...>>
positional-parameter-1
<,positional-
parameter-2 ...>
specifies one or more positional parameters. You
can specify positional parameters in any order,
but in the macro invocation, the order in which
you specify the values must match the order that
you list them in the %MACRO statement. If you
define more than one positional parameter, use a
comma to separate the parameters. If at
invocation that you do not supply a value for a
%MACRO Statement 325
positional parameter, the macro facility assigns a
null value to that parameter.
keyword-
parameter=<value>
<,keyword-
parameter-2=<value> ...>
names one or more macro parameters followed
by equal signs. You can specify default values
after the equal signs. If you omit a default value
after an equal sign, the keyword parameter has a
null value. Using default values enables you to
write more flexible macro definitions and
reduces the number of parameters that must be
specified to invoke the macro. To override the
default value, specify the macro variable name
followed by an equal sign and the new value in
the macro invocation.
Note: You can define an unlimited number of parameters. If both positional and
keyword parameters appear in a macro definition, positional parameters must
come first.
option(s)
can be one or more of these optional arguments:
CMD
specifies that the macro can accept either a name-style invocation or a command-
style invocation. Macros defined with the CMD option are sometimes called
command-style macros.
Use the CMD option only for macros that you plan to execute from the command
line of a SAS window. The SAS system option CMDMAC must be in effect to
use command-style invocations. If CMDMAC is in effect and you have defined a
command-style macro in your program, the macro processor scans the first word
of every SAS command to see whether it is a command-style macro invocation.
When the SAS system option NOCMDMAC option is in effect, the macro
processor treats only the words following the % symbols as potential macro
invocations. If the CMDMAC option is not in effect, you still can use a name-
style invocation for a macro defined with the CMD option.
DES='text'
specifies a description for the macro entry in the macro catalog. The description
text can be up to 256 characters in length. Enclose the description in quotation
marks. This description appears in the CATALOG window when you display the
contents of the catalog containing the stored compiled macros. The DES= option
is especially useful when you use the stored compiled macro facility.
MINDELIMITER='single character';
specifies a value that will override the value of the MINDELIMITER= global
option. The value must be a single character enclosed in single quotation marks
and can appear only once in a %MACRO statement.
Restriction
The following characters cannot be used as a delimiter
% & ' " ( ) ;
MINOPERATOR | NOMINOPERATOR
specifies that the macro processor recognizes and evaluates the mnemonic IN
and the special character # as logical operators when evaluating arithmetic or
logical expressions during the execution of the macro. The setting of this
argument overrides the setting of the NOMINOPERATOR global system option.
326 Chapter 19 Macro Statements
..................Content has been hidden....................

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