You are previewing Carpenter's Complete Guide to the SAS Macro Language, Third Edition, 3rd Edition.
O'Reilly logo
Carpenter's Complete Guide to the SAS Macro Language, Third Edition, 3rd Edition

Book Description

For SAS programmers or analysts who need to generalize their programs or improve programming efficiency, Art Carpenter thoroughly updates his highly successful second edition of Carpenter's Complete Guide to the SAS Macro Language with an extensive collection of new macro language techniques and examples.

Addressing the composition and operation of the SAS macro facility and the SAS macro language, this third edition offers nearly 400 ready-to-use macros, macro functions, and macro tools that enable you to convert SAS code to macros, define macro variables, and more! Users with a basic understanding of Base SAS who are new to the SAS macro language will find more detail, utilities, and references to additional learning opportunities; advanced macro language programmers who need help with data-driven macros and dynamic application development will find greatly expanded treatment of these topics.

This revised and enlarged edition includes the following topics:

  • New and expanded introduction to the macro language
  • Functions, automatic macro variables, and macro statements new to the macro language
  • Expanded macro language tools that interface with the operating system
  • Expanded data-driven methodologies used to build dynamic applications
  • Expanded discussion of list processing, with four alternative approaches presented
  • Additional file and data management examples
  • Expanded discussion of CALL EXECUTE and DOSUBL
  • New discussion of using the macro language on remote servers
  • Expanded discussion and examples of macro quoting

Far beyond a reference manual issued from an “ivory tower,” this book is pragmatic and example-driven: Yes, you will find syntax examples; yes, the code is explained. But the focus of this book is on actual code used to solve real-world business problems. In fact, an entire appendix is dedicated to listing the nearly 70 classes of problems that are solved by programs covered in this edition. Discussion of the examples elucidates the pros and cons of the particular solution and often suggests alternative approaches. Therefore, this book provides you both a compendium of reusable and adaptable code, and opportunities for deepening your understanding and growing as a SAS programmer.

Table of Contents

  1. Acknowledgments
  2. About This Book
  3. About The Author
  4. Part 1: Macro Basics
  5. Chapter 1: What the Language Is, What It Does, and What It Can Do
  6. 1.1 Introduction
  7. 1.2 Stages of Macro Language Learning
    1. 1.2.1 Code Substitution
    2. 1.2.2 Macro Language Elements
    3. 1.2.3 Dynamic Programming
  8. 1.3 Terminology
  9. 1.4 Sequencing Events—It’s All about the Timing
  10. 1.5 Scopes or Referencing Environments
    1. 1.5.1 Use of Symbol Tables
    2. 1.5.2 Nested Symbol Tables
  11. Chapter 2: Defining and Using Macro Variables
  12. 2.1 Naming Macro Variables
  13. 2.2 Defining Macro Variables
  14. 2.3 Using Macro Variables
  15. 2.4 Displaying Macro Variables by Using the %PUT Statement
  16. 2.5 Resolving Macro Variables
    1. 2.5.1 Using the Macro Variable as a Suffix
    2. 2.5.2 Using the Macro Variable as a Prefix
    3. 2.5.3 Using Macro Variables as Building Blocks—Appending Macro Variables
    4. 2.5.4 Understanding Results When Macro References Are Not Resolved
  17. 2.6 Using Automatic Macro Variables
    1. 2.6.1 &SYSDATE, &SYSDATE9, &SYSDAY, and &SYSTIME
    2. 2.6.2 &SYSLAST and &SYSDSN
    3. 2.6.3 &SYSERR and &SYSCC
    4. 2.6.4 &SYSRC
    5. 2.6.5 &SYSSITE, &SYSSCP, &SYSSCPL, and &SYSUSERID
    6. 2.6.6 &SYSMACRONAME
  18. 2.7 Removing Macro Variables
  19. 2.8 Testing Your Knowledge with Chapter Exercises
  20. Chapter 3: Defining and Using Macros
  21. 3.1 Creating a Macro
    1. 3.1.1 Defining a Macro
    2. 3.1.2 Commenting a Block of Code with Use of %MACRO and %MEND
    3. 3.1.3 Using the /DES Macro Statement Option
  22. 3.2 Invoking a Macro
  23. 3.3 Using System Options with the Macro Facility
    1. 3.3.1 General Macro Options
    2. 3.3.2 Debugging Options
    3. 3.3.3 Use of the Debugging Options
    4. 3.3.4 Autocall Facility Options
  24. 3.4 Testing Your Knowledge with Chapter Exercises
  25. Chapter 4: Using Macro Parameters
  26. 4.1 Introducing Macro Parameters
  27. 4.2 Using Positional Parameters
    1. 4.2.1 Defining the Macro’s Parameters
    2. 4.2.2 Passing Parameter Values into the Macro
  28. 4.3 Using Keyword Parameters
    1. 4.3.1 Defining the Parameters and Their Default Values
    2. 4.3.2 Passing Parameter Values When Calling the Macro
    3. 4.3.3 Documenting Your Macro
  29. 4.4 Choosing between Keyword and Positional Parameters
    1. 4.4.1 Selecting Parameter Types
    2. 4.4.2 Using Keyword and Positional Parameters Together
    3. 4.4.3 Naming Keyword Parameters without the Equal Sign
  30. 4.5 Testing Your Knowledge with Chapter Exercises
  31. Part 2: Using Macros
  32. Chapter 5: Controlling Programs with Macros
  33. 5.1 Macros That Invoke Macros
    1. 5.1.1 Passing Parameters between Macros
    2. 5.1.2 Passing Parameters When Macros Call Macros
    3. 5.1.3 Passing Macro Parameters through Macro Calls—An Illustrated Example
    4. 5.1.4 Controlling Macro Calls
    5. 5.1.5 Nesting Macro Definitions
  34. 5.2 Conditional Execution Using %IF-%THEN/%ELSE Statements
    1. 5.2.1 Executing Macro Statements
    2. 5.2.2 Building SAS Code Dynamically
    3. 5.2.3 Using the IN Comparison Operator
  35. 5.3 Iterative Execution of Macro Statements
    1. 5.3.1 %DO Block
    2. 5.3.2 Iterative %DO Loops
    3. 5.3.3 %DO %UNTIL Loops
    4. 5.3.4 %DO %WHILE Loops
  36. 5.4 Additional Macro Program Statements
    1. 5.4.1 Macro Comments
    2. 5.4.2 %GLOBAL and %LOCAL
    3. 5.4.3 %SYSEXEC
    4. 5.4.4 Termination of Macro Execution with %ABORT
    5. 5.4.5 Normal Termination of Macro Execution with %RETURN
  37. 5.5 Testing Your Knowledge with Chapter Exercises
  38. Chapter 6: Interfacing with Data Set Values
  39. 6.1 Using the SYMPUTX Routine to Create Macro Variables
    1. 6.1.1 Introducing SYMPUTX Syntax
    2. 6.1.2 Comparing SYMPUTX with SYMPUT
    3. 6.1.3 Using a Macro Variable in the Same Step That Created It
    4. 6.1.4 Building a List of Macro Variables
  40. 6.2 Defining Macro Variables in a PROC SQL Step
    1. 6.2.1 Placing a Single Value into a Single Macro Variable
    2. 6.2.2 Building a List of Values
    3. 6.2.3 Placing a List of Values into a Series of Macro Variables
    4. 6.2.4 Understanding Automatic SQL-Generated Macro Variables
  41. 6.3 Moving Text from Macro Variables into Code
    1. 6.3.1 Assignment and RETAIN Statements
    2. 6.3.2 SYMGET and SYMGETN Functions
    3. 6.3.3 The RESOLVE Function
    4. 6.3.4 Comparison of the SYMGET and RESOLVE Functions
    5. 6.3.5 Less-Than-Optimal Uses of SYMGET and RESOLVE
  42. 6.4 Using Data to Control Program Flow
    1. 6.4.1 Assigning Macro Variable Values
    2. 6.4.2 Assigning Macro Variable Names as well as Values
  43. 6.5 Executing Macro Code Using CALL EXECUTE
    1. 6.5.1 Executing Non-Macro Code
    2. 6.5.2 Executing Macro Code
    3. 6.5.3 Addressing Timing Issues
  44. 6.6 Testing Your Knowledge with Chapter Exercises
  45. Chapter 7: Using Macro Functions
  46. 7.1 Quoting Functions
    1. 7.1.1 Using the %BQUOTE Function
    2. 7.1.2 %STR
    3. 7.1.3 Considerations When Quoting
    4. 7.1.4 Basic Types of Quoting Functions and Why We Care
    5. 7.1.5 A Bit about the %QUOTE and %NRQUOTE Functions
    6. 7.1.6 Removing Masking Characters
    7. 7.1.7 The %SUPERQ Quoting Function
    8. 7.1.8 Quoting Function Summary
    9. 7.1.9 Quoting Mismatched Symbols with the %STR and %QUOTE Functions
  47. 7.2 Text Functions
    1. 7.2.1 %INDEX
    2. 7.2.2 %LENGTH
    3. 7.2.3 %SCAN and %QSCAN
    4. 7.2.4 %SUBSTR and %QSUBSTR
    5. 7.2.5 %UPCASE and %QUPCASE
    6. 7.2.6 %LEFT and %QLEFT
    7. 7.2.7 %LOWCASE and %QLOWCASE
    8. 7.2.8 %TRIM and %QTRIM
  48. 7.3 Evaluation Functions
    1. 7.3.1 Explicit Use of %EVAL
    2. 7.3.2 Implicit Use of %EVAL
    3. 7.3.3 Using %SYSEVALF
  49. 7.4 Using DATA Step Functions and Routines
    1. 7.4.1 Using %SYSCALL
    2. 7.4.2 Using %SYSFUNC and %QSYSFUNC
    3. 7.4.3 Taking Advantage of Less Commonly Used DATA Step Functions
  50. 7.5 Building Your Own Macro Functions
    1. 7.5.1 Introduction
    2. 7.5.2 Building the Function
    3. 7.5.3 Using the Function
    4. 7.5.4 Returning a Value
  51. 7.6 Other Useful User-Written Macro Functions
    1. 7.6.1 One-Liners
    2. 7.6.2 Macro Functions with Logic
    3. 7.6.3 Functions for the DATA Step
  52. 7.7 Testing Your Knowledge with Chapter Exercises
  53. Chapter 8: Discovering Even More Macro Language Elements
  54. 8.1 Even More Macro Functions
    1. 8.1.1 Accessing System Environmental Variables Using %SYSGET
    2. 8.1.2 %SYSMEXECDEPTH and %SYSMEXECNAME
    3. 8.1.3 Assessing Macro Existence and Execution Status with %SYSMACEXEC and %SYSMACEXIST
    4. 8.1.4 Determining Product Availability Using %SYSPROD
    5. 8.1.5 Checking Up on Macro Variable Scopes
  55. 8.2 Even More Macro Statements
    1. 8.2.1 Extending the Use of %SYMDEL
    2. 8.2.2 Using the %GOTO and %label Statements Appropriately
    3. 8.2.3 Using %WINDOW and %DISPLAY
    4. 8.2.4 Extending %SYSEXEC with Examples
    5. 8.2.5 Deleting Macro Definitions with %SYSMACDELETE
    6. 8.2.6 Making Macro Variables READONLY
  56. 8.3 Even More Automatic Macro Variables
    1. 8.3.1 Passing VALUES into SAS Using &SYSPARM
    2. 8.3.2 Learning More about Deciphering Errors
    3. 8.3.3 Taking Advantage of the Parameter Buffer
    4. 8.3.4 Using &SYSNOBS as an Observation Counter
    5. 8.3.5 Using &SYSMACRONAME
    6. 8.3.6 Using &SYSLIBRC and &SYSFILRC
  57. 8.4 Even More System Options
    1. 8.4.1 Memory Control Options
    2. 8.4.2 Preventing New Macro Definitions with NOMCOMPILE
  58. 8.5 Even More DATA Step Functions and Statements
    1. 8.5.1 DOSUBL Function
    2. 8.5.2 Deleting Macro Variables with CALL SYMDEL
    3. 8.5.3 Using SYMEXIST, SYMGLOBL, and SYMLOCAL
  59. Chapter 9: Exploring Some Less Common Intermediate Topics
  60. 9.1 Building Macro Calls
    1. 9.1.1 Building Macro Calls %&name
    2. 9.1.2 Calling Macros from the Display Manager
  61. 9.2 Working with Macro Variables
    1. 9.2.1 Determining Macro Variable Existence and Scope
    2. 9.2.2 Creating a Large Number of Macro Variables
  62. 9.3 Using the Macro Language to Form Simple Hash Tables
  63. 9.4 Using the Macro Language for Formatted Table LookUps
  64. 9.5 Making Comparisons to Null Values—Some Considerations
  65. 9.6 Evaluating Expressions Stored in a Data Set
  66. 9.7 Using Macro Language Elements on Remote Servers
  67. 9.8 Working with Macro Variables That Contain Special Characters
    1. 9.8.1 Quoting Review
    2. 9.8.2 The Problem with Quotes
    3. 9.8.3 Ampersands and Percent Signs
    4. 9.8.4 Lists and Nested Functions—The Comma Problem
  68. Chapter 10: Building and Using Macro Libraries
  69. 10.1 Establishing Macro Libraries
  70. 10.2 Using %INCLUDE as a Macro Library
  71. 10.3 Using Stored Compiled Macro Libraries
    1. 10.3.1 Stored Compiled Macro Library Overview
    2. 10.3.2 Defining and Using a Stored Compiled Macro Library
    3. 10.3.3 Storing and Retrieving the Source Code for Compiled Macros
    4. 10.3.4 Recovering Compiled Macro Source Code
    5. 10.3.5 Using the %SYSMACDELETE Statement
    6. 10.3.6 Changing the SASMSTORE= libref Location
  72. 10.4 Using the Autocall Facility
    1. 10.4.1 Autocall Library Review
    2. 10.4.2 Tracking Autocall Macro Locations
    3. 10.4.3 Options Used with Macro Libraries
  73. 10.5 Macro Library Essentials
    1. 10.5.1 The Macro Library Search Order
    2. 10.5.2 Establishing a Macro Library Structure and Strategy
    3. 10.5.3 Interactive Macro Development
    4. 10.5.4 Modifying the SASAUTOS System Variable
  74. 10.6 Autocall Macros Supplied by SAS
    1. 10.6.1 %VERIFY and %KVERIFY
    2. 10.6.2 %LEFT and %QLEFT
    3. 10.6.3 %CMPRES and %QCMPRES
    4. 10.6.4 %LOWCASE and %QLOWCASE
    5. 10.6.5 %TRIM and %QTRIM
    6. 10.6.6 %DATATYP
    7. 10.6.7 %COMPSTOR
    8. 10.6.8 Autocall Macros That Assist with Color Conversions
    9. 10.6.9 Surfacing Other Autocall Macros Supplied by SAS
  75. Part 3: Dynamic Macro Coding Techniques
  76. Chapter 11: Writing Dynamic Programs
  77. 11.1 Dynamic Programming Introduction and Design Elements
    1. 11.1.1 A Short Macro Language Review from the Perspective of a Dynamic Programmer
    2. 11.1.2 Elements of a Dynamic Program
    3. 11.1.3 Creating Data Independence
    4. 11.1.4 Elements for Making a Program Dynamic
    5. 11.1.5 Controlling the Program with Data
    6. 11.1.6 List Processing Basics
    7. 11.1.7 Iterative Step Execution
    8. 11.1.8 Building Statements
  78. 11.2 Information Sources
    1. 11.2.1 Using SASHELP Views
    2. 11.2.2 Using SQL DICTIONARY Tables
    3. 11.2.3 Automatic Macro Variables
    4. 11.2.4 %SYSFUNC and DATA Step Functions
    5. 11.2.5 Retrieving Operating System Information
    6. 11.2.6 Using Data Set Metadata
    7. 11.2.7 Using Data Tables to Control a Process
    8. 11.2.8 Creating and Using Control Files
    9. 11.2.9 Using SET Statement Options
  79. 11.3 Using &&VAR&I Constructs as Vertical Macro Arrays
    1. 11.3.1 Creating the List of Macro Variables
    2. 11.3.2 Resolving &&VAR&i
    3. 11.3.3 Stepping through a List of Data Sets
  80. 11.4 Horizontal Lists
    1. 11.4.1 Creating Horizontal Lists
    2. 11.4.2 Resolving Horizontal Lists
    3. 11.4.3 Stepping through the Horizontal List
    4. 11.4.4 Counting the Items in a List
  81. 11.5 Using CALL EXECUTE
  82. 11.6 Writing %INCLUDE Programs
  83. 11.7 Writing Applications without Hardcoded Data Dependencies
    1. 11.7.1 Generalized and Controlled Repeatability
    2. 11.7.2 Setting Up Project Control Files
    3. 11.7.3 Using Control Files to Build Macro Variable Lists
    4. 11.7.4 Using Control Files to Create Empty Data Sets
    5. 11.7.5 Using Control Files to Create Data Validation Checks Dynamically
  84. 11.8 Building SAS Statements Dynamically
  85. 11.9 More Than Just the Macro Coding
    1. 11.9.1 Naming Conventions
    2. 11.9.2 Directory Structure
    3. 11.9.3 Using the AUTOEXEC File
    4. 11.9.4 Unifying fileref and libref Definitions
  86. Chapter 12: Examples of Dynamic Programs
  87. 12.1 File Management
    1. 12.1.1 Copy an Unknown Number of Catalogs
    2. 12.1.2 Appending Unknown Data Sets
  88. 12.2 Controlling Output
    1. 12.2.1 Coordinating Titles (or Footnotes)
    2. 12.2.2 Auto Display of ODS Styles
    3. 12.2.3 Consolidating ODS OUTPUT Destination Data Sets
  89. 12.3 Adapting Your SAS Environment
    1. 12.3.1 Maintaining System Options
    2. 12.3.2 Building and Maintaining Formats
    3. 12.3.3 Working with Libraries and Directories
  90. 12.4 Working with Data Sets and Variables
    1. 12.4.1 Splitting a Data Set Vertically
    2. 12.4.2 Creating a List of Variable Names from Procedure Output
    3. 12.4.3 Parsing Individual Values from an Existing Horizontal List
    4. 12.4.4 Placing Commas between Words
    5. 12.4.5 Quoting Words in a List
    6. 12.4.6 Checking for Existence of Variables
    7. 12.4.7 Removing Repeated Words from a List
    8. 12.4.8 Controlled Data Corrections and Manipulations
  91. Part 4: Miscellaneous Topics and Examples
  92. Chapter 13: Examples and Utilities to Perform Various Tasks
  93. 13.1 Working with Operating System Operations
    1. 13.1.1 Write the First N Lines of a Series of Flat Files
    2. 13.1.2 Storing System Clock Values in Macro Variables
    3. 13.1.3 Executing a Series of SAS Programs
  94. 13.2 Working with the Output Delivery System
    1. 13.2.1 Why You Might Need to Automate with Macros
    2. 13.2.2 Controlling Directories
    3. 13.2.3 Controlling Hyperlinks
  95. 13.3 Working with Data
    1. 13.3.1 Selection of a Top Percentage of Observations
    2. 13.3.2 Selection of Top Percentage Using the POINT Option
    3. 13.3.3 Random Selection of Observations
    4. 13.3.4 Building a WHERE Clause Dynamically
  96. Chapter 14: Miscellaneous Topics
  97. 14.1 More on Triple Ampersand Macro Variables
    1. 14.1.1 Overview of Triple-Ampersand Macro Variables
    2. 14.1.2 Selecting Elements from Macro Arrays
  98. 14.2 Doubly Subscripted Macro Arrays
    1. 14.2.1 Subscript Resolution Issues for a Simple Case
    2. 14.2.2 Naming Row and Column Indicators
    3. 14.2.3 Using the &&&VAR&I Variable Form
    4. 14.2.4 Using the %SCAN Function to Identify Array Elements
  99. 14.3 Programming Smarter
    1. 14.3.1 Efficiency Issues
    2. 14.3.2 Programming with Style
    3. 14.3.3 Macro Programming Best Practices
    4. 14.3.4 Debugging Your Macros
    5. 14.3.5 Traps: DATA Step Code versus the Macro Language
    6. 14.3.6 Little Things with a Big Bite
  100. 14.4 Understanding Recursion in the Macro Language
  101. 14.5 Determining Macro Variable Scopes
    1. 14.5.1 Nested or Layered Symbol Tables
    2. 14.5.2 Macro Parameters
    3. 14.5.3 Macro Variables Created with %LET and %DO
    4. 14.5.4 Macro Variables Created with the SYMPUT and SYMPUTX Routines
    5. 14.5.5 Macro Variables Created in a PROC SQL Step Using the INTO: Operator
  102. 14.6 Controlling System Initialization and Termination
    1. 14.6.1 Controlling AUTOEXEC Execution
    2. 14.6.2 Saving the Global Symbol Table
    3. 14.6.3 Executing Initialization and Termination Statements
  103. 14.7 Protecting Macros and Controlling Their Execution
  104. Appendix 1: Exercise Solutions
  105. Chapter 2
  106. Chapter 3
  107. Chapter 4
  108. Chapter 5
  109. Chapter 6
  110. Chapter 7
  111. Section 14.3.6 Quizlette
  112. Appendix 2: Using the Macro Language with Compiled Programs
  113. A2.1 The Problem: Macro Variable Resolution during Compilation
  114. A2.2 Using Macro Variables
    1. A2.2.1 Defining Macro Variables
    2. A2.2.2 Macro Variables in SCL SUBMIT Blocks
    3. A2.2.3 Using Macro Variables in SCL
    4. A2.2.4 Passing Macro Values between SCL Entries
    5. A2.2.5 Using &&VAR&I Macro Arrays in SCL Programs
  115. A2.3 Calling Macros from within Compiled Programs
    1. A2.3.1 Run-Time Macros
    2. A2.3.2 Compile-Time Macros
  116. A2.4 Using the Macro Language with FCMP Functions
    1. A2.4.1 Compile-Time Execution
    2. A2.4.2 Executing a Macro during Function Execution
  117. Appendix 3: Utilities and Examples Locator
  118. Data Set / File Manipulation
  119. Data Variable Manipulation
  120. Data Value Manipulation
  121. Date / Time
  122. Library / Directory Tools
  123. Macro Techniques
  124. Macro Variable Tools
  125. SAS Execution
  126. SAS/GRAPH Tools
  127. System and Environment
  128. Text Manipulation
  129. Appendix 4: Code Sample Locator
  130. A4.1 Macro Variable Constructs
  131. A4.2 Macro Language Statements, Functions, and Autocall Macros
  132. A4.3 %MACRO Statement Options
  133. A4.4 Automatic Macro Variables
  134. A4.5 DATA Step and Other Non-Macro-Language Elements
  135. A4.6 SASHELP Views and DICTIONARY Tables
  136. Appendix 5: Glossary
  137. Bibliography
  138. Index