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
Log 1.1 Creating the Work.Class Data Set and the Mycas.Class Table
58 /* MACROS */
59 /* macro 1 */
60 %macro myMac1;
61 data class;
62 set sashelp.class;
63 x=1;
64 put " **** ";
65 put "This macro is running in the SAS-client
session and creating a SAS data set in the
WORK library";
66 put " **** ";
67 put _hostname_ 'thread #' _threadid_;
68 run;
69 %mend myMac1;
70 %myMac1;
****
This macro is running in the SAS-client session and
creating a SAS data set in the WORK library.
****
rdcesx15179 thread #1
****
...
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.CLASS has 19 observations and 6 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.00 seconds
71 quit;
72
73 libname mycas cas;
NOTE: Libref MYCAS was successfully assigned as follows:
Engine: CAS
Physical Name: f84824ee
74
75 /* macro 2 */
76 %macro myMac2;
77 data mycas.class;
78 set class;
79 y=2;
80 put " **** ";
81 put "This macro is running in the SAS-client
session and loading a CAS-session table in
the Mycas library";
82 put " **** ";
83 put _hostname_ 'thread #' _threadid_;
84 run;
85 %mend myMac2;
86 %myMac2;
****
This macro is running in the SAS-client session and loading a CAS-session table
in the Mycas library
****
rdcesx15179 thread #1
****
...
NOTE: There were 19 observations read from the data set WORK.CLASS.
NOTE: The data set MYCAS.CLASS has 19 observations and 7 variables.
NOTE: DATA statement used (Total process time):
real time 0.04 seconds
cpu time 0.01 seconds
87 quit;
6 Chapter 1
Introduction to the Macro Facility
Here is the Work.Class data set with the X variable added.
Figure 1.1 Work.Class Data Set
Here is the Mycas.Class table with the Y variable added.
Figure 1.2 Mycas.Class Table
Using the Macro Facility in SAS Viya 7
..................Content has been hidden....................

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