You are on page 1of 19

SAS Macro

Click to edit Master subtitle style

3/23/13

What Is the SAS Macro Facility?


SAS

macro facility is a tool for text substitution. SAS macro facility is a component of Base SAS. two main components of the SAS macro facility are SAS macro variables and SAS macro programs.

The The

3/23/13

Advantages of the SAS Macro Facility


The You You

programs you write can become reusable, shorter, and easier to follow. can accomplish repetitive tasks quickly and efficiently. can provide a more modular structure to your programs.

3/23/13

Macro Variables

3/23/13

Macro Variables
A

macro variable can be referenced anywhere in a SAS program other than in data lines. variables can be used in open code as well as in macro programs. values assigned to macro variables are considered text values. name assigned to a macro variable must be a valid SAS name.

Macro All

The

3/23/13

Referencing Macro Variables


You

tell the macro processor to resolve a macro variable value by preceding the macro variable name with an ampersand (&). referencing macro variables in a SAS statement, double quotation marks enclosing a string allow resolution of macro variable references while single quotation marks do not.

When

3/23/13

Types of Macro Variables


Automatic:

Built-in macro variables. The automatic variables listed depend on the SAS products installed at your site and on your operating system. User-defined global macro variables. User-defined local macro variables. Local macro variables are those defined in the currently executing macro program that have not been designated as global macro variables.
3/23/13

Global: Local:

Examples
%let path = D:\Sanjay\SAS Material\SAS Practice Downloads\SAS Data ; libname practice "&path";

3/23/13

%PUT Statement
The

%PUT statement instructs the macro processor to write information to the SAS log.
%put _automatic_ %put ; _global_; %put The value of practice is &path;

3/23/13

SYMBOLGEN Option
With

SYMBOLGEN enabled, SAS presents the results of the resolution of macro variables in the SAS log. displays the value of a macro variable in the SAS log near the statement with the macro variable reference.

SYMBOLGEN

options symbolgen;
3/23/13

Automatic Macro Variables


SAS

automatically defines a set of macro variables when a SAS session starts. macro processor maintains these variables and their values in the macro symbol table.

The

3/23/13

Some of the automatic Macro Variables


SYSDATE:

the character value that is equal to the date the SAS session started in DATE7. format. text of the day of the week the SAS session started name of the most recently created data set in one field: WORK.TEMP

SYSDAY:

SYSLAST:

3/23/13

User-Defined Macro Variables


One

way to create and update a macro variable is with a %LET statement.

Syntax:

%let year = 98; %let macro-variable-name=macro-variable-value; proc print data = sasuser.stress&year; run; %let dataset = calves; title "Dataset: &dataset"; proc print data = practice.&dataset; run;
3/23/13

Macro programs

3/23/13

Syntax
%MACRO program <(parameter-list)></option(s)>; <text> %MEND <program>;

3/23/13

Executing a Macro Program


A

macro program is executed by submitting a reference to the macro program.


where

%program

program is the name assigned to the macro program.

3/23/13

Examples
%macro printing(dataset); title "Dataset: &dataset"; proc print data = practice.&dataset; run; %mend; %printing(cars)

3/23/13

Mprint
By

default, SAS language statements submitted from within a macro program are not written to the SAS log. see the SAS code that the macro processor constructs and submits, MPRINT option should be enabled.

To

options mprint;

3/23/13

mlogic
The

information written to the SAS log when MLOGIC is enabled includes :


the the

beginning and ending of the macro program

results of arithmetic and logical macro language operations.

The

MLOGIC option is useful for debugging macro language statements in macro programs.
options mlogic;
3/23/13

You might also like