Data Analysis using the SAS Language/Macro Programming

Completion status: this resource is ~50% complete.
Subject classification: this is a science resource.
Subject classification: this is a statistics resource.
Educational level: this is a tertiary (university) resource.

Introduction

edit

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.

     %let listStates='NY' 'NJ' 'CT';
     %let reportYear=2008;
     proc print data=sales(where=(state in (&listStates) and year=&reportYear));
          title "Sales Report for Selected States for and Year &reportYear";
          title2 "States in report: &listStates";
          id state;
          var Month SalesVolume Purchases;
          sum salesVolume Purchases;
     run;

When the macro variables are processed above, the final SAS code looks as follows:

     proc print data=sales(where=(state in ('NY' 'NJ' 'CT') and year=2008));
          title "Sales Report for Selected States for and Year 2008";
          title2 "States in report: 'NY' 'NJ' 'CT'";
          id state;
          var Month SalesVolume Purchases;
          sum salesVolume Purchases;
     run;

The discussion will explain some of the finer points about macros and show more complex and useful examples.

Macro Variables

edit

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

edit

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.

Assignment

edit

%let

edit

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

     %let mytitle="Apple Shipment as of &sysdate";
     title "&mytitle";
     proc print data=shipment;
     run;

symput symget

edit

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

edit

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

%put _automatic_;

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.

%put &sysdate &systime;
%put %sysfunc(putn(%substr(&sysprocessid,1,16)x,datetime.));

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 Logic

edit

%if %then %else

edit

Macro Loops

edit

%do %while %end

edit

%do %until %end

edit

Macro Definition %macro

edit

Macro Modules

edit

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.