sas® macro programming tips and techniques

8
 1 SAS  ®  Macro Programming Tips and Techniques Kirk Paul Lafler, Software Intelligence Corporation, Spring Valley, California Abstract The SAS ®  Macro Language is a powerful tool for extending the capabilities of the SAS System. This hands-on workshop presents numerous tips and tricks related to the c onstruction of effective macros through the demonstration of a collection of proven Macro Language coding techniques. Attendees learn how to process statements containing macros; replace text strings with macro variables; generate SAS code using macros; manipulate macro variable values with macro functions; handle global and local variables; construct arithmetic and logical expressions; interface the macro language with the DATA step and SQL procedure; store and reuse macros; troubleshoot and debug macros; 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. Macro Language Basics The macro language provides an additional set of tools to assist in: 1) communicating between SAS steps, 2) constructing executable and reusable code, 3) designing custom languages, 4) developing user-friendly routines, and 5) conditionally execute DATA or PROC steps. When a program is run, the SAS System first checks to see if a macro statement exists. If the program does not contain any macro statements, then processing continues as normal with the DATA or PROC step processor. If the program does contain one or more macro statements, then the macro processor must first execute them. The result of this execution is the production of character information, macro variables, or SAS statements, which are then be passed to the DATA or PROC step processor . The control flow of a macro process appears in Figure 1 below. The SAS System Log displays information about the compilation and execution of a SAS program. This information is a vital part of any SAS execution which when viewed provides information about: 1) What statements were executed, 2) What SAS System data sets were created, 3) The number of variables and observations each data set contains, and 4) The time and memory expended by each DATA and PROC step. Figure 1. Macro Program Control Flow. Foundations & Fundamentals NESUG 2008

Upload: bmobilelee

Post on 02-Jun-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 18

1

SAS reg Macro Programming Tips and Techniques

Kirk Paul Lafler Software Intelligence Corporation Spring Valley California

AbstractThe SAS

reg Macro Language is a powerful tool for extending the capabilities of the SAS System This hands-on

workshop presents numerous tips and tricks related to the construction of effective macros through the demonstrationof a collection of proven Macro Language coding techniques Attendees learn how to process statements containingmacros replace text strings with macro variables generate SAS code using macros manipulate macro variablevalues with macro functions handle global and local variables construct arithmetic and logical expressions interfacethe macro language with the DATA step and SQL procedure store and reuse macros troubleshoot and debugmacros and develop efficient and portable macro language code

IntroductionThe Macro Language is an extension to the SAS System which provides the capability to develop SAS statement text Itconsists of its own set of statements options functions and has its own compiler When programming with macrostatements the resulting program is called a MACRO The Macro Language has its own rules for using the variousstatements and parameters The Macro environment can be thought of as a lower level (3rd Generation) programmingenvironment within the SAS System

Macro Language BasicsThe macro language provides an additional set of tools to assist in 1) communicating between SAS steps 2) constructingexecutable and reusable code 3) designing custom languages 4) developing user-friendly routines and 5) conditionallyexecute DATA or PROC steps

When a program is run the SAS System first checks to see if a macro statement exists If the program does notcontain any macro statements then processing continues as normal with the DATA or PROC step processor If theprogram does contain one or more macro statements then the macro processor must first execute them The resultof this execution is the production of character information macro variables or SAS statements which are then bepassed to the DATA or PROC step processor The control flow of a macro process appears in Figure 1 below

The SAS System Log displays information about the compilation and execution of a SAS program This information is avital part of any SAS execution which when viewed provides information about 1) What statements were executed 2)What SAS System data sets were created 3) The number of variables and observations each data set contains and 4)The time and memory expended by each DATA and PROC step

Figure 1 Macro Program Control Flow

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 28

2

The Anatomy of a MacroEvery macro begins with a MACRO and must contain a name for the macro To close a macro a MEND is used andcan optionally specify the macro name for documentation reasons Macro text can include any of the followinginformation

bull Constant Text

bull Macro Variables

bull Macro Functions

bull Macro Program Statements

bull Macro Expressions

Constant TextThe macro language treats constant text as character strings Examples include

bull SAS Data Set Names

bull SAS Variable Names

bull SAS Statements

Macro VariablesMacro variables (symbolic variables) are not DATA step variables but belong to the SAS System macro languageSymbolic variables once defined can take on many different values during the execution of a macro program Basic rulesthat apply to the naming of symbolic variables are

bull A name can be one to eight characters in length

bull A name must begin with a character (A-Z) or underscore (_)

bull Letters numbers and underscores can follow the first character

Basic rules that apply to the use of symbolic variables include

bull Values range from 0 to 1024 bytes in length

bull May be referenced (called) inside or outside of a macro by immediately prefixing an ampersand (amp) before the

namebull 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

983116E983124 983118A983117E=983125983123E983122F983113983116E983117A983123983124E983122

983117AC983122983119 983117983120983122983119C 983117EA983118983123 DA983124A=amp983118A983117E983122983125983118

983117E983118D 983117

References Outside a Macro

983120983122983119C 983120983122983113983118983124 DA983124A=amp983118A983117E983122983125983118

Macro FunctionsMacro functions are available to process text in macros and with macro variable values Some macro functions areassociated with DATA step functions while others are used only in the macro processor You may notice a similaritybetween DATA step functions and macro functions To illustrate how macro functions can be used a few examples areshown below

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 38

3

Examples

983113983118DE983128(98313798315498314398315798314998314198315098315619831379831549831439831579831499831419831509831562)

983123983124983122(983137983154983143983157983149983141983150983156)

983125983120CA983123E(983137983154983143983157983149983141983150983156)

B983121983125983119983124E(983137983154983143983157983149983141983150983156)

Macro Program StatementsThe macro language provides a powerful language environment for users to construct and use macro programs Thereare a number of Macro program statements many of which resemble DATA step statements in use and functionalityMacro program statements are available to instruct the macro processor what to do Each statement begins with apercent sign () and is terminated with a semi-colon () The statements are executed by the macro processor and thenpassed to either the DATA or PROC step for processing

Examples

D983119

E983118D

G983116983119BA983116 983149983137983139983154983151983085983158983137983154983145983137983138983148983141

983117AC983122983119 983150983137983149983141983131(983152983137983154983137983149983141983156983141983154983155)983123983124983117983124983133

Macro ExpressionsMacro expressions consist of macro statements macro variable names constant text andor function names combinedtogether Their purpose is to tie processing operations together through the use of operators and parentheses

Examples

983113F amp983124983119983124A983116 gt 999 983124983112E983118 983127E983113G983112983124=983127E983113G983112983124+1

ampC983112A983122 = 983116E983118G983124983112(amp983123983120A983118)

ampC983119983125983118983124 = E983126A983116(ampC983119983125983118983124 + 1)

Tip 1 ndash Debugging a Macro with SAS System Options

The SAS System offers users a number of useful system options to help debug macro issues and problems Theresults associated with using macro options are automatically displayed on the SAS Log Specific options related tomacro debugging appear in alphabetical order in the table below

SAS Option DescriptionMACRO Specifies that the macro language SYMGET and SYMPUT functions be availableMEMERR Controls Diagnostics

MEMRPT Specifies that memory usage statistics be displayed on the SAS LogMERROR Presents Warning Messages when there are misspellings or when an undefined macro is calledMLOGIC Macro execution is traced and displayed on the SAS Log for debugging purposesMPRINT SAS statements generated by macro execution are traced on the SAS Log for debugging

purposesSYMBOLGEN Displays text from expanding macro variables to the SAS Log

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 48

4

Tip 2 ndash Using the Autocall Facility to Call a Macro

Macro programs can be stored as SAS programs in a location in your operating environment and called on-demandusing the built-in autocall facility Macro programs stored this way are defined once and referenced (or called)anytime needed This provides an effective way to store and manage your macro programs in a library aggregate Tofacilitate the autocall environment you will need to specify the SAS System options presented in the following table

SAS Option Description

MAUTOSOURCE Turns on the Autocall Facility so stored macro programs are included in the search for macrodefinitions

MRECALL Turns on the capability to search stored macro programs when a macro is not foundSASAUTOS= Specifies the location of the stored macro programs

Tip 3 ndash Accessing the SAS Institute-supplied Autocall Macros

Users may be unaware that SAS Institute has provided as part of your SAS software an autocall library of existingmacros These autocall macros are automatically found in your default SASAUTOS fileref For example the defaultlocation of the SASAUTOS fileref under Windows XP Professional on my computer is cprogram filessassas91coresasmacro Readers are encouraged to refer to the SAS Companion manual for the operating environmentyou are running under for further details

Numerous SAS-supplied autocall macros are included ndash many of which act and behave as macro functions It isworth mentioning that these autocall macros provide a wealth of effective coding techniques and can be useful as ameans of improving macro coding prowess in particular for those users who learn by example The following tabledepicts an alphabetical sampling of the SAS Institute-supplied autocall macros for SAS 91

SASAUTOSMacro Name SASAUTOS Macro DescriptionCHNGCASE This macro is used in the change dialog box for pmenusCMPRES This macro returns the argument passed to it in an unquoted form with multiple blanks

compressed to single blanks and also with leading and trailing blanks removedDATATYP The DATATYP macro determines if the input parameter is NUMERIC or CHARacter data and

returns either CHAR or NUMERIC depending on the value passed through the parameterLEFT This macro returns the argument passed to it without any leading blanks in an unquoted formLOWCASE This macro returns the argument passed to it unchanged except that all upper-case alphabetic

characters are changed to their lower-case equivalentsSYSRC This macro returns a numeric value corresponding to the mnemonic string passed to it and

should only be used to check return code values from SCL functionsTRIM This macro returns the argument passed to it without any trailing blanks in an unquoted formVERIFY This macro returns the position of the first character in the argument that is not in the target

value

To help illustrate a SASAUTOS macro we will display the contents of the TRIM autocall macro below The purposeof the TRIM autocall macro is to remove (or trim) trailing blanks from text and return the result

TRIM AUTOCALL Macro

983149983137983139983154983151 983156983154983145983149(983158983137983148983157983141)

983117AC983122983119 983124983122983113983117

983125983123AGE 1) 983156983154983145983149(983137983154983143983157983149983141983150983156)

DE983123C983122983113983120983124983113983119983118

983124983144983145983155 983149983137983139983154983151 983154983141983156983157983154983150983155 983156983144983141 983137983154983143983157983149983141983150983156 983152983137983155983155983141983140 983156983151 983145983156 983159983145983156983144983151983157983156 983137983150983161 983156983154983137983145983148983145983150983143 983138983148983137983150983147983155 983145983150 983137983150 983157983150983153983157983151983156983141983140 983142983151983154983149 983124983144983141 983155983161983150983156983137983160 983142983151983154 983145983156983155 983157983155983141

983145983155 983155983145983149983145983148983137983154 983156983151 983156983144983137983156 983151983142 983150983137983156983145983158983141 983149983137983139983154983151 983142983157983150983139983156983145983151983150983155

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 58

5

E983143 983148983141983156 983149983137983139983158983137983154=983156983154983145983149(amp983137983154983143983156983141983160983156)

983118983119983124E983123

983118983151983150983141

983148983151983139983137983148 983145

983140983151 983145=983148983141983150983143983156983144(amp983158983137983148983157983141) 983156983151 1 983138983161 9830851983145983142 983153983155983157983138983155983156983154(amp983158983137983148983157983141amp9831451)983134=983155983156983154( ) 983156983144983141983150 983143983151983156983151 983156983154983145983149983149983141983140

983141983150983140

983156983154983145983149983149983141983140 983145983142 amp983145gt0 983156983144983141983150 983155983157983138983155983156983154(amp9831589831379831489831579831411amp983145)

983149983141983150983140

Tip 4 ndash Compiling a Stored Macro with the Compiled Macro Facility

A macro can be compiled once and the compiled version stored so it can be used over and over again Thisapproach saves time and resources because the macro does not have to be compiled each time it is called To takeadvantage of this time-saving approach you will need to either verify andor turn on the SAS System optionsMSTORED and SASMSTORE You will also need to specify the STORE option of the MACRO statement It isworth mentioning that during macro compilation only macro statements are compiled so be aware that non-macrotext and macro references are not evaluated during the compilation phase ndash but during macro execution

SAS Option DescriptionMSTORED Turns on the Compiled Macro Facility so you can take advantage of this featureSASMSTORE= Specifies the libref associated with the SAS catalog SASMACR This catalog stores compiled

macros

Tip 5 ndash Streamlining Command-line DMS Commands with a Macro

The macro language is a wonderful tool for streamlining frequently entered SAS Display Manager System (DMS)commands to reduce the number of keystrokes By embedding a series of DMS commands inside a simple macroyoursquoll not only save by not having to enter them over and over again but yoursquoll improve your productivity as well Thefollowing macro code illustrates a series of DMS commands being strung together in lieu of entering them individuallyon a Display Manager command line The commands display and expand the SAS Log to full size respectively andthen position the cursor at the top of the log Once the macro is defined it can be called by entering POSTSUBMITon any DMS command line to activate the commands

Macro Code

983117AC983122983119 983152983151983155983156983155983157983138983149983145983156983116983151983143983130983151983151983149

983124983151983152

983117E983118D 983152983151983155983156983155983157983138983149983145983156

Tip 6 ndash Assigning a Defined Macro to a Function Key

To further reduce keystrokes and enhance user productivity even further a call to a defined macro can be saved to aFunction Key The purpose for doing this would be to allow for one-button operation of any defined macro Toillustrate the process of saving a macro call to a Function Key the POSTSUBMIT macro defined in the previous tipis assigned to Function Key F12 in the KEYS window The partial KEYS window is displayed to illustrate the process

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 68

6

KEYS Window

Key Definition

F1 helpF2 reshowF3 end

F10 keysF11 command focusF12 POSTSUBMIT

Tip 7 ndash Defining Positional Parameters

Macros are frequently designed to allow the passing of one or more parameters This allows the creation of macrovariables so text strings can be passed into the macro The order of macro variables as positional parameters isspecified when the macro is coded The assignment of values for each positional parameter is supplied at the timethe macro is called

To illustrate the definition of a two positional parameter macro the following macro was created to display all table

names (data sets) that contain the variable TITLE in the user-assigned MYDATA libref as a cross-reference listingTo retrieve the needed type of information you could execute multiple PROC CONTENTS against selected tables Orin a more efficient method you could retrieve the information directly from the read-only Dictionary table COLUMNSwith the selected columns LIBNAME MEMNAME NAME TYPE and LENGTH as shown For more informationabout Dictionary tables readers may want to view the ldquofreerdquo SAS Press Webinar by Kirk Paul Lafler athttpsupportsascompublishingbbuwebinarhtmllafler2 or the published paper by Kirk Paul Lafler ExploringDictionary Tables and SASHELP Views

Macro Code

983117AC983122983119 C983119983116983125983117983118983123(983116983113983106 983107983119983116983118983105983117983109)983120983122983119C 983123983121983116

983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118A983117E 983124983129983120E 983116E983118G983124983112

F983122983119983117 D983113C983124983113983119983118A983122983129C983119983116983125983117983118983123

983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=amp983116983113983106 A983118D

983125983120CA983123E(983118A983117E)=amp983107983119983116983118983105983117983109 A983118D983125983120CA983123E(983117E983117983124983129983120E)=DA983124A

983121983125983113983124

983117E983118D C983119983116983125983117983118983123

983107983119983116983125983117983118983123(983117983129983108983105983124983105983124983113983124983116983109)983099

After Macro Resolution

983120983122983119C 983123983121983116983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118A983117E 983124983129983120E 983116E983118G983124983112

F983122983119983117 D983113C983124983113983119983118A983122983129C983119983116983125983117983118983123983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=983117983129DA983124A A983118D983125983120CA983123E(983118A983117E)=983124983113983124983116E A983118D983125983120CA983123E(983117E983117983124983129983120E)=DA983124A

983121983125983113983124

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 78

7

Output

Now letrsquos examine another useful macro that is designed with a positional parameter The following macro isdesigned to accept one positional parameter called ampLIB When called it accesses the read-only Dictionary tableTABLES to display each table name and the number of observations in the user-assigned MYDATA libref Thismacro provides a handy way to quickly determine the number of observations in one or all tables in a libref withouthaving to execute multiple PROC CONTENTS by using the stored information in the Dictionary table TABLES

Macro Code

983117AC983122983119 983118983125983117983122983119983127983123(983116983113983106)

983120983122983119C 983123983121983116983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118983119B983123

F983122983119983117 D983113C983124983113983119983118A983122983129983124AB983116E983123

983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=amp983116983113983106 A983118D

983125983120CA983123E(983117E983117983124983129983120E)=DA983124A983121983125983113983124

983117E983118D 983118983125983117983122983119983127983123

983118983125983117983122983119983127983123(983117983129983108983105983124983105)983099

After Macro Resolution

Output

983116983145983138983154983137983154983161 983107983151983148983157983149983150 983107983151983148983157983149983150983118983137983149983141 983117983141983149983138983141983154 983118983137983149983141 983107983151983148983157983149983150 983118983137983149983141 983124983161983152983141 983116983141983150983143983156983144

983117983129983108983105983124983105 983105983107983124983119983122983123 983124983145983156983148983141 983139983144983137983154 30983117983129983108983105983124983105 983117983119983126983113983109983123 983124983145983156983148983141 983139983144983137983154 30

983117983129983108983105983124983105 983120983111983135983117983119983126983113983109983123 983124983145983156983148983141 983139983144983137983154 30

983117983129983108983105983124983105 983120983111983135983122983105983124983109983108983135983117983119983126983113983109983123 983124983145983156983148983141 983139983144983137983154 30983117983129983108983105983124983105 983122983109983118983124983105983116983135983113983118983110983119 983124983145983156983148983141 983139983144983137983154 30

983120983122983119C 983123983121983116

983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118983119B983123

F983122983119983117 D983113C983124983113983119983118A983122983129983124AB983116E983123983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=983117983129DA983124A A983118D

983125983120CA983123E(983117E983117983124983129983120E)=DA983124A983121983125983113983124

983116983145983138983154983137983154983161 983118983157983149983138983141983154 983151983142 983120983144983161983155983145983139983137983148983118983137983149983141 983117983141983149983138983141983154 983118983137983149983141 983119983138983155983141983154983158983137983156983145983151983150983155

983117983129983108983105983124983105 983117983119983126983113983109983123 22983117983129983108983105983124983105 983107983125983123983124983119983117983109983122983123 3983117983129983108983105983124983105 983117983119983126983113983109983123 22

983117983129983108983105983124983105 983120983105983124983113983109983118983124983123 983095983117983129983108983105983124983105 983120983111983135983117983119983126983113983109983123 13983117983129983108983105983124983105 983120983111983135983122983105983124983109983108983135983117983119983126983113983109983123 13

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 88

8

ConclusionThe macro language provides SAS users with a powerful language environment for constructing a library of powerfultools routines and reusable programs It offers a comprehensive set of statements options functions and has its owncompiler Once written and debugged macro programs can be stored in a location on your operating environment that canbe referenced and accessed using an autocall macro environment Macros can also be compiled providing for a moreefficient process for executing macros because the macro does not have to be compiled over and over again Finallyusers are able to design and construct reusable macro tools that can be used again and again

ReferencesBurlew Michele M (1998) SAS Macro Programming Made Easy SAS Institute Inc Cary NC USA

Carpenter Art (2004) Carpenterrsquos Complete Guide to the SAS Macro Language Second Edition SAS Institute IncCary NC USA

Lafler Kirk Paul (2008) ldquoBuilding Reusable SASreg Macro Toolsrdquo Michigan SAS Users Group 2008 Conference

Software Intelligence Corporation Spring Valley CA USA

Lafler Kirk Paul (2007) ldquoSAS Macro Programming Tips and Techniquesrdquo Proceedings of the NorthEast SAS UsersGroup (NESUG) 2007 Conference Software Intelligence Corporation Spring Valley CA USA

Lafler Kirk Paul (2007) SAS System Macro Language Course Notes Fourth Edition Software IntelligenceCorporation Spring Valley CA USA

Lafler Kirk Paul (2006) Exploring DICTIONARY Tables and SASHELP Views Software Intelligence CorporationSpring Valley CA USA

Lafler Kirk Paul (2004) PROC SQL Beyond the Basics Using SAS SAS Institute Inc Cary NC USA

Roberts Clark (1997) ldquoBuilding and Using Macro Variable Listsrdquo Proceedings of the Twenty-second Annual SASUsers Group International Conference San Diego CA 441-443

SAS Macro Language Reference SAS OnlineDocreg 92 SAS Institute Inc Cary NC USA

SAS Macro Language Reference SAS OnlineDocreg 913 SAS Institute Inc Cary NC USA

TrademarksSAS and all other SAS Institute Inc product or service names are registered trademarks or trademarks of SASInstitute Inc and other countries reg indicates USA registration

AcknowledgmentsI would like to thank the NorthEast SAS Users Group (NESUG) Conference Team including Ken Borowiak and Gary

McQuown for inviting me to present this paper and special thanks are also extended to Earl Westerlund and theNESUG 2008 Executive Committee for their support of a great Conference

Author InformationKirk Paul Lafler is consultant and founder of Software Intelligence Corporation and has been programming in SASsince 1979 As a SAS Certified Professional and SAS Institute Alliance Member (1996 ndash 2002) Kirk provides ITconsulting services and training to SAS users around the world As the author of four books including PROC SQLBeyond the Basics Using SAS (SAS Institute 2004) Kirk has written more than two hundred peer-reviewed papersand articles that have appeared in professional journals and SAS User Group proceedings He has also been anInvited speaker at more than two hundred SAS International regional local and special-interest user groupconferences and meetings throughout North America His popular SAS Tips column ldquoKirkrsquos Korner of Quick andSimple Tipsrdquo appears regularly in several SAS User Group newsletters and Web sites and his fun-filled SASwordPuzzles are featured in SAScommunityorg and User Group Newsletters

Comments and suggestions can be sent to

Kirk Paul LaflerSoftware Intelligence Corporation

World HeadquartersPO Box 1390

Spring Valley California 91979-1390E-mail KirkLaflercscom

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 28

2

The Anatomy of a MacroEvery macro begins with a MACRO and must contain a name for the macro To close a macro a MEND is used andcan optionally specify the macro name for documentation reasons Macro text can include any of the followinginformation

bull Constant Text

bull Macro Variables

bull Macro Functions

bull Macro Program Statements

bull Macro Expressions

Constant TextThe macro language treats constant text as character strings Examples include

bull SAS Data Set Names

bull SAS Variable Names

bull SAS Statements

Macro VariablesMacro variables (symbolic variables) are not DATA step variables but belong to the SAS System macro languageSymbolic variables once defined can take on many different values during the execution of a macro program Basic rulesthat apply to the naming of symbolic variables are

bull A name can be one to eight characters in length

bull A name must begin with a character (A-Z) or underscore (_)

bull Letters numbers and underscores can follow the first character

Basic rules that apply to the use of symbolic variables include

bull Values range from 0 to 1024 bytes in length

bull May be referenced (called) inside or outside of a macro by immediately prefixing an ampersand (amp) before the

namebull 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

983116E983124 983118A983117E=983125983123E983122F983113983116E983117A983123983124E983122

983117AC983122983119 983117983120983122983119C 983117EA983118983123 DA983124A=amp983118A983117E983122983125983118

983117E983118D 983117

References Outside a Macro

983120983122983119C 983120983122983113983118983124 DA983124A=amp983118A983117E983122983125983118

Macro FunctionsMacro functions are available to process text in macros and with macro variable values Some macro functions areassociated with DATA step functions while others are used only in the macro processor You may notice a similaritybetween DATA step functions and macro functions To illustrate how macro functions can be used a few examples areshown below

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 38

3

Examples

983113983118DE983128(98313798315498314398315798314998314198315098315619831379831549831439831579831499831419831509831562)

983123983124983122(983137983154983143983157983149983141983150983156)

983125983120CA983123E(983137983154983143983157983149983141983150983156)

B983121983125983119983124E(983137983154983143983157983149983141983150983156)

Macro Program StatementsThe macro language provides a powerful language environment for users to construct and use macro programs Thereare a number of Macro program statements many of which resemble DATA step statements in use and functionalityMacro program statements are available to instruct the macro processor what to do Each statement begins with apercent sign () and is terminated with a semi-colon () The statements are executed by the macro processor and thenpassed to either the DATA or PROC step for processing

Examples

D983119

E983118D

G983116983119BA983116 983149983137983139983154983151983085983158983137983154983145983137983138983148983141

983117AC983122983119 983150983137983149983141983131(983152983137983154983137983149983141983156983141983154983155)983123983124983117983124983133

Macro ExpressionsMacro expressions consist of macro statements macro variable names constant text andor function names combinedtogether Their purpose is to tie processing operations together through the use of operators and parentheses

Examples

983113F amp983124983119983124A983116 gt 999 983124983112E983118 983127E983113G983112983124=983127E983113G983112983124+1

ampC983112A983122 = 983116E983118G983124983112(amp983123983120A983118)

ampC983119983125983118983124 = E983126A983116(ampC983119983125983118983124 + 1)

Tip 1 ndash Debugging a Macro with SAS System Options

The SAS System offers users a number of useful system options to help debug macro issues and problems Theresults associated with using macro options are automatically displayed on the SAS Log Specific options related tomacro debugging appear in alphabetical order in the table below

SAS Option DescriptionMACRO Specifies that the macro language SYMGET and SYMPUT functions be availableMEMERR Controls Diagnostics

MEMRPT Specifies that memory usage statistics be displayed on the SAS LogMERROR Presents Warning Messages when there are misspellings or when an undefined macro is calledMLOGIC Macro execution is traced and displayed on the SAS Log for debugging purposesMPRINT SAS statements generated by macro execution are traced on the SAS Log for debugging

purposesSYMBOLGEN Displays text from expanding macro variables to the SAS Log

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 48

4

Tip 2 ndash Using the Autocall Facility to Call a Macro

Macro programs can be stored as SAS programs in a location in your operating environment and called on-demandusing the built-in autocall facility Macro programs stored this way are defined once and referenced (or called)anytime needed This provides an effective way to store and manage your macro programs in a library aggregate Tofacilitate the autocall environment you will need to specify the SAS System options presented in the following table

SAS Option Description

MAUTOSOURCE Turns on the Autocall Facility so stored macro programs are included in the search for macrodefinitions

MRECALL Turns on the capability to search stored macro programs when a macro is not foundSASAUTOS= Specifies the location of the stored macro programs

Tip 3 ndash Accessing the SAS Institute-supplied Autocall Macros

Users may be unaware that SAS Institute has provided as part of your SAS software an autocall library of existingmacros These autocall macros are automatically found in your default SASAUTOS fileref For example the defaultlocation of the SASAUTOS fileref under Windows XP Professional on my computer is cprogram filessassas91coresasmacro Readers are encouraged to refer to the SAS Companion manual for the operating environmentyou are running under for further details

Numerous SAS-supplied autocall macros are included ndash many of which act and behave as macro functions It isworth mentioning that these autocall macros provide a wealth of effective coding techniques and can be useful as ameans of improving macro coding prowess in particular for those users who learn by example The following tabledepicts an alphabetical sampling of the SAS Institute-supplied autocall macros for SAS 91

SASAUTOSMacro Name SASAUTOS Macro DescriptionCHNGCASE This macro is used in the change dialog box for pmenusCMPRES This macro returns the argument passed to it in an unquoted form with multiple blanks

compressed to single blanks and also with leading and trailing blanks removedDATATYP The DATATYP macro determines if the input parameter is NUMERIC or CHARacter data and

returns either CHAR or NUMERIC depending on the value passed through the parameterLEFT This macro returns the argument passed to it without any leading blanks in an unquoted formLOWCASE This macro returns the argument passed to it unchanged except that all upper-case alphabetic

characters are changed to their lower-case equivalentsSYSRC This macro returns a numeric value corresponding to the mnemonic string passed to it and

should only be used to check return code values from SCL functionsTRIM This macro returns the argument passed to it without any trailing blanks in an unquoted formVERIFY This macro returns the position of the first character in the argument that is not in the target

value

To help illustrate a SASAUTOS macro we will display the contents of the TRIM autocall macro below The purposeof the TRIM autocall macro is to remove (or trim) trailing blanks from text and return the result

TRIM AUTOCALL Macro

983149983137983139983154983151 983156983154983145983149(983158983137983148983157983141)

983117AC983122983119 983124983122983113983117

983125983123AGE 1) 983156983154983145983149(983137983154983143983157983149983141983150983156)

DE983123C983122983113983120983124983113983119983118

983124983144983145983155 983149983137983139983154983151 983154983141983156983157983154983150983155 983156983144983141 983137983154983143983157983149983141983150983156 983152983137983155983155983141983140 983156983151 983145983156 983159983145983156983144983151983157983156 983137983150983161 983156983154983137983145983148983145983150983143 983138983148983137983150983147983155 983145983150 983137983150 983157983150983153983157983151983156983141983140 983142983151983154983149 983124983144983141 983155983161983150983156983137983160 983142983151983154 983145983156983155 983157983155983141

983145983155 983155983145983149983145983148983137983154 983156983151 983156983144983137983156 983151983142 983150983137983156983145983158983141 983149983137983139983154983151 983142983157983150983139983156983145983151983150983155

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 58

5

E983143 983148983141983156 983149983137983139983158983137983154=983156983154983145983149(amp983137983154983143983156983141983160983156)

983118983119983124E983123

983118983151983150983141

983148983151983139983137983148 983145

983140983151 983145=983148983141983150983143983156983144(amp983158983137983148983157983141) 983156983151 1 983138983161 9830851983145983142 983153983155983157983138983155983156983154(amp983158983137983148983157983141amp9831451)983134=983155983156983154( ) 983156983144983141983150 983143983151983156983151 983156983154983145983149983149983141983140

983141983150983140

983156983154983145983149983149983141983140 983145983142 amp983145gt0 983156983144983141983150 983155983157983138983155983156983154(amp9831589831379831489831579831411amp983145)

983149983141983150983140

Tip 4 ndash Compiling a Stored Macro with the Compiled Macro Facility

A macro can be compiled once and the compiled version stored so it can be used over and over again Thisapproach saves time and resources because the macro does not have to be compiled each time it is called To takeadvantage of this time-saving approach you will need to either verify andor turn on the SAS System optionsMSTORED and SASMSTORE You will also need to specify the STORE option of the MACRO statement It isworth mentioning that during macro compilation only macro statements are compiled so be aware that non-macrotext and macro references are not evaluated during the compilation phase ndash but during macro execution

SAS Option DescriptionMSTORED Turns on the Compiled Macro Facility so you can take advantage of this featureSASMSTORE= Specifies the libref associated with the SAS catalog SASMACR This catalog stores compiled

macros

Tip 5 ndash Streamlining Command-line DMS Commands with a Macro

The macro language is a wonderful tool for streamlining frequently entered SAS Display Manager System (DMS)commands to reduce the number of keystrokes By embedding a series of DMS commands inside a simple macroyoursquoll not only save by not having to enter them over and over again but yoursquoll improve your productivity as well Thefollowing macro code illustrates a series of DMS commands being strung together in lieu of entering them individuallyon a Display Manager command line The commands display and expand the SAS Log to full size respectively andthen position the cursor at the top of the log Once the macro is defined it can be called by entering POSTSUBMITon any DMS command line to activate the commands

Macro Code

983117AC983122983119 983152983151983155983156983155983157983138983149983145983156983116983151983143983130983151983151983149

983124983151983152

983117E983118D 983152983151983155983156983155983157983138983149983145983156

Tip 6 ndash Assigning a Defined Macro to a Function Key

To further reduce keystrokes and enhance user productivity even further a call to a defined macro can be saved to aFunction Key The purpose for doing this would be to allow for one-button operation of any defined macro Toillustrate the process of saving a macro call to a Function Key the POSTSUBMIT macro defined in the previous tipis assigned to Function Key F12 in the KEYS window The partial KEYS window is displayed to illustrate the process

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 68

6

KEYS Window

Key Definition

F1 helpF2 reshowF3 end

F10 keysF11 command focusF12 POSTSUBMIT

Tip 7 ndash Defining Positional Parameters

Macros are frequently designed to allow the passing of one or more parameters This allows the creation of macrovariables so text strings can be passed into the macro The order of macro variables as positional parameters isspecified when the macro is coded The assignment of values for each positional parameter is supplied at the timethe macro is called

To illustrate the definition of a two positional parameter macro the following macro was created to display all table

names (data sets) that contain the variable TITLE in the user-assigned MYDATA libref as a cross-reference listingTo retrieve the needed type of information you could execute multiple PROC CONTENTS against selected tables Orin a more efficient method you could retrieve the information directly from the read-only Dictionary table COLUMNSwith the selected columns LIBNAME MEMNAME NAME TYPE and LENGTH as shown For more informationabout Dictionary tables readers may want to view the ldquofreerdquo SAS Press Webinar by Kirk Paul Lafler athttpsupportsascompublishingbbuwebinarhtmllafler2 or the published paper by Kirk Paul Lafler ExploringDictionary Tables and SASHELP Views

Macro Code

983117AC983122983119 C983119983116983125983117983118983123(983116983113983106 983107983119983116983118983105983117983109)983120983122983119C 983123983121983116

983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118A983117E 983124983129983120E 983116E983118G983124983112

F983122983119983117 D983113C983124983113983119983118A983122983129C983119983116983125983117983118983123

983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=amp983116983113983106 A983118D

983125983120CA983123E(983118A983117E)=amp983107983119983116983118983105983117983109 A983118D983125983120CA983123E(983117E983117983124983129983120E)=DA983124A

983121983125983113983124

983117E983118D C983119983116983125983117983118983123

983107983119983116983125983117983118983123(983117983129983108983105983124983105983124983113983124983116983109)983099

After Macro Resolution

983120983122983119C 983123983121983116983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118A983117E 983124983129983120E 983116E983118G983124983112

F983122983119983117 D983113C983124983113983119983118A983122983129C983119983116983125983117983118983123983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=983117983129DA983124A A983118D983125983120CA983123E(983118A983117E)=983124983113983124983116E A983118D983125983120CA983123E(983117E983117983124983129983120E)=DA983124A

983121983125983113983124

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 78

7

Output

Now letrsquos examine another useful macro that is designed with a positional parameter The following macro isdesigned to accept one positional parameter called ampLIB When called it accesses the read-only Dictionary tableTABLES to display each table name and the number of observations in the user-assigned MYDATA libref Thismacro provides a handy way to quickly determine the number of observations in one or all tables in a libref withouthaving to execute multiple PROC CONTENTS by using the stored information in the Dictionary table TABLES

Macro Code

983117AC983122983119 983118983125983117983122983119983127983123(983116983113983106)

983120983122983119C 983123983121983116983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118983119B983123

F983122983119983117 D983113C983124983113983119983118A983122983129983124AB983116E983123

983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=amp983116983113983106 A983118D

983125983120CA983123E(983117E983117983124983129983120E)=DA983124A983121983125983113983124

983117E983118D 983118983125983117983122983119983127983123

983118983125983117983122983119983127983123(983117983129983108983105983124983105)983099

After Macro Resolution

Output

983116983145983138983154983137983154983161 983107983151983148983157983149983150 983107983151983148983157983149983150983118983137983149983141 983117983141983149983138983141983154 983118983137983149983141 983107983151983148983157983149983150 983118983137983149983141 983124983161983152983141 983116983141983150983143983156983144

983117983129983108983105983124983105 983105983107983124983119983122983123 983124983145983156983148983141 983139983144983137983154 30983117983129983108983105983124983105 983117983119983126983113983109983123 983124983145983156983148983141 983139983144983137983154 30

983117983129983108983105983124983105 983120983111983135983117983119983126983113983109983123 983124983145983156983148983141 983139983144983137983154 30

983117983129983108983105983124983105 983120983111983135983122983105983124983109983108983135983117983119983126983113983109983123 983124983145983156983148983141 983139983144983137983154 30983117983129983108983105983124983105 983122983109983118983124983105983116983135983113983118983110983119 983124983145983156983148983141 983139983144983137983154 30

983120983122983119C 983123983121983116

983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118983119B983123

F983122983119983117 D983113C983124983113983119983118A983122983129983124AB983116E983123983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=983117983129DA983124A A983118D

983125983120CA983123E(983117E983117983124983129983120E)=DA983124A983121983125983113983124

983116983145983138983154983137983154983161 983118983157983149983138983141983154 983151983142 983120983144983161983155983145983139983137983148983118983137983149983141 983117983141983149983138983141983154 983118983137983149983141 983119983138983155983141983154983158983137983156983145983151983150983155

983117983129983108983105983124983105 983117983119983126983113983109983123 22983117983129983108983105983124983105 983107983125983123983124983119983117983109983122983123 3983117983129983108983105983124983105 983117983119983126983113983109983123 22

983117983129983108983105983124983105 983120983105983124983113983109983118983124983123 983095983117983129983108983105983124983105 983120983111983135983117983119983126983113983109983123 13983117983129983108983105983124983105 983120983111983135983122983105983124983109983108983135983117983119983126983113983109983123 13

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 88

8

ConclusionThe macro language provides SAS users with a powerful language environment for constructing a library of powerfultools routines and reusable programs It offers a comprehensive set of statements options functions and has its owncompiler Once written and debugged macro programs can be stored in a location on your operating environment that canbe referenced and accessed using an autocall macro environment Macros can also be compiled providing for a moreefficient process for executing macros because the macro does not have to be compiled over and over again Finallyusers are able to design and construct reusable macro tools that can be used again and again

ReferencesBurlew Michele M (1998) SAS Macro Programming Made Easy SAS Institute Inc Cary NC USA

Carpenter Art (2004) Carpenterrsquos Complete Guide to the SAS Macro Language Second Edition SAS Institute IncCary NC USA

Lafler Kirk Paul (2008) ldquoBuilding Reusable SASreg Macro Toolsrdquo Michigan SAS Users Group 2008 Conference

Software Intelligence Corporation Spring Valley CA USA

Lafler Kirk Paul (2007) ldquoSAS Macro Programming Tips and Techniquesrdquo Proceedings of the NorthEast SAS UsersGroup (NESUG) 2007 Conference Software Intelligence Corporation Spring Valley CA USA

Lafler Kirk Paul (2007) SAS System Macro Language Course Notes Fourth Edition Software IntelligenceCorporation Spring Valley CA USA

Lafler Kirk Paul (2006) Exploring DICTIONARY Tables and SASHELP Views Software Intelligence CorporationSpring Valley CA USA

Lafler Kirk Paul (2004) PROC SQL Beyond the Basics Using SAS SAS Institute Inc Cary NC USA

Roberts Clark (1997) ldquoBuilding and Using Macro Variable Listsrdquo Proceedings of the Twenty-second Annual SASUsers Group International Conference San Diego CA 441-443

SAS Macro Language Reference SAS OnlineDocreg 92 SAS Institute Inc Cary NC USA

SAS Macro Language Reference SAS OnlineDocreg 913 SAS Institute Inc Cary NC USA

TrademarksSAS and all other SAS Institute Inc product or service names are registered trademarks or trademarks of SASInstitute Inc and other countries reg indicates USA registration

AcknowledgmentsI would like to thank the NorthEast SAS Users Group (NESUG) Conference Team including Ken Borowiak and Gary

McQuown for inviting me to present this paper and special thanks are also extended to Earl Westerlund and theNESUG 2008 Executive Committee for their support of a great Conference

Author InformationKirk Paul Lafler is consultant and founder of Software Intelligence Corporation and has been programming in SASsince 1979 As a SAS Certified Professional and SAS Institute Alliance Member (1996 ndash 2002) Kirk provides ITconsulting services and training to SAS users around the world As the author of four books including PROC SQLBeyond the Basics Using SAS (SAS Institute 2004) Kirk has written more than two hundred peer-reviewed papersand articles that have appeared in professional journals and SAS User Group proceedings He has also been anInvited speaker at more than two hundred SAS International regional local and special-interest user groupconferences and meetings throughout North America His popular SAS Tips column ldquoKirkrsquos Korner of Quick andSimple Tipsrdquo appears regularly in several SAS User Group newsletters and Web sites and his fun-filled SASwordPuzzles are featured in SAScommunityorg and User Group Newsletters

Comments and suggestions can be sent to

Kirk Paul LaflerSoftware Intelligence Corporation

World HeadquartersPO Box 1390

Spring Valley California 91979-1390E-mail KirkLaflercscom

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 38

3

Examples

983113983118DE983128(98313798315498314398315798314998314198315098315619831379831549831439831579831499831419831509831562)

983123983124983122(983137983154983143983157983149983141983150983156)

983125983120CA983123E(983137983154983143983157983149983141983150983156)

B983121983125983119983124E(983137983154983143983157983149983141983150983156)

Macro Program StatementsThe macro language provides a powerful language environment for users to construct and use macro programs Thereare a number of Macro program statements many of which resemble DATA step statements in use and functionalityMacro program statements are available to instruct the macro processor what to do Each statement begins with apercent sign () and is terminated with a semi-colon () The statements are executed by the macro processor and thenpassed to either the DATA or PROC step for processing

Examples

D983119

E983118D

G983116983119BA983116 983149983137983139983154983151983085983158983137983154983145983137983138983148983141

983117AC983122983119 983150983137983149983141983131(983152983137983154983137983149983141983156983141983154983155)983123983124983117983124983133

Macro ExpressionsMacro expressions consist of macro statements macro variable names constant text andor function names combinedtogether Their purpose is to tie processing operations together through the use of operators and parentheses

Examples

983113F amp983124983119983124A983116 gt 999 983124983112E983118 983127E983113G983112983124=983127E983113G983112983124+1

ampC983112A983122 = 983116E983118G983124983112(amp983123983120A983118)

ampC983119983125983118983124 = E983126A983116(ampC983119983125983118983124 + 1)

Tip 1 ndash Debugging a Macro with SAS System Options

The SAS System offers users a number of useful system options to help debug macro issues and problems Theresults associated with using macro options are automatically displayed on the SAS Log Specific options related tomacro debugging appear in alphabetical order in the table below

SAS Option DescriptionMACRO Specifies that the macro language SYMGET and SYMPUT functions be availableMEMERR Controls Diagnostics

MEMRPT Specifies that memory usage statistics be displayed on the SAS LogMERROR Presents Warning Messages when there are misspellings or when an undefined macro is calledMLOGIC Macro execution is traced and displayed on the SAS Log for debugging purposesMPRINT SAS statements generated by macro execution are traced on the SAS Log for debugging

purposesSYMBOLGEN Displays text from expanding macro variables to the SAS Log

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 48

4

Tip 2 ndash Using the Autocall Facility to Call a Macro

Macro programs can be stored as SAS programs in a location in your operating environment and called on-demandusing the built-in autocall facility Macro programs stored this way are defined once and referenced (or called)anytime needed This provides an effective way to store and manage your macro programs in a library aggregate Tofacilitate the autocall environment you will need to specify the SAS System options presented in the following table

SAS Option Description

MAUTOSOURCE Turns on the Autocall Facility so stored macro programs are included in the search for macrodefinitions

MRECALL Turns on the capability to search stored macro programs when a macro is not foundSASAUTOS= Specifies the location of the stored macro programs

Tip 3 ndash Accessing the SAS Institute-supplied Autocall Macros

Users may be unaware that SAS Institute has provided as part of your SAS software an autocall library of existingmacros These autocall macros are automatically found in your default SASAUTOS fileref For example the defaultlocation of the SASAUTOS fileref under Windows XP Professional on my computer is cprogram filessassas91coresasmacro Readers are encouraged to refer to the SAS Companion manual for the operating environmentyou are running under for further details

Numerous SAS-supplied autocall macros are included ndash many of which act and behave as macro functions It isworth mentioning that these autocall macros provide a wealth of effective coding techniques and can be useful as ameans of improving macro coding prowess in particular for those users who learn by example The following tabledepicts an alphabetical sampling of the SAS Institute-supplied autocall macros for SAS 91

SASAUTOSMacro Name SASAUTOS Macro DescriptionCHNGCASE This macro is used in the change dialog box for pmenusCMPRES This macro returns the argument passed to it in an unquoted form with multiple blanks

compressed to single blanks and also with leading and trailing blanks removedDATATYP The DATATYP macro determines if the input parameter is NUMERIC or CHARacter data and

returns either CHAR or NUMERIC depending on the value passed through the parameterLEFT This macro returns the argument passed to it without any leading blanks in an unquoted formLOWCASE This macro returns the argument passed to it unchanged except that all upper-case alphabetic

characters are changed to their lower-case equivalentsSYSRC This macro returns a numeric value corresponding to the mnemonic string passed to it and

should only be used to check return code values from SCL functionsTRIM This macro returns the argument passed to it without any trailing blanks in an unquoted formVERIFY This macro returns the position of the first character in the argument that is not in the target

value

To help illustrate a SASAUTOS macro we will display the contents of the TRIM autocall macro below The purposeof the TRIM autocall macro is to remove (or trim) trailing blanks from text and return the result

TRIM AUTOCALL Macro

983149983137983139983154983151 983156983154983145983149(983158983137983148983157983141)

983117AC983122983119 983124983122983113983117

983125983123AGE 1) 983156983154983145983149(983137983154983143983157983149983141983150983156)

DE983123C983122983113983120983124983113983119983118

983124983144983145983155 983149983137983139983154983151 983154983141983156983157983154983150983155 983156983144983141 983137983154983143983157983149983141983150983156 983152983137983155983155983141983140 983156983151 983145983156 983159983145983156983144983151983157983156 983137983150983161 983156983154983137983145983148983145983150983143 983138983148983137983150983147983155 983145983150 983137983150 983157983150983153983157983151983156983141983140 983142983151983154983149 983124983144983141 983155983161983150983156983137983160 983142983151983154 983145983156983155 983157983155983141

983145983155 983155983145983149983145983148983137983154 983156983151 983156983144983137983156 983151983142 983150983137983156983145983158983141 983149983137983139983154983151 983142983157983150983139983156983145983151983150983155

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 58

5

E983143 983148983141983156 983149983137983139983158983137983154=983156983154983145983149(amp983137983154983143983156983141983160983156)

983118983119983124E983123

983118983151983150983141

983148983151983139983137983148 983145

983140983151 983145=983148983141983150983143983156983144(amp983158983137983148983157983141) 983156983151 1 983138983161 9830851983145983142 983153983155983157983138983155983156983154(amp983158983137983148983157983141amp9831451)983134=983155983156983154( ) 983156983144983141983150 983143983151983156983151 983156983154983145983149983149983141983140

983141983150983140

983156983154983145983149983149983141983140 983145983142 amp983145gt0 983156983144983141983150 983155983157983138983155983156983154(amp9831589831379831489831579831411amp983145)

983149983141983150983140

Tip 4 ndash Compiling a Stored Macro with the Compiled Macro Facility

A macro can be compiled once and the compiled version stored so it can be used over and over again Thisapproach saves time and resources because the macro does not have to be compiled each time it is called To takeadvantage of this time-saving approach you will need to either verify andor turn on the SAS System optionsMSTORED and SASMSTORE You will also need to specify the STORE option of the MACRO statement It isworth mentioning that during macro compilation only macro statements are compiled so be aware that non-macrotext and macro references are not evaluated during the compilation phase ndash but during macro execution

SAS Option DescriptionMSTORED Turns on the Compiled Macro Facility so you can take advantage of this featureSASMSTORE= Specifies the libref associated with the SAS catalog SASMACR This catalog stores compiled

macros

Tip 5 ndash Streamlining Command-line DMS Commands with a Macro

The macro language is a wonderful tool for streamlining frequently entered SAS Display Manager System (DMS)commands to reduce the number of keystrokes By embedding a series of DMS commands inside a simple macroyoursquoll not only save by not having to enter them over and over again but yoursquoll improve your productivity as well Thefollowing macro code illustrates a series of DMS commands being strung together in lieu of entering them individuallyon a Display Manager command line The commands display and expand the SAS Log to full size respectively andthen position the cursor at the top of the log Once the macro is defined it can be called by entering POSTSUBMITon any DMS command line to activate the commands

Macro Code

983117AC983122983119 983152983151983155983156983155983157983138983149983145983156983116983151983143983130983151983151983149

983124983151983152

983117E983118D 983152983151983155983156983155983157983138983149983145983156

Tip 6 ndash Assigning a Defined Macro to a Function Key

To further reduce keystrokes and enhance user productivity even further a call to a defined macro can be saved to aFunction Key The purpose for doing this would be to allow for one-button operation of any defined macro Toillustrate the process of saving a macro call to a Function Key the POSTSUBMIT macro defined in the previous tipis assigned to Function Key F12 in the KEYS window The partial KEYS window is displayed to illustrate the process

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 68

6

KEYS Window

Key Definition

F1 helpF2 reshowF3 end

F10 keysF11 command focusF12 POSTSUBMIT

Tip 7 ndash Defining Positional Parameters

Macros are frequently designed to allow the passing of one or more parameters This allows the creation of macrovariables so text strings can be passed into the macro The order of macro variables as positional parameters isspecified when the macro is coded The assignment of values for each positional parameter is supplied at the timethe macro is called

To illustrate the definition of a two positional parameter macro the following macro was created to display all table

names (data sets) that contain the variable TITLE in the user-assigned MYDATA libref as a cross-reference listingTo retrieve the needed type of information you could execute multiple PROC CONTENTS against selected tables Orin a more efficient method you could retrieve the information directly from the read-only Dictionary table COLUMNSwith the selected columns LIBNAME MEMNAME NAME TYPE and LENGTH as shown For more informationabout Dictionary tables readers may want to view the ldquofreerdquo SAS Press Webinar by Kirk Paul Lafler athttpsupportsascompublishingbbuwebinarhtmllafler2 or the published paper by Kirk Paul Lafler ExploringDictionary Tables and SASHELP Views

Macro Code

983117AC983122983119 C983119983116983125983117983118983123(983116983113983106 983107983119983116983118983105983117983109)983120983122983119C 983123983121983116

983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118A983117E 983124983129983120E 983116E983118G983124983112

F983122983119983117 D983113C983124983113983119983118A983122983129C983119983116983125983117983118983123

983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=amp983116983113983106 A983118D

983125983120CA983123E(983118A983117E)=amp983107983119983116983118983105983117983109 A983118D983125983120CA983123E(983117E983117983124983129983120E)=DA983124A

983121983125983113983124

983117E983118D C983119983116983125983117983118983123

983107983119983116983125983117983118983123(983117983129983108983105983124983105983124983113983124983116983109)983099

After Macro Resolution

983120983122983119C 983123983121983116983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118A983117E 983124983129983120E 983116E983118G983124983112

F983122983119983117 D983113C983124983113983119983118A983122983129C983119983116983125983117983118983123983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=983117983129DA983124A A983118D983125983120CA983123E(983118A983117E)=983124983113983124983116E A983118D983125983120CA983123E(983117E983117983124983129983120E)=DA983124A

983121983125983113983124

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 78

7

Output

Now letrsquos examine another useful macro that is designed with a positional parameter The following macro isdesigned to accept one positional parameter called ampLIB When called it accesses the read-only Dictionary tableTABLES to display each table name and the number of observations in the user-assigned MYDATA libref Thismacro provides a handy way to quickly determine the number of observations in one or all tables in a libref withouthaving to execute multiple PROC CONTENTS by using the stored information in the Dictionary table TABLES

Macro Code

983117AC983122983119 983118983125983117983122983119983127983123(983116983113983106)

983120983122983119C 983123983121983116983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118983119B983123

F983122983119983117 D983113C983124983113983119983118A983122983129983124AB983116E983123

983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=amp983116983113983106 A983118D

983125983120CA983123E(983117E983117983124983129983120E)=DA983124A983121983125983113983124

983117E983118D 983118983125983117983122983119983127983123

983118983125983117983122983119983127983123(983117983129983108983105983124983105)983099

After Macro Resolution

Output

983116983145983138983154983137983154983161 983107983151983148983157983149983150 983107983151983148983157983149983150983118983137983149983141 983117983141983149983138983141983154 983118983137983149983141 983107983151983148983157983149983150 983118983137983149983141 983124983161983152983141 983116983141983150983143983156983144

983117983129983108983105983124983105 983105983107983124983119983122983123 983124983145983156983148983141 983139983144983137983154 30983117983129983108983105983124983105 983117983119983126983113983109983123 983124983145983156983148983141 983139983144983137983154 30

983117983129983108983105983124983105 983120983111983135983117983119983126983113983109983123 983124983145983156983148983141 983139983144983137983154 30

983117983129983108983105983124983105 983120983111983135983122983105983124983109983108983135983117983119983126983113983109983123 983124983145983156983148983141 983139983144983137983154 30983117983129983108983105983124983105 983122983109983118983124983105983116983135983113983118983110983119 983124983145983156983148983141 983139983144983137983154 30

983120983122983119C 983123983121983116

983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118983119B983123

F983122983119983117 D983113C983124983113983119983118A983122983129983124AB983116E983123983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=983117983129DA983124A A983118D

983125983120CA983123E(983117E983117983124983129983120E)=DA983124A983121983125983113983124

983116983145983138983154983137983154983161 983118983157983149983138983141983154 983151983142 983120983144983161983155983145983139983137983148983118983137983149983141 983117983141983149983138983141983154 983118983137983149983141 983119983138983155983141983154983158983137983156983145983151983150983155

983117983129983108983105983124983105 983117983119983126983113983109983123 22983117983129983108983105983124983105 983107983125983123983124983119983117983109983122983123 3983117983129983108983105983124983105 983117983119983126983113983109983123 22

983117983129983108983105983124983105 983120983105983124983113983109983118983124983123 983095983117983129983108983105983124983105 983120983111983135983117983119983126983113983109983123 13983117983129983108983105983124983105 983120983111983135983122983105983124983109983108983135983117983119983126983113983109983123 13

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 88

8

ConclusionThe macro language provides SAS users with a powerful language environment for constructing a library of powerfultools routines and reusable programs It offers a comprehensive set of statements options functions and has its owncompiler Once written and debugged macro programs can be stored in a location on your operating environment that canbe referenced and accessed using an autocall macro environment Macros can also be compiled providing for a moreefficient process for executing macros because the macro does not have to be compiled over and over again Finallyusers are able to design and construct reusable macro tools that can be used again and again

ReferencesBurlew Michele M (1998) SAS Macro Programming Made Easy SAS Institute Inc Cary NC USA

Carpenter Art (2004) Carpenterrsquos Complete Guide to the SAS Macro Language Second Edition SAS Institute IncCary NC USA

Lafler Kirk Paul (2008) ldquoBuilding Reusable SASreg Macro Toolsrdquo Michigan SAS Users Group 2008 Conference

Software Intelligence Corporation Spring Valley CA USA

Lafler Kirk Paul (2007) ldquoSAS Macro Programming Tips and Techniquesrdquo Proceedings of the NorthEast SAS UsersGroup (NESUG) 2007 Conference Software Intelligence Corporation Spring Valley CA USA

Lafler Kirk Paul (2007) SAS System Macro Language Course Notes Fourth Edition Software IntelligenceCorporation Spring Valley CA USA

Lafler Kirk Paul (2006) Exploring DICTIONARY Tables and SASHELP Views Software Intelligence CorporationSpring Valley CA USA

Lafler Kirk Paul (2004) PROC SQL Beyond the Basics Using SAS SAS Institute Inc Cary NC USA

Roberts Clark (1997) ldquoBuilding and Using Macro Variable Listsrdquo Proceedings of the Twenty-second Annual SASUsers Group International Conference San Diego CA 441-443

SAS Macro Language Reference SAS OnlineDocreg 92 SAS Institute Inc Cary NC USA

SAS Macro Language Reference SAS OnlineDocreg 913 SAS Institute Inc Cary NC USA

TrademarksSAS and all other SAS Institute Inc product or service names are registered trademarks or trademarks of SASInstitute Inc and other countries reg indicates USA registration

AcknowledgmentsI would like to thank the NorthEast SAS Users Group (NESUG) Conference Team including Ken Borowiak and Gary

McQuown for inviting me to present this paper and special thanks are also extended to Earl Westerlund and theNESUG 2008 Executive Committee for their support of a great Conference

Author InformationKirk Paul Lafler is consultant and founder of Software Intelligence Corporation and has been programming in SASsince 1979 As a SAS Certified Professional and SAS Institute Alliance Member (1996 ndash 2002) Kirk provides ITconsulting services and training to SAS users around the world As the author of four books including PROC SQLBeyond the Basics Using SAS (SAS Institute 2004) Kirk has written more than two hundred peer-reviewed papersand articles that have appeared in professional journals and SAS User Group proceedings He has also been anInvited speaker at more than two hundred SAS International regional local and special-interest user groupconferences and meetings throughout North America His popular SAS Tips column ldquoKirkrsquos Korner of Quick andSimple Tipsrdquo appears regularly in several SAS User Group newsletters and Web sites and his fun-filled SASwordPuzzles are featured in SAScommunityorg and User Group Newsletters

Comments and suggestions can be sent to

Kirk Paul LaflerSoftware Intelligence Corporation

World HeadquartersPO Box 1390

Spring Valley California 91979-1390E-mail KirkLaflercscom

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 48

4

Tip 2 ndash Using the Autocall Facility to Call a Macro

Macro programs can be stored as SAS programs in a location in your operating environment and called on-demandusing the built-in autocall facility Macro programs stored this way are defined once and referenced (or called)anytime needed This provides an effective way to store and manage your macro programs in a library aggregate Tofacilitate the autocall environment you will need to specify the SAS System options presented in the following table

SAS Option Description

MAUTOSOURCE Turns on the Autocall Facility so stored macro programs are included in the search for macrodefinitions

MRECALL Turns on the capability to search stored macro programs when a macro is not foundSASAUTOS= Specifies the location of the stored macro programs

Tip 3 ndash Accessing the SAS Institute-supplied Autocall Macros

Users may be unaware that SAS Institute has provided as part of your SAS software an autocall library of existingmacros These autocall macros are automatically found in your default SASAUTOS fileref For example the defaultlocation of the SASAUTOS fileref under Windows XP Professional on my computer is cprogram filessassas91coresasmacro Readers are encouraged to refer to the SAS Companion manual for the operating environmentyou are running under for further details

Numerous SAS-supplied autocall macros are included ndash many of which act and behave as macro functions It isworth mentioning that these autocall macros provide a wealth of effective coding techniques and can be useful as ameans of improving macro coding prowess in particular for those users who learn by example The following tabledepicts an alphabetical sampling of the SAS Institute-supplied autocall macros for SAS 91

SASAUTOSMacro Name SASAUTOS Macro DescriptionCHNGCASE This macro is used in the change dialog box for pmenusCMPRES This macro returns the argument passed to it in an unquoted form with multiple blanks

compressed to single blanks and also with leading and trailing blanks removedDATATYP The DATATYP macro determines if the input parameter is NUMERIC or CHARacter data and

returns either CHAR or NUMERIC depending on the value passed through the parameterLEFT This macro returns the argument passed to it without any leading blanks in an unquoted formLOWCASE This macro returns the argument passed to it unchanged except that all upper-case alphabetic

characters are changed to their lower-case equivalentsSYSRC This macro returns a numeric value corresponding to the mnemonic string passed to it and

should only be used to check return code values from SCL functionsTRIM This macro returns the argument passed to it without any trailing blanks in an unquoted formVERIFY This macro returns the position of the first character in the argument that is not in the target

value

To help illustrate a SASAUTOS macro we will display the contents of the TRIM autocall macro below The purposeof the TRIM autocall macro is to remove (or trim) trailing blanks from text and return the result

TRIM AUTOCALL Macro

983149983137983139983154983151 983156983154983145983149(983158983137983148983157983141)

983117AC983122983119 983124983122983113983117

983125983123AGE 1) 983156983154983145983149(983137983154983143983157983149983141983150983156)

DE983123C983122983113983120983124983113983119983118

983124983144983145983155 983149983137983139983154983151 983154983141983156983157983154983150983155 983156983144983141 983137983154983143983157983149983141983150983156 983152983137983155983155983141983140 983156983151 983145983156 983159983145983156983144983151983157983156 983137983150983161 983156983154983137983145983148983145983150983143 983138983148983137983150983147983155 983145983150 983137983150 983157983150983153983157983151983156983141983140 983142983151983154983149 983124983144983141 983155983161983150983156983137983160 983142983151983154 983145983156983155 983157983155983141

983145983155 983155983145983149983145983148983137983154 983156983151 983156983144983137983156 983151983142 983150983137983156983145983158983141 983149983137983139983154983151 983142983157983150983139983156983145983151983150983155

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 58

5

E983143 983148983141983156 983149983137983139983158983137983154=983156983154983145983149(amp983137983154983143983156983141983160983156)

983118983119983124E983123

983118983151983150983141

983148983151983139983137983148 983145

983140983151 983145=983148983141983150983143983156983144(amp983158983137983148983157983141) 983156983151 1 983138983161 9830851983145983142 983153983155983157983138983155983156983154(amp983158983137983148983157983141amp9831451)983134=983155983156983154( ) 983156983144983141983150 983143983151983156983151 983156983154983145983149983149983141983140

983141983150983140

983156983154983145983149983149983141983140 983145983142 amp983145gt0 983156983144983141983150 983155983157983138983155983156983154(amp9831589831379831489831579831411amp983145)

983149983141983150983140

Tip 4 ndash Compiling a Stored Macro with the Compiled Macro Facility

A macro can be compiled once and the compiled version stored so it can be used over and over again Thisapproach saves time and resources because the macro does not have to be compiled each time it is called To takeadvantage of this time-saving approach you will need to either verify andor turn on the SAS System optionsMSTORED and SASMSTORE You will also need to specify the STORE option of the MACRO statement It isworth mentioning that during macro compilation only macro statements are compiled so be aware that non-macrotext and macro references are not evaluated during the compilation phase ndash but during macro execution

SAS Option DescriptionMSTORED Turns on the Compiled Macro Facility so you can take advantage of this featureSASMSTORE= Specifies the libref associated with the SAS catalog SASMACR This catalog stores compiled

macros

Tip 5 ndash Streamlining Command-line DMS Commands with a Macro

The macro language is a wonderful tool for streamlining frequently entered SAS Display Manager System (DMS)commands to reduce the number of keystrokes By embedding a series of DMS commands inside a simple macroyoursquoll not only save by not having to enter them over and over again but yoursquoll improve your productivity as well Thefollowing macro code illustrates a series of DMS commands being strung together in lieu of entering them individuallyon a Display Manager command line The commands display and expand the SAS Log to full size respectively andthen position the cursor at the top of the log Once the macro is defined it can be called by entering POSTSUBMITon any DMS command line to activate the commands

Macro Code

983117AC983122983119 983152983151983155983156983155983157983138983149983145983156983116983151983143983130983151983151983149

983124983151983152

983117E983118D 983152983151983155983156983155983157983138983149983145983156

Tip 6 ndash Assigning a Defined Macro to a Function Key

To further reduce keystrokes and enhance user productivity even further a call to a defined macro can be saved to aFunction Key The purpose for doing this would be to allow for one-button operation of any defined macro Toillustrate the process of saving a macro call to a Function Key the POSTSUBMIT macro defined in the previous tipis assigned to Function Key F12 in the KEYS window The partial KEYS window is displayed to illustrate the process

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 68

6

KEYS Window

Key Definition

F1 helpF2 reshowF3 end

F10 keysF11 command focusF12 POSTSUBMIT

Tip 7 ndash Defining Positional Parameters

Macros are frequently designed to allow the passing of one or more parameters This allows the creation of macrovariables so text strings can be passed into the macro The order of macro variables as positional parameters isspecified when the macro is coded The assignment of values for each positional parameter is supplied at the timethe macro is called

To illustrate the definition of a two positional parameter macro the following macro was created to display all table

names (data sets) that contain the variable TITLE in the user-assigned MYDATA libref as a cross-reference listingTo retrieve the needed type of information you could execute multiple PROC CONTENTS against selected tables Orin a more efficient method you could retrieve the information directly from the read-only Dictionary table COLUMNSwith the selected columns LIBNAME MEMNAME NAME TYPE and LENGTH as shown For more informationabout Dictionary tables readers may want to view the ldquofreerdquo SAS Press Webinar by Kirk Paul Lafler athttpsupportsascompublishingbbuwebinarhtmllafler2 or the published paper by Kirk Paul Lafler ExploringDictionary Tables and SASHELP Views

Macro Code

983117AC983122983119 C983119983116983125983117983118983123(983116983113983106 983107983119983116983118983105983117983109)983120983122983119C 983123983121983116

983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118A983117E 983124983129983120E 983116E983118G983124983112

F983122983119983117 D983113C983124983113983119983118A983122983129C983119983116983125983117983118983123

983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=amp983116983113983106 A983118D

983125983120CA983123E(983118A983117E)=amp983107983119983116983118983105983117983109 A983118D983125983120CA983123E(983117E983117983124983129983120E)=DA983124A

983121983125983113983124

983117E983118D C983119983116983125983117983118983123

983107983119983116983125983117983118983123(983117983129983108983105983124983105983124983113983124983116983109)983099

After Macro Resolution

983120983122983119C 983123983121983116983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118A983117E 983124983129983120E 983116E983118G983124983112

F983122983119983117 D983113C983124983113983119983118A983122983129C983119983116983125983117983118983123983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=983117983129DA983124A A983118D983125983120CA983123E(983118A983117E)=983124983113983124983116E A983118D983125983120CA983123E(983117E983117983124983129983120E)=DA983124A

983121983125983113983124

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 78

7

Output

Now letrsquos examine another useful macro that is designed with a positional parameter The following macro isdesigned to accept one positional parameter called ampLIB When called it accesses the read-only Dictionary tableTABLES to display each table name and the number of observations in the user-assigned MYDATA libref Thismacro provides a handy way to quickly determine the number of observations in one or all tables in a libref withouthaving to execute multiple PROC CONTENTS by using the stored information in the Dictionary table TABLES

Macro Code

983117AC983122983119 983118983125983117983122983119983127983123(983116983113983106)

983120983122983119C 983123983121983116983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118983119B983123

F983122983119983117 D983113C983124983113983119983118A983122983129983124AB983116E983123

983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=amp983116983113983106 A983118D

983125983120CA983123E(983117E983117983124983129983120E)=DA983124A983121983125983113983124

983117E983118D 983118983125983117983122983119983127983123

983118983125983117983122983119983127983123(983117983129983108983105983124983105)983099

After Macro Resolution

Output

983116983145983138983154983137983154983161 983107983151983148983157983149983150 983107983151983148983157983149983150983118983137983149983141 983117983141983149983138983141983154 983118983137983149983141 983107983151983148983157983149983150 983118983137983149983141 983124983161983152983141 983116983141983150983143983156983144

983117983129983108983105983124983105 983105983107983124983119983122983123 983124983145983156983148983141 983139983144983137983154 30983117983129983108983105983124983105 983117983119983126983113983109983123 983124983145983156983148983141 983139983144983137983154 30

983117983129983108983105983124983105 983120983111983135983117983119983126983113983109983123 983124983145983156983148983141 983139983144983137983154 30

983117983129983108983105983124983105 983120983111983135983122983105983124983109983108983135983117983119983126983113983109983123 983124983145983156983148983141 983139983144983137983154 30983117983129983108983105983124983105 983122983109983118983124983105983116983135983113983118983110983119 983124983145983156983148983141 983139983144983137983154 30

983120983122983119C 983123983121983116

983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118983119B983123

F983122983119983117 D983113C983124983113983119983118A983122983129983124AB983116E983123983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=983117983129DA983124A A983118D

983125983120CA983123E(983117E983117983124983129983120E)=DA983124A983121983125983113983124

983116983145983138983154983137983154983161 983118983157983149983138983141983154 983151983142 983120983144983161983155983145983139983137983148983118983137983149983141 983117983141983149983138983141983154 983118983137983149983141 983119983138983155983141983154983158983137983156983145983151983150983155

983117983129983108983105983124983105 983117983119983126983113983109983123 22983117983129983108983105983124983105 983107983125983123983124983119983117983109983122983123 3983117983129983108983105983124983105 983117983119983126983113983109983123 22

983117983129983108983105983124983105 983120983105983124983113983109983118983124983123 983095983117983129983108983105983124983105 983120983111983135983117983119983126983113983109983123 13983117983129983108983105983124983105 983120983111983135983122983105983124983109983108983135983117983119983126983113983109983123 13

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 88

8

ConclusionThe macro language provides SAS users with a powerful language environment for constructing a library of powerfultools routines and reusable programs It offers a comprehensive set of statements options functions and has its owncompiler Once written and debugged macro programs can be stored in a location on your operating environment that canbe referenced and accessed using an autocall macro environment Macros can also be compiled providing for a moreefficient process for executing macros because the macro does not have to be compiled over and over again Finallyusers are able to design and construct reusable macro tools that can be used again and again

ReferencesBurlew Michele M (1998) SAS Macro Programming Made Easy SAS Institute Inc Cary NC USA

Carpenter Art (2004) Carpenterrsquos Complete Guide to the SAS Macro Language Second Edition SAS Institute IncCary NC USA

Lafler Kirk Paul (2008) ldquoBuilding Reusable SASreg Macro Toolsrdquo Michigan SAS Users Group 2008 Conference

Software Intelligence Corporation Spring Valley CA USA

Lafler Kirk Paul (2007) ldquoSAS Macro Programming Tips and Techniquesrdquo Proceedings of the NorthEast SAS UsersGroup (NESUG) 2007 Conference Software Intelligence Corporation Spring Valley CA USA

Lafler Kirk Paul (2007) SAS System Macro Language Course Notes Fourth Edition Software IntelligenceCorporation Spring Valley CA USA

Lafler Kirk Paul (2006) Exploring DICTIONARY Tables and SASHELP Views Software Intelligence CorporationSpring Valley CA USA

Lafler Kirk Paul (2004) PROC SQL Beyond the Basics Using SAS SAS Institute Inc Cary NC USA

Roberts Clark (1997) ldquoBuilding and Using Macro Variable Listsrdquo Proceedings of the Twenty-second Annual SASUsers Group International Conference San Diego CA 441-443

SAS Macro Language Reference SAS OnlineDocreg 92 SAS Institute Inc Cary NC USA

SAS Macro Language Reference SAS OnlineDocreg 913 SAS Institute Inc Cary NC USA

TrademarksSAS and all other SAS Institute Inc product or service names are registered trademarks or trademarks of SASInstitute Inc and other countries reg indicates USA registration

AcknowledgmentsI would like to thank the NorthEast SAS Users Group (NESUG) Conference Team including Ken Borowiak and Gary

McQuown for inviting me to present this paper and special thanks are also extended to Earl Westerlund and theNESUG 2008 Executive Committee for their support of a great Conference

Author InformationKirk Paul Lafler is consultant and founder of Software Intelligence Corporation and has been programming in SASsince 1979 As a SAS Certified Professional and SAS Institute Alliance Member (1996 ndash 2002) Kirk provides ITconsulting services and training to SAS users around the world As the author of four books including PROC SQLBeyond the Basics Using SAS (SAS Institute 2004) Kirk has written more than two hundred peer-reviewed papersand articles that have appeared in professional journals and SAS User Group proceedings He has also been anInvited speaker at more than two hundred SAS International regional local and special-interest user groupconferences and meetings throughout North America His popular SAS Tips column ldquoKirkrsquos Korner of Quick andSimple Tipsrdquo appears regularly in several SAS User Group newsletters and Web sites and his fun-filled SASwordPuzzles are featured in SAScommunityorg and User Group Newsletters

Comments and suggestions can be sent to

Kirk Paul LaflerSoftware Intelligence Corporation

World HeadquartersPO Box 1390

Spring Valley California 91979-1390E-mail KirkLaflercscom

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 58

5

E983143 983148983141983156 983149983137983139983158983137983154=983156983154983145983149(amp983137983154983143983156983141983160983156)

983118983119983124E983123

983118983151983150983141

983148983151983139983137983148 983145

983140983151 983145=983148983141983150983143983156983144(amp983158983137983148983157983141) 983156983151 1 983138983161 9830851983145983142 983153983155983157983138983155983156983154(amp983158983137983148983157983141amp9831451)983134=983155983156983154( ) 983156983144983141983150 983143983151983156983151 983156983154983145983149983149983141983140

983141983150983140

983156983154983145983149983149983141983140 983145983142 amp983145gt0 983156983144983141983150 983155983157983138983155983156983154(amp9831589831379831489831579831411amp983145)

983149983141983150983140

Tip 4 ndash Compiling a Stored Macro with the Compiled Macro Facility

A macro can be compiled once and the compiled version stored so it can be used over and over again Thisapproach saves time and resources because the macro does not have to be compiled each time it is called To takeadvantage of this time-saving approach you will need to either verify andor turn on the SAS System optionsMSTORED and SASMSTORE You will also need to specify the STORE option of the MACRO statement It isworth mentioning that during macro compilation only macro statements are compiled so be aware that non-macrotext and macro references are not evaluated during the compilation phase ndash but during macro execution

SAS Option DescriptionMSTORED Turns on the Compiled Macro Facility so you can take advantage of this featureSASMSTORE= Specifies the libref associated with the SAS catalog SASMACR This catalog stores compiled

macros

Tip 5 ndash Streamlining Command-line DMS Commands with a Macro

The macro language is a wonderful tool for streamlining frequently entered SAS Display Manager System (DMS)commands to reduce the number of keystrokes By embedding a series of DMS commands inside a simple macroyoursquoll not only save by not having to enter them over and over again but yoursquoll improve your productivity as well Thefollowing macro code illustrates a series of DMS commands being strung together in lieu of entering them individuallyon a Display Manager command line The commands display and expand the SAS Log to full size respectively andthen position the cursor at the top of the log Once the macro is defined it can be called by entering POSTSUBMITon any DMS command line to activate the commands

Macro Code

983117AC983122983119 983152983151983155983156983155983157983138983149983145983156983116983151983143983130983151983151983149

983124983151983152

983117E983118D 983152983151983155983156983155983157983138983149983145983156

Tip 6 ndash Assigning a Defined Macro to a Function Key

To further reduce keystrokes and enhance user productivity even further a call to a defined macro can be saved to aFunction Key The purpose for doing this would be to allow for one-button operation of any defined macro Toillustrate the process of saving a macro call to a Function Key the POSTSUBMIT macro defined in the previous tipis assigned to Function Key F12 in the KEYS window The partial KEYS window is displayed to illustrate the process

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 68

6

KEYS Window

Key Definition

F1 helpF2 reshowF3 end

F10 keysF11 command focusF12 POSTSUBMIT

Tip 7 ndash Defining Positional Parameters

Macros are frequently designed to allow the passing of one or more parameters This allows the creation of macrovariables so text strings can be passed into the macro The order of macro variables as positional parameters isspecified when the macro is coded The assignment of values for each positional parameter is supplied at the timethe macro is called

To illustrate the definition of a two positional parameter macro the following macro was created to display all table

names (data sets) that contain the variable TITLE in the user-assigned MYDATA libref as a cross-reference listingTo retrieve the needed type of information you could execute multiple PROC CONTENTS against selected tables Orin a more efficient method you could retrieve the information directly from the read-only Dictionary table COLUMNSwith the selected columns LIBNAME MEMNAME NAME TYPE and LENGTH as shown For more informationabout Dictionary tables readers may want to view the ldquofreerdquo SAS Press Webinar by Kirk Paul Lafler athttpsupportsascompublishingbbuwebinarhtmllafler2 or the published paper by Kirk Paul Lafler ExploringDictionary Tables and SASHELP Views

Macro Code

983117AC983122983119 C983119983116983125983117983118983123(983116983113983106 983107983119983116983118983105983117983109)983120983122983119C 983123983121983116

983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118A983117E 983124983129983120E 983116E983118G983124983112

F983122983119983117 D983113C983124983113983119983118A983122983129C983119983116983125983117983118983123

983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=amp983116983113983106 A983118D

983125983120CA983123E(983118A983117E)=amp983107983119983116983118983105983117983109 A983118D983125983120CA983123E(983117E983117983124983129983120E)=DA983124A

983121983125983113983124

983117E983118D C983119983116983125983117983118983123

983107983119983116983125983117983118983123(983117983129983108983105983124983105983124983113983124983116983109)983099

After Macro Resolution

983120983122983119C 983123983121983116983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118A983117E 983124983129983120E 983116E983118G983124983112

F983122983119983117 D983113C983124983113983119983118A983122983129C983119983116983125983117983118983123983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=983117983129DA983124A A983118D983125983120CA983123E(983118A983117E)=983124983113983124983116E A983118D983125983120CA983123E(983117E983117983124983129983120E)=DA983124A

983121983125983113983124

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 78

7

Output

Now letrsquos examine another useful macro that is designed with a positional parameter The following macro isdesigned to accept one positional parameter called ampLIB When called it accesses the read-only Dictionary tableTABLES to display each table name and the number of observations in the user-assigned MYDATA libref Thismacro provides a handy way to quickly determine the number of observations in one or all tables in a libref withouthaving to execute multiple PROC CONTENTS by using the stored information in the Dictionary table TABLES

Macro Code

983117AC983122983119 983118983125983117983122983119983127983123(983116983113983106)

983120983122983119C 983123983121983116983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118983119B983123

F983122983119983117 D983113C983124983113983119983118A983122983129983124AB983116E983123

983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=amp983116983113983106 A983118D

983125983120CA983123E(983117E983117983124983129983120E)=DA983124A983121983125983113983124

983117E983118D 983118983125983117983122983119983127983123

983118983125983117983122983119983127983123(983117983129983108983105983124983105)983099

After Macro Resolution

Output

983116983145983138983154983137983154983161 983107983151983148983157983149983150 983107983151983148983157983149983150983118983137983149983141 983117983141983149983138983141983154 983118983137983149983141 983107983151983148983157983149983150 983118983137983149983141 983124983161983152983141 983116983141983150983143983156983144

983117983129983108983105983124983105 983105983107983124983119983122983123 983124983145983156983148983141 983139983144983137983154 30983117983129983108983105983124983105 983117983119983126983113983109983123 983124983145983156983148983141 983139983144983137983154 30

983117983129983108983105983124983105 983120983111983135983117983119983126983113983109983123 983124983145983156983148983141 983139983144983137983154 30

983117983129983108983105983124983105 983120983111983135983122983105983124983109983108983135983117983119983126983113983109983123 983124983145983156983148983141 983139983144983137983154 30983117983129983108983105983124983105 983122983109983118983124983105983116983135983113983118983110983119 983124983145983156983148983141 983139983144983137983154 30

983120983122983119C 983123983121983116

983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118983119B983123

F983122983119983117 D983113C983124983113983119983118A983122983129983124AB983116E983123983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=983117983129DA983124A A983118D

983125983120CA983123E(983117E983117983124983129983120E)=DA983124A983121983125983113983124

983116983145983138983154983137983154983161 983118983157983149983138983141983154 983151983142 983120983144983161983155983145983139983137983148983118983137983149983141 983117983141983149983138983141983154 983118983137983149983141 983119983138983155983141983154983158983137983156983145983151983150983155

983117983129983108983105983124983105 983117983119983126983113983109983123 22983117983129983108983105983124983105 983107983125983123983124983119983117983109983122983123 3983117983129983108983105983124983105 983117983119983126983113983109983123 22

983117983129983108983105983124983105 983120983105983124983113983109983118983124983123 983095983117983129983108983105983124983105 983120983111983135983117983119983126983113983109983123 13983117983129983108983105983124983105 983120983111983135983122983105983124983109983108983135983117983119983126983113983109983123 13

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 88

8

ConclusionThe macro language provides SAS users with a powerful language environment for constructing a library of powerfultools routines and reusable programs It offers a comprehensive set of statements options functions and has its owncompiler Once written and debugged macro programs can be stored in a location on your operating environment that canbe referenced and accessed using an autocall macro environment Macros can also be compiled providing for a moreefficient process for executing macros because the macro does not have to be compiled over and over again Finallyusers are able to design and construct reusable macro tools that can be used again and again

ReferencesBurlew Michele M (1998) SAS Macro Programming Made Easy SAS Institute Inc Cary NC USA

Carpenter Art (2004) Carpenterrsquos Complete Guide to the SAS Macro Language Second Edition SAS Institute IncCary NC USA

Lafler Kirk Paul (2008) ldquoBuilding Reusable SASreg Macro Toolsrdquo Michigan SAS Users Group 2008 Conference

Software Intelligence Corporation Spring Valley CA USA

Lafler Kirk Paul (2007) ldquoSAS Macro Programming Tips and Techniquesrdquo Proceedings of the NorthEast SAS UsersGroup (NESUG) 2007 Conference Software Intelligence Corporation Spring Valley CA USA

Lafler Kirk Paul (2007) SAS System Macro Language Course Notes Fourth Edition Software IntelligenceCorporation Spring Valley CA USA

Lafler Kirk Paul (2006) Exploring DICTIONARY Tables and SASHELP Views Software Intelligence CorporationSpring Valley CA USA

Lafler Kirk Paul (2004) PROC SQL Beyond the Basics Using SAS SAS Institute Inc Cary NC USA

Roberts Clark (1997) ldquoBuilding and Using Macro Variable Listsrdquo Proceedings of the Twenty-second Annual SASUsers Group International Conference San Diego CA 441-443

SAS Macro Language Reference SAS OnlineDocreg 92 SAS Institute Inc Cary NC USA

SAS Macro Language Reference SAS OnlineDocreg 913 SAS Institute Inc Cary NC USA

TrademarksSAS and all other SAS Institute Inc product or service names are registered trademarks or trademarks of SASInstitute Inc and other countries reg indicates USA registration

AcknowledgmentsI would like to thank the NorthEast SAS Users Group (NESUG) Conference Team including Ken Borowiak and Gary

McQuown for inviting me to present this paper and special thanks are also extended to Earl Westerlund and theNESUG 2008 Executive Committee for their support of a great Conference

Author InformationKirk Paul Lafler is consultant and founder of Software Intelligence Corporation and has been programming in SASsince 1979 As a SAS Certified Professional and SAS Institute Alliance Member (1996 ndash 2002) Kirk provides ITconsulting services and training to SAS users around the world As the author of four books including PROC SQLBeyond the Basics Using SAS (SAS Institute 2004) Kirk has written more than two hundred peer-reviewed papersand articles that have appeared in professional journals and SAS User Group proceedings He has also been anInvited speaker at more than two hundred SAS International regional local and special-interest user groupconferences and meetings throughout North America His popular SAS Tips column ldquoKirkrsquos Korner of Quick andSimple Tipsrdquo appears regularly in several SAS User Group newsletters and Web sites and his fun-filled SASwordPuzzles are featured in SAScommunityorg and User Group Newsletters

Comments and suggestions can be sent to

Kirk Paul LaflerSoftware Intelligence Corporation

World HeadquartersPO Box 1390

Spring Valley California 91979-1390E-mail KirkLaflercscom

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 68

6

KEYS Window

Key Definition

F1 helpF2 reshowF3 end

F10 keysF11 command focusF12 POSTSUBMIT

Tip 7 ndash Defining Positional Parameters

Macros are frequently designed to allow the passing of one or more parameters This allows the creation of macrovariables so text strings can be passed into the macro The order of macro variables as positional parameters isspecified when the macro is coded The assignment of values for each positional parameter is supplied at the timethe macro is called

To illustrate the definition of a two positional parameter macro the following macro was created to display all table

names (data sets) that contain the variable TITLE in the user-assigned MYDATA libref as a cross-reference listingTo retrieve the needed type of information you could execute multiple PROC CONTENTS against selected tables Orin a more efficient method you could retrieve the information directly from the read-only Dictionary table COLUMNSwith the selected columns LIBNAME MEMNAME NAME TYPE and LENGTH as shown For more informationabout Dictionary tables readers may want to view the ldquofreerdquo SAS Press Webinar by Kirk Paul Lafler athttpsupportsascompublishingbbuwebinarhtmllafler2 or the published paper by Kirk Paul Lafler ExploringDictionary Tables and SASHELP Views

Macro Code

983117AC983122983119 C983119983116983125983117983118983123(983116983113983106 983107983119983116983118983105983117983109)983120983122983119C 983123983121983116

983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118A983117E 983124983129983120E 983116E983118G983124983112

F983122983119983117 D983113C983124983113983119983118A983122983129C983119983116983125983117983118983123

983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=amp983116983113983106 A983118D

983125983120CA983123E(983118A983117E)=amp983107983119983116983118983105983117983109 A983118D983125983120CA983123E(983117E983117983124983129983120E)=DA983124A

983121983125983113983124

983117E983118D C983119983116983125983117983118983123

983107983119983116983125983117983118983123(983117983129983108983105983124983105983124983113983124983116983109)983099

After Macro Resolution

983120983122983119C 983123983121983116983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118A983117E 983124983129983120E 983116E983118G983124983112

F983122983119983117 D983113C983124983113983119983118A983122983129C983119983116983125983117983118983123983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=983117983129DA983124A A983118D983125983120CA983123E(983118A983117E)=983124983113983124983116E A983118D983125983120CA983123E(983117E983117983124983129983120E)=DA983124A

983121983125983113983124

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 78

7

Output

Now letrsquos examine another useful macro that is designed with a positional parameter The following macro isdesigned to accept one positional parameter called ampLIB When called it accesses the read-only Dictionary tableTABLES to display each table name and the number of observations in the user-assigned MYDATA libref Thismacro provides a handy way to quickly determine the number of observations in one or all tables in a libref withouthaving to execute multiple PROC CONTENTS by using the stored information in the Dictionary table TABLES

Macro Code

983117AC983122983119 983118983125983117983122983119983127983123(983116983113983106)

983120983122983119C 983123983121983116983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118983119B983123

F983122983119983117 D983113C983124983113983119983118A983122983129983124AB983116E983123

983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=amp983116983113983106 A983118D

983125983120CA983123E(983117E983117983124983129983120E)=DA983124A983121983125983113983124

983117E983118D 983118983125983117983122983119983127983123

983118983125983117983122983119983127983123(983117983129983108983105983124983105)983099

After Macro Resolution

Output

983116983145983138983154983137983154983161 983107983151983148983157983149983150 983107983151983148983157983149983150983118983137983149983141 983117983141983149983138983141983154 983118983137983149983141 983107983151983148983157983149983150 983118983137983149983141 983124983161983152983141 983116983141983150983143983156983144

983117983129983108983105983124983105 983105983107983124983119983122983123 983124983145983156983148983141 983139983144983137983154 30983117983129983108983105983124983105 983117983119983126983113983109983123 983124983145983156983148983141 983139983144983137983154 30

983117983129983108983105983124983105 983120983111983135983117983119983126983113983109983123 983124983145983156983148983141 983139983144983137983154 30

983117983129983108983105983124983105 983120983111983135983122983105983124983109983108983135983117983119983126983113983109983123 983124983145983156983148983141 983139983144983137983154 30983117983129983108983105983124983105 983122983109983118983124983105983116983135983113983118983110983119 983124983145983156983148983141 983139983144983137983154 30

983120983122983119C 983123983121983116

983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118983119B983123

F983122983119983117 D983113C983124983113983119983118A983122983129983124AB983116E983123983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=983117983129DA983124A A983118D

983125983120CA983123E(983117E983117983124983129983120E)=DA983124A983121983125983113983124

983116983145983138983154983137983154983161 983118983157983149983138983141983154 983151983142 983120983144983161983155983145983139983137983148983118983137983149983141 983117983141983149983138983141983154 983118983137983149983141 983119983138983155983141983154983158983137983156983145983151983150983155

983117983129983108983105983124983105 983117983119983126983113983109983123 22983117983129983108983105983124983105 983107983125983123983124983119983117983109983122983123 3983117983129983108983105983124983105 983117983119983126983113983109983123 22

983117983129983108983105983124983105 983120983105983124983113983109983118983124983123 983095983117983129983108983105983124983105 983120983111983135983117983119983126983113983109983123 13983117983129983108983105983124983105 983120983111983135983122983105983124983109983108983135983117983119983126983113983109983123 13

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 88

8

ConclusionThe macro language provides SAS users with a powerful language environment for constructing a library of powerfultools routines and reusable programs It offers a comprehensive set of statements options functions and has its owncompiler Once written and debugged macro programs can be stored in a location on your operating environment that canbe referenced and accessed using an autocall macro environment Macros can also be compiled providing for a moreefficient process for executing macros because the macro does not have to be compiled over and over again Finallyusers are able to design and construct reusable macro tools that can be used again and again

ReferencesBurlew Michele M (1998) SAS Macro Programming Made Easy SAS Institute Inc Cary NC USA

Carpenter Art (2004) Carpenterrsquos Complete Guide to the SAS Macro Language Second Edition SAS Institute IncCary NC USA

Lafler Kirk Paul (2008) ldquoBuilding Reusable SASreg Macro Toolsrdquo Michigan SAS Users Group 2008 Conference

Software Intelligence Corporation Spring Valley CA USA

Lafler Kirk Paul (2007) ldquoSAS Macro Programming Tips and Techniquesrdquo Proceedings of the NorthEast SAS UsersGroup (NESUG) 2007 Conference Software Intelligence Corporation Spring Valley CA USA

Lafler Kirk Paul (2007) SAS System Macro Language Course Notes Fourth Edition Software IntelligenceCorporation Spring Valley CA USA

Lafler Kirk Paul (2006) Exploring DICTIONARY Tables and SASHELP Views Software Intelligence CorporationSpring Valley CA USA

Lafler Kirk Paul (2004) PROC SQL Beyond the Basics Using SAS SAS Institute Inc Cary NC USA

Roberts Clark (1997) ldquoBuilding and Using Macro Variable Listsrdquo Proceedings of the Twenty-second Annual SASUsers Group International Conference San Diego CA 441-443

SAS Macro Language Reference SAS OnlineDocreg 92 SAS Institute Inc Cary NC USA

SAS Macro Language Reference SAS OnlineDocreg 913 SAS Institute Inc Cary NC USA

TrademarksSAS and all other SAS Institute Inc product or service names are registered trademarks or trademarks of SASInstitute Inc and other countries reg indicates USA registration

AcknowledgmentsI would like to thank the NorthEast SAS Users Group (NESUG) Conference Team including Ken Borowiak and Gary

McQuown for inviting me to present this paper and special thanks are also extended to Earl Westerlund and theNESUG 2008 Executive Committee for their support of a great Conference

Author InformationKirk Paul Lafler is consultant and founder of Software Intelligence Corporation and has been programming in SASsince 1979 As a SAS Certified Professional and SAS Institute Alliance Member (1996 ndash 2002) Kirk provides ITconsulting services and training to SAS users around the world As the author of four books including PROC SQLBeyond the Basics Using SAS (SAS Institute 2004) Kirk has written more than two hundred peer-reviewed papersand articles that have appeared in professional journals and SAS User Group proceedings He has also been anInvited speaker at more than two hundred SAS International regional local and special-interest user groupconferences and meetings throughout North America His popular SAS Tips column ldquoKirkrsquos Korner of Quick andSimple Tipsrdquo appears regularly in several SAS User Group newsletters and Web sites and his fun-filled SASwordPuzzles are featured in SAScommunityorg and User Group Newsletters

Comments and suggestions can be sent to

Kirk Paul LaflerSoftware Intelligence Corporation

World HeadquartersPO Box 1390

Spring Valley California 91979-1390E-mail KirkLaflercscom

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 78

7

Output

Now letrsquos examine another useful macro that is designed with a positional parameter The following macro isdesigned to accept one positional parameter called ampLIB When called it accesses the read-only Dictionary tableTABLES to display each table name and the number of observations in the user-assigned MYDATA libref Thismacro provides a handy way to quickly determine the number of observations in one or all tables in a libref withouthaving to execute multiple PROC CONTENTS by using the stored information in the Dictionary table TABLES

Macro Code

983117AC983122983119 983118983125983117983122983119983127983123(983116983113983106)

983120983122983119C 983123983121983116983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118983119B983123

F983122983119983117 D983113C983124983113983119983118A983122983129983124AB983116E983123

983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=amp983116983113983106 A983118D

983125983120CA983123E(983117E983117983124983129983120E)=DA983124A983121983125983113983124

983117E983118D 983118983125983117983122983119983127983123

983118983125983117983122983119983127983123(983117983129983108983105983124983105)983099

After Macro Resolution

Output

983116983145983138983154983137983154983161 983107983151983148983157983149983150 983107983151983148983157983149983150983118983137983149983141 983117983141983149983138983141983154 983118983137983149983141 983107983151983148983157983149983150 983118983137983149983141 983124983161983152983141 983116983141983150983143983156983144

983117983129983108983105983124983105 983105983107983124983119983122983123 983124983145983156983148983141 983139983144983137983154 30983117983129983108983105983124983105 983117983119983126983113983109983123 983124983145983156983148983141 983139983144983137983154 30

983117983129983108983105983124983105 983120983111983135983117983119983126983113983109983123 983124983145983156983148983141 983139983144983137983154 30

983117983129983108983105983124983105 983120983111983135983122983105983124983109983108983135983117983119983126983113983109983123 983124983145983156983148983141 983139983144983137983154 30983117983129983108983105983124983105 983122983109983118983124983105983116983135983113983118983110983119 983124983145983156983148983141 983139983144983137983154 30

983120983122983119C 983123983121983116

983123E983116EC983124 983116983113B983118A983117E 983117E983117983118A983117E 983118983119B983123

F983122983119983117 D983113C983124983113983119983118A983122983129983124AB983116E983123983127983112E983122E 983125983120CA983123E(983116983113B983118A983117E)=983117983129DA983124A A983118D

983125983120CA983123E(983117E983117983124983129983120E)=DA983124A983121983125983113983124

983116983145983138983154983137983154983161 983118983157983149983138983141983154 983151983142 983120983144983161983155983145983139983137983148983118983137983149983141 983117983141983149983138983141983154 983118983137983149983141 983119983138983155983141983154983158983137983156983145983151983150983155

983117983129983108983105983124983105 983117983119983126983113983109983123 22983117983129983108983105983124983105 983107983125983123983124983119983117983109983122983123 3983117983129983108983105983124983105 983117983119983126983113983109983123 22

983117983129983108983105983124983105 983120983105983124983113983109983118983124983123 983095983117983129983108983105983124983105 983120983111983135983117983119983126983113983109983123 13983117983129983108983105983124983105 983120983111983135983122983105983124983109983108983135983117983119983126983113983109983123 13

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 88

8

ConclusionThe macro language provides SAS users with a powerful language environment for constructing a library of powerfultools routines and reusable programs It offers a comprehensive set of statements options functions and has its owncompiler Once written and debugged macro programs can be stored in a location on your operating environment that canbe referenced and accessed using an autocall macro environment Macros can also be compiled providing for a moreefficient process for executing macros because the macro does not have to be compiled over and over again Finallyusers are able to design and construct reusable macro tools that can be used again and again

ReferencesBurlew Michele M (1998) SAS Macro Programming Made Easy SAS Institute Inc Cary NC USA

Carpenter Art (2004) Carpenterrsquos Complete Guide to the SAS Macro Language Second Edition SAS Institute IncCary NC USA

Lafler Kirk Paul (2008) ldquoBuilding Reusable SASreg Macro Toolsrdquo Michigan SAS Users Group 2008 Conference

Software Intelligence Corporation Spring Valley CA USA

Lafler Kirk Paul (2007) ldquoSAS Macro Programming Tips and Techniquesrdquo Proceedings of the NorthEast SAS UsersGroup (NESUG) 2007 Conference Software Intelligence Corporation Spring Valley CA USA

Lafler Kirk Paul (2007) SAS System Macro Language Course Notes Fourth Edition Software IntelligenceCorporation Spring Valley CA USA

Lafler Kirk Paul (2006) Exploring DICTIONARY Tables and SASHELP Views Software Intelligence CorporationSpring Valley CA USA

Lafler Kirk Paul (2004) PROC SQL Beyond the Basics Using SAS SAS Institute Inc Cary NC USA

Roberts Clark (1997) ldquoBuilding and Using Macro Variable Listsrdquo Proceedings of the Twenty-second Annual SASUsers Group International Conference San Diego CA 441-443

SAS Macro Language Reference SAS OnlineDocreg 92 SAS Institute Inc Cary NC USA

SAS Macro Language Reference SAS OnlineDocreg 913 SAS Institute Inc Cary NC USA

TrademarksSAS and all other SAS Institute Inc product or service names are registered trademarks or trademarks of SASInstitute Inc and other countries reg indicates USA registration

AcknowledgmentsI would like to thank the NorthEast SAS Users Group (NESUG) Conference Team including Ken Borowiak and Gary

McQuown for inviting me to present this paper and special thanks are also extended to Earl Westerlund and theNESUG 2008 Executive Committee for their support of a great Conference

Author InformationKirk Paul Lafler is consultant and founder of Software Intelligence Corporation and has been programming in SASsince 1979 As a SAS Certified Professional and SAS Institute Alliance Member (1996 ndash 2002) Kirk provides ITconsulting services and training to SAS users around the world As the author of four books including PROC SQLBeyond the Basics Using SAS (SAS Institute 2004) Kirk has written more than two hundred peer-reviewed papersand articles that have appeared in professional journals and SAS User Group proceedings He has also been anInvited speaker at more than two hundred SAS International regional local and special-interest user groupconferences and meetings throughout North America His popular SAS Tips column ldquoKirkrsquos Korner of Quick andSimple Tipsrdquo appears regularly in several SAS User Group newsletters and Web sites and his fun-filled SASwordPuzzles are featured in SAScommunityorg and User Group Newsletters

Comments and suggestions can be sent to

Kirk Paul LaflerSoftware Intelligence Corporation

World HeadquartersPO Box 1390

Spring Valley California 91979-1390E-mail KirkLaflercscom

Foundations amp FundameESUG 2008

8112019 SASreg Macro Programming Tips and Techniques

httpslidepdfcomreaderfullsas-macro-programming-tips-and-techniques 88

8

ConclusionThe macro language provides SAS users with a powerful language environment for constructing a library of powerfultools routines and reusable programs It offers a comprehensive set of statements options functions and has its owncompiler Once written and debugged macro programs can be stored in a location on your operating environment that canbe referenced and accessed using an autocall macro environment Macros can also be compiled providing for a moreefficient process for executing macros because the macro does not have to be compiled over and over again Finallyusers are able to design and construct reusable macro tools that can be used again and again

ReferencesBurlew Michele M (1998) SAS Macro Programming Made Easy SAS Institute Inc Cary NC USA

Carpenter Art (2004) Carpenterrsquos Complete Guide to the SAS Macro Language Second Edition SAS Institute IncCary NC USA

Lafler Kirk Paul (2008) ldquoBuilding Reusable SASreg Macro Toolsrdquo Michigan SAS Users Group 2008 Conference

Software Intelligence Corporation Spring Valley CA USA

Lafler Kirk Paul (2007) ldquoSAS Macro Programming Tips and Techniquesrdquo Proceedings of the NorthEast SAS UsersGroup (NESUG) 2007 Conference Software Intelligence Corporation Spring Valley CA USA

Lafler Kirk Paul (2007) SAS System Macro Language Course Notes Fourth Edition Software IntelligenceCorporation Spring Valley CA USA

Lafler Kirk Paul (2006) Exploring DICTIONARY Tables and SASHELP Views Software Intelligence CorporationSpring Valley CA USA

Lafler Kirk Paul (2004) PROC SQL Beyond the Basics Using SAS SAS Institute Inc Cary NC USA

Roberts Clark (1997) ldquoBuilding and Using Macro Variable Listsrdquo Proceedings of the Twenty-second Annual SASUsers Group International Conference San Diego CA 441-443

SAS Macro Language Reference SAS OnlineDocreg 92 SAS Institute Inc Cary NC USA

SAS Macro Language Reference SAS OnlineDocreg 913 SAS Institute Inc Cary NC USA

TrademarksSAS and all other SAS Institute Inc product or service names are registered trademarks or trademarks of SASInstitute Inc and other countries reg indicates USA registration

AcknowledgmentsI would like to thank the NorthEast SAS Users Group (NESUG) Conference Team including Ken Borowiak and Gary

McQuown for inviting me to present this paper and special thanks are also extended to Earl Westerlund and theNESUG 2008 Executive Committee for their support of a great Conference

Author InformationKirk Paul Lafler is consultant and founder of Software Intelligence Corporation and has been programming in SASsince 1979 As a SAS Certified Professional and SAS Institute Alliance Member (1996 ndash 2002) Kirk provides ITconsulting services and training to SAS users around the world As the author of four books including PROC SQLBeyond the Basics Using SAS (SAS Institute 2004) Kirk has written more than two hundred peer-reviewed papersand articles that have appeared in professional journals and SAS User Group proceedings He has also been anInvited speaker at more than two hundred SAS International regional local and special-interest user groupconferences and meetings throughout North America His popular SAS Tips column ldquoKirkrsquos Korner of Quick andSimple Tipsrdquo appears regularly in several SAS User Group newsletters and Web sites and his fun-filled SASwordPuzzles are featured in SAScommunityorg and User Group Newsletters

Comments and suggestions can be sent to

Kirk Paul LaflerSoftware Intelligence Corporation

World HeadquartersPO Box 1390

Spring Valley California 91979-1390E-mail KirkLaflercscom

Foundations amp FundameESUG 2008