macro processor
is the portion of SAS that does the work
macro language
is the syntax that you use to communicate with the macro processor
When SAS compiles program text, two delimiters trigger macro processor activity:
&name
refers to a macro variable. “Replacing Text Strings Using Macro Variables” on page
8 explains how to create a macro variable. The form &name is called a macro
variable reference.
%name
refers to a macro. “Generating SAS Code Using Macros” on page 9 explains how
to create a macro. The form %name is called a macro call.
The text substitution produced by the macro processor is completed before the program
text is compiled and executed. The macro facility uses statements and functions that
resemble the statements and functions that you use in the DATA step. An important
difference, however, is that macro language elements can enable only text substitution
and are not present during program or command execution.
Note: Three SAS statements begin with a % that are not part of the macro facility.
These elements are the %INCLUDE, %LIST, and %RUN statements in SAS DATA
Step Statements: Reference.
The following graphic explains the syntax used in this document:
Using the Macro Facility in SAS Viya
The macro facility does not run within Cloud Analytic Services (CAS). The macro
facility runs in a SAS client session within SAS Viya. You can use the macro facility to
generate code for procedures, DATA steps, and global statements. Code generated by the
macro facility can use LIBNAME engines. CAS uses caslibs and data connectors instead
of LIBNAME statements.
All macros are created in and executed from a SAS client session. In this example the
macro myMac1 contains a DATA step to add the X variable to the Work.Class data set.
The DATA step created by myMac1 macro uses the V9 engine to read the V9 data set
Class from the SASHELP library and outputs a V9 data set in the Work directory.
4 Chapter 1 Introduction to the Macro Facility
For the myMac2 macro, a CAS LIBNAME statement is entered, which assigns a caslib
named Mycas. The macro myMac2 runs in the SAS client session to add the Y variable
and outputs to the Mycas.Class table.
%macro myMac1;
data class;
set sashelp.class;
x=1;
put " **** ";
put "This macro is running in the SAS-client session
and creating a SAS data set in the Work library";
put " **** ";
put _hostname_ 'thread #' _threadid_;
run;
%mend myMac1;
%myMac1;
quit;
libname mycas cas;
/* macro 2 */
%macro myMac2;
data mycas.class;
set class;
y=2;
put " **** ";
put "This macro is running in the SAS-client session
and loading a CAS-session table in the Mycas library";
put " **** ";
put _hostname_ 'thread #' _threadid_;
run;
%mend myMac2;
%myMac2;
quit;
Here is a condensed version of the log. The following text is repeated for each of the 19
observations.
****
This macro is running in the SAS-client session...
****
rdcesx15179 thread #1
****
Using the Macro Facility in SAS Viya 5

Get SAS 9.4 Macro Language, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.