SAS Macros Basics (Part-I)

SAS Macros have been studied within 3 parts here. Please go through all the three parts Sequentially;

Follow the links to study;

Part 1: SAS Macros Basics (Part-I)

Part 2: SAS Macros Basics (Part-II) CALL SYMPUT( ) SYMGET( )

Part 3: SAS Macros with parameters (parameterized macros)

· SAS macro facility is used to generate dynamic SAS code by text substitution and facilitates reusability of the code

· Program Flow

Step 1: Submission of SAS program

Step 2: Input Stack (Memory)

Step 3: Word scanner for tokenization

Step 4: Compiler for syntax checking

· Token are considered to be of different types like literals, Numeric, Name, Special characters

· %include is not a call to SAS macro processor; it just puts a SAS code specified by the file on the input stack

· When SAS get invoked Global Symbol Table gets created into memory and all automatic macro variables get loaded into it

· All user defined macro variables get stored into Global Symbol Table itself.

· SAS Macros come in three types depending on how they are called, those are name style, command style, statement style out of these name style is most efficient & normally used

· To see the contents of Global symbol table we can use following commands

For both user defined and system defined macro variables

%put _ALL_;

For User Defined macro variables

%put _USER_;

For Automatic/ System defined macro variables

%put _AUTO_;

· System option symbolgen is used to write the resolved values of macro variables to the log

· %symdel is used to delete the macro variable from memory, as they consume memory directly they should be deleted when no more needed

· In situations where we need macro variables appended by other string we make use of ‘.’ (PERIOD) or ‘..’ sign

Suppose a database table name is generated dynamically using macro variable called tName and the year;

So if use it like &tName2010; SAS will search for macro variable &tName2010 in the global symbol table instead of searching for &tName itself.

To avoid this confusion we use ‘.’ In between.

So finally it would be like &tName.2010

When we need actual ‘.’ At the same place then we can use two consecutive period sings ‘..

· Macro variables placed inside double quotes get resolved but when we place them inside single quoted string SAS doesn’t make an attempt to resolve it

· We can assign any string up to length of 65,534 characters to the macro variable

· Option MCOMPILENOTE = ALL issues a note about the compilation of macro definition in the log

· Compiled macros get stored in the catalogue in the memory, default catalogue is work.sasmacr

· Those macros stored in work.sasmacr catalogue are called session compiled macros.

· We can view contents of catalogue using

PROC CATALOG cat = work.sasmacr;

CONTENTS;

QUIT;

· Autocall facility enables the storage of macro source code in external files and invocation of macros as needed without having to define or include them in the current program of session.

· Different ways to store the macro permanently

Storing it in file with the same name as that of macro and with .SAS extension

Storing it as an object in SASUSER catalogue

Storing it in autocall library

· To create autocall library create a folder; store each macro definition in separate file with .SAS extension and with the name same as macro

· To access autocall library use options MAUTOSOURCE &

SASAUTOS = ‘C:\directoryPathWhereMacrosAreStored’

· Copying macro with code

%COPY macro_name/ SOURCE out = external_file;

Next Part : >> SAS Macros Basics (Part-II) CALL SYMPUT( ) SYMGET( )

SASBuddy Presenting....

First ever FREE Mobile App on SAS Concepts.

SAS Guide

Download it from Playstore Now!!!"