Data Analysis using the SAS Language/Macro Programming

Introduction
Macros and macro variables can be used to change a SAS program while it is running. Macro variables are also used to define SAS code that is used in several parts of the SAS program. The following demonstrates the use of macro variables and the resulting code. When the macro variables are processed above, the final SAS code looks as follows: The discussion will explain some of the finer points about macros and show more complex and useful examples.

Macro Variables
Macro variables are assigned using the %let statement. The assignment occurs during preprocess before the nonmacro code is parsed during processing. A macro variable value is a string of tokens, which may be a single character, a portion of a statement, or an entire set of SAS statements comprising what SAS calls a step. The syntax for expansion of a macro variable is ampersand macro-variable-name, e.g. &variablename. When a macro variable reference is encountered during preprocessing the macro variable reference is replaced by the tokens, i.e. the characters in the string. These tokens become part of the SAS statements submitted for compile and execution.

Macro Statements
There are several macro statements similar to the SAS data step statemetns. These statements include assignments, logic and loops. The difference is that these statements operate on macro variable and produce SAS code. This can make a powerful SAS program dynamic and flexible. From a programming standpoint, a section of SAS code can be written once, debugged, placed in a macro and then used as often as needed by placing the macro reference in the code at each point it is needed.

%let
The macro variable assignment statement %let is a global statement.

symput symget
data demo; input sno sname $; datalines; 100 naresh Reddy 101 Kranthi

proc sql; select sname into :sname separated by ' ' from demo; run; %put &sname;

System Macro Variables
SAS maintains many macro variables which provide system information. They can be reviewed with this %put statement.

The log shows the following values.

AUTOMATIC SYSADDRBITS 32 ... AUTOMATIC SYSDATE 17APR15 AUTOMATIC SYSDATE9 17APR2015 AUTOMATIC SYSDAY Friday ... AUTOMATIC SYSHOSTNAME MY-LAPTOP ... AUTOMATIC SYSJOBID 8068 ... AUTOMATIC SYSPROCESSID 41DA0058F5CBF7CF4018000000000000 ... AUTOMATIC SYSTCPIPHOSTNAME MY-LAPTOP AUTOMATIC SYSTIME 20:25 AUTOMATIC SYSUSERID RFMM AUTOMATIC SYSVER 9.4

Note: the first 16 digits of sys-process-id are a date-time stamp in hex16.

The log shows the following values.

8   %put &sysdate &systime; 17APR15 20:25 9   %put %sysfunc(putn(%substr(&sysprocessid,1,16)x,datetime.)); 17APR15:20:25:59

Macro Modules
Macro modules and simple macros, are combination of macro statments that produce SAS code. Modules can take parameters which can be used to produce dynamic SAS code.