Hands-On SAS Macro Programming Tips and Techniques
Hands-On SAS Macro Programming Tips and Techniques
The SAS Macro Language is a powerful feature for extending the capabilities of the SAS System. This presentation highlights a
collection of techniques for constructing reusable and effective macros tools. Attendees are introduced to the techniques
associated with building functional macros that process statements containing SAS code; design reusable macro techniques;
create macros containing keyword and positional parameters; utilize defensive programming tactics and techniques; build a
library of macro utilities; interface the macro language with the SQL procedure; and develop efficient and portable macro
language code.
Introduction
The Macro Language is an extension to the SAS System which provides the capability to develop SAS statement text. It consists of its
own set of statements, options, functions, and has its own compiler. When programming with macro statements, the resulting
program is called a MACRO. The Macro Language has its own rules for using the various statements and parameters. The Macro
environment can be thought of as a lower level (3rd Generation) programming environment within the SAS System.
Constant Text
Macro Variables
Macro Functions
Macro Expressions
Constant Text
The macro language treats constant text as character strings. Examples include:
SAS Statements
Macro Variables
Macro variables (symbolic variables) are not DATA step variables, but belong to the SAS System macro language. Symbolic variables,
once defined, can take on many different values during the execution of a macro program. Basic rules that apply to the naming of
symbolic variables are:
The number of characters assigned to a macro variable determines its length no length declaration is made
Leading and trailing blanks are not stored with the value
May be referenced (called) inside or outside of a macro by immediately prefixing an ampersand (&) before the name
The macro processor replaces (substitutes) the symbolic variable with the value of the symbolic variable
A couple examples are provided to help clarify the creation and use of macro variables.
References Inside a Macro:
%LET NAME=USERFILE.MASTER;
%MACRO M;
PROC MEANS DATA=&NAME;
RUN;
%MEND M;
Macro Functions
Macro functions are available to process text in macros and with macro variable values. Some macro functions are associated with
DATA step functions while others are used only in the macro processor. You may notice a similarity between DATA step functions
and macro functions. To illustrate how macro functions can be used, a few examples are shown below.
Examples:
%INDEX(argument1,argument2)
%STR(argument)
%UPCASE(argument)
%BQUOTE(argument)
Macro Expressions
Macro expressions consist of macro statements, macro variable names, constant text, and/or function names combined together.
Their purpose is to tie processing operations together through the use of operators and parentheses.
Examples:
IF &TOTAL > 999 THEN WEIGHT=WEIGHT+1;
&CHAR = %LENGTH(&SPAN)
&COUNT = %EVAL(&COUNT + 1);
SAS Option
Description
MACRO
MEMERR
MEMRPT
MERROR
MLOGIC
MPRINT
SYMBOLGEN
Specifies that the macro language SYMGET and SYMPUT functions be available.
Controls Diagnostics.
Specifies that memory usage statistics be displayed on the SAS Log.
Presents Warning Messages when there are misspellings or when an undefined macro is called.
Macro execution is traced and displayed on the SAS Log for debugging purposes.
SAS statements generated by macro execution are traced on the SAS Log for debugging purposes.
Displays text from expanding macro variables to the SAS Log.
SAS Option
Description
MAUTOSOURCE
Turns on the Autocall Facility so stored macro programs are included in the search for macro
definitions.
Turns on the capability to search stored macro programs when a macro is not found.
Specifies the location of the stored macro programs.
MRECALL
SASAUTOS=
SASAUTOS
Macro Name
%CHNGCASE
%CMPRES
%DATATYP
%LEFT
%LOWCASE
%SYSRC
%TRIM
%VERIFY
To help illustrate a SASAUTOS macro, we will display the contents of the %TRIM autocall macro below. The purpose of the
%TRIM autocall macro is to remove (or trim) trailing blanks from text and return the result.
SAS Option
Description
MSTORED
SASMSTORE=
Turns on the Compiled Macro Facility so you can take advantage of this feature.
Specifies the libref associated with the SAS catalog, SASMACR, of stored compiled macros.
Definition
help
reshow
end;
...
keys
command focus
%POSTSUBMIT
PROC SQL;
SELECT LIBNAME, MEMNAME, NAME, TYPE, LENGTH
FROM DICTIONARY.COLUMNS
WHERE UPCASE(LIBNAME)="MYDATA" AND
UPCASE(NAME)="TITLE" AND
UPCASE(MEMTYPE)="DATA";
QUIT;
Output
Library
Name
MYDATA
MYDATA
MYDATA
MYDATA
MYDATA
Member Name
ACTORS
MOVIES
PG_MOVIES
PG_RATED_MOVIES
RENTAL_INFO
Column Name
Title
Title
Title
Title
Title
Column
Type
char
char
char
char
char
Column
Length
30
30
30
30
30
Now lets examine another useful macro that is designed with a positional parameter. The following macro is designed to
accept one positional parameter called &LIB. When called, it accesses the read-only Dictionary table TABLES to display each
table name and the number of observations in the user-assigned MYDATA libref. This macro provides a handy way to quickly
determine the number of observations in one or all tables in a libref without having to execute multiple PROC CONTENTS by
using the stored information in the Dictionary table TABLES.
Macro Code
%MACRO NUMROWS(LIB);
PROC SQL;
SELECT LIBNAME, MEMNAME, NOBS
FROM DICTIONARY.TABLES
WHERE UPCASE(LIBNAME)="&LIB" AND
UPCASE(MEMTYPE)="DATA";
QUIT;
%MEND NUMROWS;
%NUMROWS(MYDATA);
PROC SQL;
SELECT LIBNAME, MEMNAME, NOBS
FROM DICTIONARY.TABLES
WHERE UPCASE(LIBNAME)="MYDATA" AND
UPCASE(MEMTYPE)="DATA";
QUIT;
Output
Library
Name
MYDATA
MYDATA
MYDATA
MYDATA
MYDATA
MYDATA
Number of Physical
Observations
22
3
22
7
13
13
Member Name
MOVIES
CUSTOMERS
MOVIES
PATIENTS
PG_MOVIES
PG_RATED_MOVIES
Output
G
PG
PG-13
Conclusion
The macro language provides SAS users with a powerful language environment for constructing a library of powerful tools,
routines, and reusable programs. It offers a comprehensive set of statements, options, functions, and has its own compiler. Once
written and debugged macro programs can be stored in a location on your operating environment that can be referenced and
accessed using an autocall macro environment. Macros can also be compiled providing for a more efficient process for executing
macros because the macro does not have to be compiled over and over again. Finally, users are able to design and construct reusable
macro tools that can be used again and again.
References
Burlew, Michele M. (1998), SAS Macro Programming Made Easy, SAS Institute Inc., Cary, NC, USA.
Carpenter, Art (2004), Carpenters Complete Guide to the SAS Macro Language, Second Edition. SAS Institute Inc., Cary, NC,
USA.
Lafler, Kirk Paul (2013), Hands-on SAS Macro Programming Tips and Techniques, Proceedings of the 2013 Western Users of
SAS Software (WUSS) Conference, Software Intelligence Corporation, Spring Valley, CA, USA.
Lafler, Kirk Paul (2013), Hands-on SAS Macro Programming Tips and Techniques, Proceedings of the 2013 MidWest SAS Users
Group (MWSUG) Conference, Software Intelligence Corporation, Spring Valley, CA, USA.
Lafler, Kirk Paul (2013), Hands-on SAS Macro Programming Tips and Techniques, Proceedings of the 2013 SAS Global Forum
(SGF) Conference, Software Intelligence Corporation, Spring Valley, CA, USA.
Lafler, Kirk Paul (2012), SAS Macro Programming Tips and Techniques, Proceedings of the 2012 PharmaSUG Conference,
Software Intelligence Corporation, Spring Valley, CA, USA.
Lafler, Kirk Paul (2012), Building ReusableTools with the SAS Macro Language, Proceedings of the 2012 SAS Global Forum
(SGF) Conference, Software Intelligence Corporation, Spring Valley, CA, USA.
Lafler, Kirk Paul (2009), Building Reusable and Highly Effective Tools with the SAS Macro Language, PharmaSUG 2009
Conference, Software Intelligence Corporation, Spring Valley, CA, USA.
Lafler, Kirk Paul (2008), Building Reusable SAS Macro Tools, Michigan SAS Users Group 2008 Conference, Software
Intelligence Corporation, Spring Valley, CA, USA.
Lafler, Kirk Paul (2007), SAS Macro Programming Tips and Techniques, Proceedings of the NorthEast SAS Users Group
(NESUG) 2007 Conference, Software Intelligence Corporation, Spring Valley, CA, USA.
Lafler, Kirk Paul (2009), SAS System Macro Language Course Notes, Fifth Edition. Software Intelligence Corporation, Spring
Valley, CA, USA.
Lafler, Kirk Paul (2007), SAS System Macro Language Course Notes, Fourth Edition. Software Intelligence Corporation, Spring
Valley, CA, USA.
Lafler, Kirk Paul (2008), Exploring DICTIONARY Tables and SASHELP Views, Software Intelligence Corporation, Spring Valley, CA,
USA.
Lafler, Kirk Paul (2006), Exploring DICTIONARY Tables and SASHELP Views, Software Intelligence Corporation, Spring Valley, CA,
USA.
Lafler, Kirk Paul (2004), PROC SQL: Beyond the Basics Using SAS, SAS Institute Inc., Cary, NC, USA.
Roberts, Clark (1997), Building and Using Macro Variable Lists, Proceedings of the Twenty-second Annual SAS Users Group
International Conference, San Diego, CA, 441-443.
SAS Macro Language: Reference, SAS OnlineDoc 9.2, SAS Institute Inc., Cary, NC, USA.
Acknowledgments
I want to thank Kenny Bissett and Lizette Alonzo, SCSUG 2013 Conference Chairs for accepting my abstract and paper, as well as
the SCSUG Executive and Conference Committees, and SAS Institute for organizing a great conference!
Trademarks Citations
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the
USA and other countries. indicates USA registration. Other brand and product names are trademarks of their respective
companies.
Author Information
Kirk Paul Lafler is consultant and founder of Software Intelligence Corporation and has been using SAS since 1979. He is a SAS
Certified Professional, provider of IT consulting services, trainer to SAS users around the world, and sasCommunity.org emeritus
Advisory Board member. As the author of five books including PROC SQL: Beyond the Basics Using SAS, Second Edition (SAS
Press 2013); PROC SQL: Beyond the Basics Using SAS (2004), Kirk has written more than five hundred papers and articles, been
an Invited speaker and trainer at four hundred-plus SAS International, regional, special-interest, local, and in-house user group
conferences and meetings, and is the recipient of 22 Best contributed paper, hands-on workshop (HOW), and poster awards.
Comments and suggestions can be sent to:
Kirk Paul Lafler
Senior Consultant, Application Developer, Data Analyst, Trainer and Author
Software Intelligence Corporation
E-mail: [email protected]
LinkedIn: http://www.linkedin.com/in/KirkPaulLafler
Twitter: @SASnerd