You are previewing SAS® Macro Programming Made Easy, Second Edition.
O'Reilly logo
SAS® Macro Programming Made Easy, Second Edition

Book Description

Need help understanding the SAS macro facility? You will find all the answers that you need in this easy-to-follow book! Michele Burlew puts her decades of macro programming experience to work for you as she guides you through the basics, as well as the more complex features of the SAS macro facility. Updated extensively for SAS 9, this book includes plenty of examples and step-by-step instructions. You will learn the elements of the macro facility (macro variables, macro programs, macro language), how to write a macro program, macro programming techniques, tips on using the macro facility, how the macro facility fits into SAS, and about the interfaces between the macro facility and other components of SAS.

New topics for the second edition include using SAS 9 macro and SAS language features, debugging macro programs, adding error checking to macro programs, and building a library of utility macro programs.

Beginning macro programmers will learn to write SAS macro programs quickly and efficiently. More experienced macro programmers will find this book useful to refresh their conceptual knowledge and expand on their macro programming skills. It is also important to note that the macro facility is a highly popular part of SAS and found in the job requirements for many SAS programming jobs. In addition, macro facility concepts are tested on the SAS Advanced Programming Exam. The book assumes some SAS programming experience, including knowledge of how to write a DATA step and how to use SAS procedures.

Table of Contents

  1. Copyright
  2. Praise from the Experts
  3. Preface
    1. How Can This Book Help You Understand the SAS Macro Facility?
    2. About the Data and Programs in This Book
    3. The Typographical Styles in This Book
  4. Acknowledgments
  5. 1. Understanding the Concepts and Features of Macro Programming
    1. 1. Introduction
      1. What Is the SAS Macro Facility?
        1. 1.1. Using a Macro Variable to Select Observations to Process
        2. 1.2. Using a Macro Program to Execute the Same PROC Step on Multiple Data Sets
      2. What Are the Advantages of the SAS Macro Facility?
        1. 1.3. Defining and Using Macro Variables
      3. Where Can the SAS Macro Facility Be Used?
      4. Examples of the SAS Macro Facility
        1. 1.4. Displaying System Information
        2. 1.5. Conditional Processing of SAS Steps
        3. 1.6. Iterative Processing of SAS Steps
        4. 1.7. Passing Information between Program Steps
        5. 1.8. Interfacing Macro Language and SAS Language Functions
        6. 1.9. Building and Saving a Library of Utility Routines
    2. 2. Mechanics of Macro Processing
      1. Introduction
      2. The Vocabulary of SAS Processing
      3. SAS Processing without Macro Activity
      4. Understanding Tokens
      5. Tokenizing a SAS Program
      6. Comparing Macro Language Processing and SAS Language Processing
      7. Processing a SAS Program That Contains Macro Language
    3. 3. Macro Variables
      1. Introduction
      2. Basic Concepts of Macro Variables
      3. Referencing Macro Variables
        1. 3.1. Defining and Referencing Macro Variables
      4. Understanding Macro Variable Resolution and the Use of Single and Double Quotation Marks
        1. 3.2. Resolving Macro Variables Enclosed in Quotation Marks
      5. Displaying Macro Variable Values
        1. Using the %PUT Statement
          1. 3.3. Submitting %PUT _AUTOMATIC_
          2. 3.4. Submitting %PUT _GLOBAL_
          3. 3.5. Submitting %PUT Statements to Display Text and Macro Variable Values
        2. Displaying Macro Variable Values As They Resolve by Enabling the SYMBOLGEN Option
          1. 3.6. Displaying Macro Variable Values with the SYMBOLGEN Option
      6. Understanding Automatic Macro Variables
        1. 3.7. Using Automatic Variables
      7. Understanding User-Defined Macro Variables
        1. Creating Macro Variables with the %LET Statement
          1. 3.8. Using the %LET Statement
      8. Combining Macro Variables with Text
        1. Placing Text before a Macro Variable Reference
          1. 3.9. Placing Text before a Macro Variable Reference
        2. Placing Text after a Macro Variable Reference
          1. 3.10. Placing Text after a Macro Variable Reference
        3. Concatenating Permanent SAS Data Set Names and Catalog Names with Macro Variables
          1. 3.11. Referencing Permanent SAS Data Set Names and Macro Variables
      9. Referencing Macro Variables Indirectly
        1. Resolving Two Ampersands That Precede a Macro Variable Reference
        2. Resolving Multiple Ampersands before a Macro Variable Reference
    4. 4. Macro Programs
      1. Introduction
      2. Creating Macro Programs
      3. Executing a Macro Program
      4. Displaying Notes about Macro Program Compilation in the SAS Log
      5. Displaying Messages about Macro Program Processing in the SAS Log
        1. Using MPRINT to Display the SAS Statements Submitted by a Macro Program
        2. Using the MLOGIC Option to Trace Execution of a Macro Program
      6. Passing Values to a Macro Program through Macro Parameters
        1. Specifying Positional Parameters in Macro Programs
          1. 4.1. Defining a Macro Program with Positional Parameters
        2. Specifying Keyword Parameters in Macro Programs
          1. 4.2. Defining a Macro Program with Keyword Parameters
        3. Specifying Mixed Parameter Lists in Macro Programs
          1. 4.3. Defining a Macro Program with Positional Parameters and Keyword Parameters
        4. Defining a Macro Program That Can Accept a Varying Number of Parameter Values
          1. 4.4. Defining a Macro Program with the PARMBUFF Option
    5. 5. Understanding Macro Symbol Tables and the Processing of Macro Programs
      1. Introduction
      2. Understanding Macro Symbol Tables
        1. Understanding the Global Macro Symbol Table
        2. Understanding Local Macro Symbol Tables
        3. Working with Global Macro Variables and Local Macro Variables
        4. Defining the Domain of a Macro Variable by Using the %GLOBAL or %LOCAL Macro Language Statements
      3. Processing of Macro Programs
        1. How a Macro Program Is Compiled
        2. How a Macro Program Is Executed
    6. 6. Macro Language Functions
      1. Introduction
      2. Macro Character Functions
        1. 6.1. Using %SUBSTR to Extract Text from a Macro Variable Value
        2. 6.2. Using %SCAN to Extract the Nth Item from a Macro Variable Value
        3. 6.3. Using %UPCASE to Convert a Macro Variable Value to Uppercase
      3. Macro Evaluation Functions
      4. Macro Quoting Functions
      5. Macro Variable Attribute Functions
        1. 6.4. Using Macro Variable Attribute Functions to Determine Domain and Existence of Macro Variables
      6. Other Macro Functions
        1. Using the %SYSFUNC and %QSYSFUNC Macro Functions
        2. 6.5. Using %SYSFUNC to Format a Date in the TITLE Statement
        3. 6.6. Using %SYSFUNC to Execute a SAS Language Function and Assign the Result to a Macro Variable
        4. 6.7. Using %SYSFUNC and the NOTNAME and NVALID SAS Language Functions to Determine If a Value Is a Valid SAS Variable Name
        5. 6.8. Using %SYSFUNC to Apply a SAS Statistical Function to Macro Variable Values
        6. 6.9. Using the %SYSFUNC Function to Apply Several SAS Language Functions That Obtain and Display Information about a Data Set
      7. SAS Supplied Autocall Macro Programs Used Like Functions
        1. 6.10. Determining with %VERIFY and %UPCASE If a Value Is in a Defined Set of Characters
    7. 7. Macro Expressions and Macro Programming Statements
      1. Introduction
      2. Macro Language Statements
      3. Constructing Macro Expressions
        1. Understanding Arithmetic Expressions
        2. Understanding Logical Expressions
        3. Understanding the IN Operator As Used in Macro Language Statements
      4. Conditional Processing with the Macro Language
        1. 7.1. Using Logical Expressions
        2. 7.2. Using Macro Language to Select SAS Steps for Processing
        3. 7.3. Using %IF-%THEN/%ELSE Statements to Modify and Select Statements within a Step
        4. 7.4. Writing %IF-%THEN/%ELSE Statements That Use the IN Operator
      5. Iterative Processing with the Macro Language
        1. Writing Iterative %DO Loops in the Macro Language
          1. 7.5. Building PROC Steps with Iterative %DO Loops
          2. 7.6. Building SAS Statements within a Step with Iterative %DO Loops
        2. Conditional Iteration with %DO %UNTIL
          1. 7.7. Building SAS Steps with %DO %UNTIL Loops
        3. Conditional Iteration with %DO %WHILE
          1. 7.8. Building SAS Steps with %DO %WHILE Loops
      6. Branching in Macro Processing
        1. 7.9. Using %GOTO to Branch in a Macro Program
    8. 8. Masking Special Characters and Mnemonic Operators
      1. Introduction
      2. Why Are Quoting Functions Called Quoting Functions?
      3. Illustrating the Need for Macro Quoting Functions
      4. Describing the Commonly Used Macro Quoting Functions
        1. %STR and %NRSTR
        2. %BQUOTE and %NRBQUOTE
        3. %SUPERQ
        4. %QUOTE and %NRQUOTE
      5. Understanding How Macro Quoting Functions Work
      6. Applying Macro Quoting Functions
        1. 8.1. Using %STR to Prevent Interpretation of the Semicolon As a SAS Statement Terminator
        2. 8.2. Using %STR to Prevent Interpretation of the Comma As an Argument Delimiter
        3. 8.3. Using %STR to Preserve Leading and Trailing Blanks
        4. 8.4. Using %NRSTR to Mask Macro Triggers
        5. 8.5. Using %STR and %BQUOTE to Mask Unbalanced Quotation Marks and Preceding Percent Signs
        6. 8.6. Masking Macro Triggers and Unbalanced Quotation Marks with %NRSTR and Preceding Percent Signs
        7. 8.7. Using %BQUOTE to Prevent Interpretation of Mnemonic Operators
        8. 8.8. Using %SUPERQ to Prevent Resolution of Special Characters in a Macro Variable Value
      7. Specifying Macro Program Parameters That Contain Special Characters or Mnemonic Operators
        1. 8.9. Masking Special Characters in Parameter Values
        2. 8.10. Masking Equal Signs in Parameter Values to Prevent Misinterpretation of Positional Parameters as Keyword Parameters
        3. 8.11. Masking Special Characters and Mnemonic Operators in Parameter Values
      8. Unmasking Text and the %UNQUOTE Function
        1. 8.12. Using %UNQUOTE to Cause Interpretation of a Masked Character
      9. Using Quoting Versions of Macro Character Functions and Autocall Macro Programs
        1. 8.13. Using %QSYSFUNC to Mask the Result from Applying a SAS Language Function
        2. 8.14. Using %QSUBSTR to Mask the Results of %SUBSTR
    9. 9. Interfaces to the Macro Facility
      1. Introduction
      2. Understanding DATA Step Interfaces to the Macro Facility
        1. Understanding the SYMGET and SYMGETN Functions
          1. 9.1. Using a Data Set Variable Name As the Argument to the SYMGET Function
          2. 9.2. Retrieving Macro Variable Values and Creating Numeric Data Set Variables with SYMGETN
          3. 9.3. Using the Resolution of a Character Expression As an Argument to SYMGET
        2. Understanding the SYMPUT and SYMPUTX Call Routines
          1. 9.4. Saving the Sum of a Variable in a Macro Variable by Executing CALL SYMPUT Once at the End of a DATA Step
          2. 9.5. Executing CALL SYMPUTX Multiple Times in a DATA Step
          3. 9.6. Creating Several Macro Variables with CALL SYMPUT and CALL SYMPUTX
          4. 9.7. Creating a Macro Variable with CALL SYMPUTX and Specifying Its Symbol Table
        3. Understanding the CALL EXECUTE Routine
          1. 9.8. Illustrating the Timing of CALL EXECUTE When It Invokes a Macro Program That Submits Macro Statements
          2. 9.9. Illustrating the Timing of CALL EXECUTE When It Invokes a Macro Program That Submits Macro Statements and a PROC Step
          3. 9.10. Using CALL EXECUTE to Conditionally Call a Macro Program
          4. 9.11. Using CALL EXECUTE to Call a Specific Macro Program
        4. Understanding the RESOLVE Function
          1. 9.12. Obtaining Macro Variable Values with RESOLVE by Resolving Character Expressions
          2. 9.13. Using RESOLVE to Call a Macro Program within a DATA Step That Assigns Text to a Data Set Variable
      3. Using Macro Facility Features in PROC SQL
        1. Creating and Updating Macro Variables with PROC SQL
          1. 9.14. Using the INTO Clause in PROC SQL to Save Summarizations in Macro Variables
          2. 9.15. Demonstrating the Default Action of the INTO Clause in Saving the First Row of a Table
          3. 9.16. Using the INTO Clause in PROC SQL to Create a Macro Variable for Each Row in a Table
          4. 9.17. Storing All Unique Values of a Table Column in One Macro Variable with PROC SQL
          5. 9.18. Storing All Values of a PROC SQL Dictionary Table Column in One Macro Variable
        2. Using the Macro Variables Created by PROC SQL
          1. 9.19. Using the PROC SQL SQLOBS Automatic Macro Variable
        3. Displaying Macro Option Settings with PROC SQL and Dictionary Tables
          1. 9.20. Accessing Macro Option Settings with PROC SQL and Dictionary Tables
          2. 9.21. Accessing Macro Variable Characteristics with PROC SQL and Dictionary Tables
      4. Using Macro Facility Features in SAS Component Language
        1. Using the Macro Facility to Pass Information between SCL Programs
          1. 9.22. Creating a Macro Variable in an SCL Program
        2. Referencing Macro Variables in SUBMIT Blocks
          1. 9.23. Using SUBMIT Blocks That Contain Macro Variables
  6. 2. Applying Your Knowledge of Macro Programming
    1. 10. Storing and Reusing Macro Programs
      1. Introduction
      2. Saving Macro Programs with the Autocall Facility
        1. Creating an Autocall Library
        2. Making Autocall Libraries Available to Your Programs
        3. Maintaining Access to the Autocall Macro Programs That Ship with SAS
          1. Defining Filerefs under Windows XP and Using Them to Identify Autocall Libraries
          2. Explicitly Specifying the Directory Locations of Autocall Libraries on the OPTIONS Statement
          3. Identifying Autocall Libraries That Are Stored in SAS Catalogs
          4. Listing the Names of the Autocall Libraries That Are Defined in the SAS Session
        4. Using the Autocall Facility under Windows, MVS/TSO, and Other Directory-Based Systems
          1. Using the Autocall Facility under MVS Batch
          2. Using the Autocall Facility under TSO
          3. Using the Autocall Facility under UNIX
          4. Using the Autocall Facility under OpenVMS
      3. Saving Macro Programs with the Stored Compiled Macro Facility
        1. Setting SAS Options to Create Stored Compiled Macro Programs
        2. Creating Stored Compiled Macro Programs
          1. 10.1. Creating a Stored Compiled Macro Program
        3. Saving and Retrieving the Source Code of a Stored Compiled Macro Program
          1. 10.2. Saving the Source Code of a Stored Compiled Macro Program
        4. Encrypting a Stored Compiled Macro Program
          1. 10.3. Encrypting a Stored Compiled Macro Program
      4. Resolving Macro Program References When Using the Autocall Facility and the Stored Compiled Macro Facility
    2. 11. Building a Library of Utilities
      1. Introduction
      2. Writing a Macro Program to Behave Like a Function
        1. 11.1. Examining Specific Data Set Characteristics
        2. 11.2. Editing Character Data for Comparisons
      3. Programming Routine Tasks
        1. 11.3. Standardizing RTF Output
        2. 11.4. Documenting Characteristics of a Data Set
    3. 12. Debugging Macro Programming and Adding Error Checking to Macro Programs
      1. Introduction
      2. Understanding the Types of Errors That Can Occur in Macro Programming
      3. Minimizing Errors in Developing SAS Programs That Contain Macro Language
      4. Categorizing and Checking for Common Problems in Macro Programming
      5. Understanding the Tools That Can Debug Macro Programming
        1. Using SAS System Options to Debug Macro Programming
        2. Using Macro Language Statements to Debug Macro Programming
        3. Using Macro Functions to Debug Macro Programming
        4. Using Automatic Macro Variables to Debug Macro Programming
      6. Examples of Solving Errors in Macro Programming
        1. 12.1. Reviewing System Options When Macro Facility Warnings and Error Messages Are Absent
        2. 12.2. Attempting to Process a Macro Program When the Macro Processor Does Not Detect a %MEND Statement
        3. 12.3. Tracing Problems in Expression Evaluation with the %PUT Statement and the MLOGIC System Option
        4. 12.4. Using %PUT to Trace a Problem at Execution
        5. 12.5. Finding a Logic Error in the Execution of a Macro Program with the MLOGIC Option
        6. 12.6. Using the MPRINT Option to Find Errors in SAS Language That Was Generated by Macro Language
      7. Improving Your Macro Programming by Including Error Checking
        1. 12.7. Evaluating Parameter Values
        2. 12.8. Reviewing SAS Processing Messages
        3. 12.9. Reviewing SAS Processing Results
    4. 13. A Stepwise Method for Writing Macro Programs
      1. Introduction
      2. Building a Macro Program in Four Steps
      3. Applying the Four Steps to an Example
        1. Step 1: Write, test, and debug the SAS program(s) that you want the macro program to build
          1. Program for Report A with No Macro Facility Features
          2. Program for Report B with No Macro Facility Features
          3. Program for Report C with No Macro Facility Features
        2. Step 2: Remove hard-coded programming constants from the program(s) in Step 1 and replace these constants with macro variables
          1. Program for Report A with Step 2 Modifications
          2. Program for Report B with Step 2 Modifications
          3. Program for Report C with Step 2 Modifications
        3. Step 3: Create macro program(s) from the program(s) in Step 2
          1. Program for Report A with Step 3 Modifications
          2. Program for Report B with Step 3 Modifications
          3. Program for Report C with Step 3 Modifications
        4. Step 4: Refine and generalize the macro program(s) in Step 3 by adding macro language statements like %IF-%THEN and %DO groups
        5. Executing the REPORT Macro Program
          1. Obtaining the Contents of Report A Using the REPORT Macro Program
          2. Obtaining the Contents of Report B Using the REPORT Macro Program
          3. Obtaining the Contents of Report C Using the REPORT Macro Program
        6. Enhancing the Macro Program REPORT
  7. 3. Appendixes
    1. A. Abridged Macro Language Reference
      1. Selected SAS Options Used with the Macro Facility
      2. Automatic Macro Variables
      3. Macro Functions
      4. Macro Language Statements
      5. PROC SQL Interface to the Macro Facility
      6. SAS Functions and Routines That Interface with the Macro Facility
    2. B. Reserved Words in the Macro Facility
    3. C. Sample Data Set
    4. D. Reference to Programs in This Book
  8. Books Available from SAS Press
    1. JMP® Books