O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

SAS 9.4 Macro Language, 5th Edition

Book Description

Explains how to increase the modularity, flexibility, and maintainability of your SAS code using the SAS macro facility. Provides complete information about macro language elements, interfaces between the SAS macro facility and other parts of SAS software, and macro processing in general.

Table of Contents

  1. Contents
  2. What’s New in the SAS 9.4 Macro Facility
    1. Overview
    2. SAS Macro Facility Error and Warning Messages
    3. SAS Macro Examples
    4. New Automatic Macro Variables
    5. New Options for Macro Statements
    6. Enhancements to Macro System Options
  3. Understanding and Using the Macro Facility
    1. Introduction to the Macro Facility
      1. Getting Started with the Macro Facility
      2. Using the Macro Facility in SAS Viya
      3. Replacing Text Strings Using Macro Variables
      4. Generating SAS Code Using Macros
        1. Defining Macros
        2. Inserting Comments in Macros
        3. Macro Definition Containing Several SAS Statements
        4. Passing Information into a Macro Using Parameters
        5. Conditionally Generating SAS Code
      5. More Advanced Macro Techniques
        1. Generating Repetitive Pieces of Text Using %DO Loops
        2. Generating a Suffix for a Macro Variable Reference
      6. Other Features of the Macro Language
    2. SAS Programs and Macro Processing
      1. SAS Programs and Macro Processing
      2. How SAS Processes Statements without Macro Activity
      3. How SAS Processes Statements with Macro Activity
    3. Macro Variables
      1. Macro Variables
      2. Macro Variables Defined by the Macro Processor
      3. Macro Variables Defined by Users
        1. Overview for Defining Macro Variables
        2. Creating User-Defined Macro Variable Names
        3. Assigning Values to Macro Variables
      4. Using Macro Variables
        1. Macro Variable Reference
        2. Combining Macro Variable References with Text
        3. Delimiting Macro Variable Names within Text
        4. Creating a Period to Follow Resolved Text
      5. Displaying Macro Variable Values
      6. Referencing Macro Variables Indirectly
        1. Using an Expression to Generate a Reference
        2. Generating a Series of Macro Variable References with a Single Macro Call
        3. Using More Than Two Ampersands
      7. Manipulating Macro Variable Values with Macro Functions
    4. Macro Processing
      1. Macro Processing
      2. Defining and Calling Macros
      3. How the Macro Processor Compiles a Macro Definition
      4. How the Macro Processor Executes a Compiled Macro (1/2)
      5. How the Macro Processor Executes a Compiled Macro (2/2)
      6. Summary of Macro Processing
    5. Scopes of Macro Variables
      1. Scopes of Macro Variables
      2. Global Macro Variables
      3. Local Macro Variables
      4. Writing the Contents of Symbol Tables to the SAS Log
      5. How Macro Variables Are Assigned and Resolved
      6. Examples of Macro Variable Scopes (1/2)
      7. Examples of Macro Variable Scopes (2/2)
        1. Changing the Values of Existing Macro Variables
        2. Creating Local Variables
        3. Forcing a Macro Variable to Be Local
        4. Creating Global Macro Variables
        5. Creating Global Variables Based on the Value of Local Variables
      8. Special Cases of Scope with the CALL SYMPUT Routine (1/2)
      9. Special Cases of Scope with the CALL SYMPUT Routine (2/2)
        1. Overview of CALL SYMPUT Routine
        2. Example Using CALL SYMPUT with Complete DATA Step and a Nonempty Local Symbol Table
        3. Example Using CALL SYMPUT with an Incomplete DATA Step
        4. Example Using CALL SYMPUT with a Complete DATA Step and an Empty Local Symbol Table
        5. Example Using CALL SYMPUT with SYSPBUFF and an Empty Local Symbol Table
    6. Macro Expressions
      1. Macro Expressions
      2. Defining Arithmetic and Logical Expressions
        1. Evaluating Arithmetic and Logical Expressions
        2. Operands and Operators
      3. How the Macro Processor Evaluates Arithmetic Expressions
        1. Evaluating Numeric Operands
        2. Evaluating Floating-Point Operands
      4. How the Macro Processor Evaluates Logical Expressions
        1. Comparing Numeric Operands in Logical Expressions
        2. Comparing Floating-Point or Missing Values
        3. Comparing Character Operands in Logical Expressions
    7. Macro Quoting
      1. Macro Quoting
        1. Masking Special Characters and Mnemonics
        2. Understanding Why Macro Quoting Is Necessary
        3. Overview of Macro Quoting Functions
        4. Passing Parameters That Contain Special Characters and Mnemonics
      2. Deciding When to Use a Macro Quoting Function and Which Function to Use
      3. %STR and %NRSTR Functions
        1. Using %STR and %NRSTR Functions
        2. Using Unmatched Quotation Marks and Parentheses with %STR and %NRSTR
        3. Using % Signs with %STR
        4. Examples Using %STR
        5. Examples Using %NRSTR
      4. %BQUOTE and %NRBQUOTE Functions
        1. Using %BQUOTE and %NRBQUOTE Functions
        2. Examples Using %BQUOTE
      5. Referring to Already Quoted Variables
      6. Deciding How Much Text to Mask with a Macro Quoting Function
      7. %SUPERQ Function
        1. Using %SUPERQ
        2. Examples Using %SUPERQ
        3. Using the %SUPERQ Function to Prevent Warning Messages
        4. Using the %SUPERQ Function to Enter Macro Keywords
      8. Summary of Macro Quoting Functions and the Characters That They Mask
      9. Unquoting Text
        1. Restoring the Significance of Symbols
        2. Example of Unquoting
        3. What to Do When Automatic Unquoting Does Not Work
      10. How Macro Quoting Works
      11. Other Functions That Perform Macro Quoting
        1. Functions That Start with the Letter Q
        2. Example Using the %QSCAN Function
    8. Interfaces with the Macro Facility
      1. Interfaces with the Macro Facility
      2. DATA Step Interfaces
        1. Interacting with the Macro Facility during DATA Step Execution
        2. CALL EXECUTE Routine Timing Details
        3. Example of Using CALL EXECUTE Incorrectly
        4. Example of Common Problem with CALL EXECUTE
      3. Using SAS Language Functions in the DATA Step and Macro Facility
      4. Interfaces with the SQL Procedure
        1. Using PROC SQL
        2. INTO Clause
        3. Controlling Job Execution
      5. Interfaces with the SAS Component Language
        1. Using an SCL Program
        2. How Macro References Are Resolved by SCL
        3. Referencing Macro Variables in Submit Blocks
        4. Considerations for Sharing Macros between SCL Programs
        5. Example Using Macros in an SCL Program
      6. SAS/CONNECT Interfaces
        1. Overview of SAS/CONNECT Interfaces
        2. Using %SYSRPUT with SAS/CONNECT
        3. Example Using %SYSRPUT to Check the Value of a Return Code on a Remote Host
        4. Using %SYSLPUT with SAS/CONNECT
        5. Example Using %SYSLPUT
    9. Storing and Reusing Macros
      1. Storing and Reusing Macros
      2. Saving Macros in an Autocall Library
        1. Overview of an Autocall Library
        2. Using Directories as Autocall Libraries
        3. Using SAS Catalogs as Autocall Libraries
        4. Calling an Autocall Macro
      3. Saving Macros Using the Stored Compiled Macro Facility
        1. Overview of the Stored Compiled Macro Facility
        2. Compiling and Storing a Macro Definition
        3. Storing Autocall Macros Supplied by SAS
        4. Calling a Stored Compiled Macro
    10. Macro Facility Error Messages and Debugging
      1. General Macro Debugging Information
        1. Developing Macros in a Layered Approach
        2. Encountering Errors
        3. Developing Bug-free Macros
      2. Troubleshooting Your Macros (1/3)
      3. Troubleshooting Your Macros (2/3)
      4. Troubleshooting Your Macros (3/3)
        1. Solving Common Macro Problems
        2. Solving Macro Variable Resolution Problems
        3. Solving Problems with Macro Variable Scope
        4. Solving Open Code Statement Recursion Problems
        5. Solving Problems with Macro Functions
        6. Solving Unresolved Macro Problems
        7. Solving the “Black Hole” Macro Problem
        8. Resolving Timing Issues
        9. Example of a Macro Statement Executing Immediately
        10. Resolving Macro Resolution Problems Occurring during DATA Step Compilation
        11. Solving Problems with the Autocall Facility
        12. Fixing Autocall Library Specifications
        13. Fixing Autocall Macro Definition Errors
        14. File and Macro Names for Autocall
        15. Displaying Information about Stored Compiled Macros
        16. Solving Problems with Expression Evaluation
      5. Debugging Techniques (1/2)
      6. Debugging Techniques (2/2)
        1. Using System Options to Track Problems
        2. Tracing the Flow of Execution with MLOGIC
        3. Nesting Information Generated by MLOGICNEST
        4. Examining the Generated SAS Statements with MPRINT
        5. Nesting Information Generated by MPRINTNEST
        6. Storing MPRINT Output in an External File
        7. Examining Macro Variable Resolution with SYMBOLGEN
        8. Using the %PUT Statement to Track Problems
    11. Writing Efficient and Portable Macros
      1. Writing Efficient and Portable Macros
      2. Keeping Efficiency in Perspective
      3. Writing Efficient Macros
        1. Use Macros Wisely
        2. Use Name Style Macros
        3. Avoid Nested Macro Definitions
        4. Assign Function Results to Macro Variables
        5. Turn Off System Options When Appropriate
        6. Use the Stored Compiled Macro Facility
        7. Centrally Store Autocall Macros
        8. Other Useful Efficiency Tips
        9. Storing Only One Copy of a Long Macro Variable Value
      4. Writing Portable Macros (1/2)
      5. Writing Portable Macros (2/2)
        1. Using Portable SAS Language Functions with %SYSFUNC
        2. Example Using %SYSFUNC
        3. Using Automatic Variables with Host-Specific Values
        4. Example Using SYSPARM
        5. SYSPARM Details
        6. SYSRC Details
        7. Macro Language Elements with System Dependencies
        8. Host-Specific Macro Variables
        9. Naming Macros and External Files for Use with the Autocall Facility
    12. Macro Language Elements
      1. Macro Language Elements
      2. Macro Statements
        1. Using Macro Statements
        2. Macro Statements That Perform Automatic Evaluation
      3. Macro Functions (1/2)
      4. Macro Functions (2/2)
        1. Using Macro Functions
        2. Macro Character Functions
        3. Macro Evaluation Functions
        4. Macro Quoting Functions
        5. Compilation Quoting Functions
        6. Execution of Macro Quoting Functions
        7. Quotation Marks and Parentheses without a Match
        8. Macro Functions for Double-Byte Character Set (DBCS)
        9. Other Macro Functions
      5. Automatic Macro Variables
      6. Interfaces with the Macro Facility
      7. Selected Autocall Macros Provided with SAS Software
        1. Overview of Provided Autocall Macros
        2. Required System Options for Autocall Macros
        3. Using Autocall Macros
        4. Autocall Macros for Double-Byte Character Set (DBCS)
      8. Selected System Options Used in the Macro Facility
  4. Macro Language Dictionary
    1. AutoCall Macros
      1. AutoCall Macros
      2. Dictionary
        1. %CMPRES and %QCMPRES Autocall Macros
        2. %COMPSTOR Autocall Macro
        3. %DATATYP Autocall Macro
        4. %KVERIFY Autocall Macro
        5. %LEFT Autocall Macro
        6. %LOWCASE and %QLOWCASE Autocall Macros
        7. %QCMPRES Autocall Macro
        8. %QLEFT Autocall Macro
        9. %QLOWCASE Autocall Macro
        10. %QTRIM Autocall Macro
        11. %SYSRC Autocall Macro
        12. %TRIM and %QTRIM Autocall Macro
        13. %VERIFY Autocall Macro
    2. Automatic Macro Variables
      1. Automatic Macro Variables
      2. Dictionary
        1. SYSADDRBITS Automatic Macro Variable
        2. SYSBUFFR Automatic Macro Variable
        3. SYSCC Automatic Macro Variable
        4. SYSCHARWIDTH Automatic Macro Variable
        5. SYSCMD Automatic Macro Variable
        6. SYSDATASTEPPHASE Automatic Macro Variable
        7. SYSDATE Automatic Macro Variable
        8. SYSDATE9 Automatic Macro Variable
        9. SYSDATE9 Automatic Macro Variable
        10. SYSDAY Automatic Macro Variable
        11. SYSDEVIC Automatic Macro Variable
        12. SYSDMG Automatic Macro Variable
        13. SYSDSN Automatic Macro Variable
        14. SYSENCODING Automatic Macro Variable
        15. SYSENDIAN Automatic Macro Variable
        16. SYSENV Automatic Macro Variable
        17. SYSERR Automatic Macro Variable
        18. SYSERRORTEXT Automatic Macro Variable
        19. SYSFILRC Automatic Macro Variable
        20. SYSHOSTINFOLONG Automatic Macro Variable
        21. SYSHOSTNAME Automatic Macro Variable
        22. SYSINCLUDEFILEDEVICE Automatic Macro Variable
        23. SYSINCLUDEFILEDIR Automatic Macro Variable
        24. SYSINCLUDEFILEFILEREF Automatic Macro Variable
        25. SYSINCLUDEFILENAME Automatic Macro Variable
        26. SYSINDEX Automatic Macro Variable
        27. SYSINFO Automatic Macro Variable
        28. SYSJOBID Automatic Macro Variable
        29. SYSLAST Automatic Macro Variable
        30. SYSLCKRC Automatic Macro Variable
        31. SYSLIBRC Automatic Macro Variable
        32. SYSLOGAPPLNAME Automatic Macro Variable
        33. SYSMACRONAME Automatic Macro Variable
        34. SYSMAXLONG Automatic Macro Variable
        35. SYSMENV Automatic Macro Variable
        36. SYSMSG Automatic Macro Variable
        37. SYSNCPU Automatic Macro Variable
        38. SYSNOBS Automatic Macro Variable
        39. SYSODSESCAPECHAR Automatic Macro Variable
        40. SYSODSPATH Automatic Macro Variable
        41. SYSPARM Automatic Macro Variable
        42. SYSPBUFF Automatic Macro Variable
        43. SYSPRINTTOLIST Automatic Macro Variable
        44. SYSPRINTTOLOG Automatic Macro Variable
        45. SYSPROCESSID Automatic Macro Variable
        46. SYSPROCESSMODE Automatic Macro Variable
        47. SYSPROCESSNAME Automatic Macro Variable
        48. SYSPROCNAME Automatic Macro Variable
        49. SYSRC Automatic Macro Variable
        50. SYSSCP and SYSSCPL Automatic Macro Variables
        51. SYSSCPL Automatic Macro Variable
        52. SYSSITE Automatic Macro Variable
        53. SYSSIZEOFLONG Automatic Macro Variable
        54. SYSSIZEOFPTR Automatic Macro Variable
        55. SYSSIZEOFUNICODE Automatic Macro Variable
        56. SYSSTARTID Automatic Macro Variable
        57. SYSSTARTNAME Automatic Macro Variable
        58. SYSTCPIPHOSTNAME Automatic Macro Variable
        59. SYSTIME Automatic Macro Variable
        60. SYSTIMEZONE Automatic Macro Variable
        61. SYSTIMEZONEIDENT Automatic Macro Variable
        62. SYSTIMEZONEOFFSET Automatic Macro Variable
        63. SYSUSERID Automatic Macro Variable
        64. SYSVER Automatic Macro Variable
        65. SYSVLONG Automatic Macro Variable
        66. SYSVLONG4 Automatic Macro Variable
        67. SYSWARNINGTEXT Automatic Macro Variable
    3. DATA Step Call Routines for Macros
      1. DATA Step Call Routines for Macros
      2. Dictionary
        1. CALL EXECUTE Routine
        2. CALL SYMDEL Routine
        3. CALL SYMPUT Routine
        4. CALL SYMPUTN Routine
        5. CALL SYMPUTX Routine
    4. DATA Step Functions for Macros
      1. DATA Step Functions for Macros
      2. Dictionary
        1. RESOLVE Function
        2. SYMEXIST Function
        3. SYMGET Function
        4. SYMGETN Function
        5. SYMGLOBL Function
        6. SYMLOCAL Function
    5. Macro Functions
      1. Macro Functions
      2. Dictionary (1/8)
      3. Dictionary (2/8)
      4. Dictionary (3/8)
      5. Dictionary (4/8)
      6. Dictionary (5/8)
      7. Dictionary (6/8)
      8. Dictionary (7/8)
      9. Dictionary (8/8)
        1. %BQUOTE and %NRBQUOTE Functions
        2. %EVAL Function
        3. %INDEX Function
        4. %LENGTH Function
        5. %NRBQUOTE Function
        6. %NRQUOTE Function
        7. %NRSTR Function
        8. %QSCAN Function
        9. %QSUBSTR Function
        10. %QSYSFUNC Function
        11. %QUOTE and %NRQUOTE Functions
        12. %QUPCASE Function
        13. %SCAN and %QSCAN Functions
        14. %STR and %NRSTR Functions
        15. %SUBSTR and %QSUBSTR Functions
        16. %SUPERQ Function
        17. %SYMEXIST Function
        18. %SYMGLOBL Function
        19. %SYMLOCAL Function
        20. %SYSEVALF Function
        21. %SYSFUNC and %QSYSFUNC Functions
        22. %SYSGET Function
        23. %SYSMACEXEC Function
        24. %SYSMACEXIST Function
        25. %SYSMEXECDEPTH Function
        26. %SYSMEXECNAME Function
        27. %SYSPROD Function
        28. %UNQUOTE Function
        29. %UPCASE and %QUPCASE Functions
    6. SQL Clauses for Macros
      1. SQL Clauses for Macros
      2. Dictionary
        1. INTO Clause
    7. Macro Statements
      1. Macro Statements
      2. Dictionary (1/11)
      3. Dictionary (2/11)
      4. Dictionary (3/11)
      5. Dictionary (4/11)
      6. Dictionary (5/11)
      7. Dictionary (6/11)
      8. Dictionary (7/11)
      9. Dictionary (8/11)
      10. Dictionary (9/11)
      11. Dictionary (10/11)
      12. Dictionary (11/11)
        1. %ABORT Statement
        2. %* Macro Comment Statement
        3. %COPY Statement
        4. %DISPLAY Statement
        5. %DO Statement
        6. %DO, Iterative Statement
        7. %DO %UNTIL Statement
        8. %DO %WHILE Statement
        9. %END Statement
        10. %GLOBAL Statement
        11. %GOTO Statement
        12. %IF-%THEN/%ELSE Statement
        13. %INPUT Statement
        14. %label Statement
        15. %LET Statement
        16. %LOCAL Statement
        17. %MACRO Statement
        18. %MEND Statement
        19. %PUT Statement
        20. %RETURN Statement
        21. %SYMDEL Statement
        22. %SYSCALL Statement
        23. %SYSEXEC Statement
        24. %SYSLPUT Statement
        25. %SYSMACDELETE Statement
        26. %SYSMSTORECLEAR Statement
        27. %SYSRPUT Statement
        28. %WINDOW Statement
    8. System Options for Macros
      1. System Options for Macros
      2. Dictionary (1/7)
      3. Dictionary (2/7)
      4. Dictionary (3/7)
      5. Dictionary (4/7)
      6. Dictionary (5/7)
      7. Dictionary (6/7)
      8. Dictionary (7/7)
        1. CMDMAC System Option
        2. IMPLMAC System Option
        3. MACRO System Option
        4. MAUTOCOMPLOC System Option
        5. MAUTOLOCDISPLAY System Option
        6. MAUTOLOCINDES System Option
        7. MAUTOSOURCE System Option
        8. MCOMPILE System Option
        9. MCOMPILENOTE System Option
        10. MCOVERAGE System Options
        11. MCOVERAGELOC= System Option
        12. MERROR System Option
        13. MERROR System Option
        14. MEXECNOTE System Option
        15. MEXECSIZE System Option
        16. MFILE System Option
        17. MINDELIMITER= System Option
        18. MINOPERATOR System Option
        19. MLOGIC System Option
        20. MLOGICNEST System Option
        21. MPRINT System Option
        22. MPRINTNEST System Option
        23. MRECALL System Option
        24. MREPLACE System Option
        25. MSTORED System Option
        26. MSYMTABMAX= System Option
        27. MVARSIZE= System Option
        28. SASAUTOS= System Option
        29. SASMSTORE= System Option
        30. SERROR System Option
        31. SYMBOLGEN System Option
        32. SYSPARM= System Option
  5. Appendixes
    1. Reserved Words in the Macro Facility
      1. Macro Facility Word Rules
      2. Reserved Words
    2. SAS Macro Facility Error and Warning Messages
      1. SAS Macro Error Messages
      2. SAS Macro Warning Messages (1/2)
      3. SAS Macro Warning Messages (2/2)
    3. SAS Tokens
      1. SAS Tokens
      2. List of Tokens
    4. Syntax for Selected Functions Used with the %SYSFUNC Function
      1. Summary Descriptions and Syntax
      2. Functions and Arguments for %SYSFUNC
    5. SAS Macro Examples
      1. Example 1: Import All CSV Files That Exist within a Directory
        1. Details
        2. Program
        3. Program Description
      2. Example 2: List All Files within a Directory Including Subdirectories
        1. Details
        2. Program
        3. Program Description
      3. Example 3: How to Increment a Macro DO Loop by a Non-integer Value
        1. Details
        2. Program
        3. Program Description
      4. Example 4: How to Use Character Values on a Macro %DO Loop
        1. Details
        2. Program
        3. Program Description
        4. Logs
      5. Example 5: Place All SAS Data Set Variables into a Macro Variable
        1. Details
        2. Program
        3. Program Description
        4. Log
      6. Example 6: How to Retrieve the Program Name That Is Currently Running in Batch Mode or Interactively
        1. Details
        2. Program
        3. Program Description
      7. Example 7: Using a Macro to Create New Variable Names from Variable Values
        1. Details
        2. Program
        3. Program Description
        4. Log
        5. Output
      8. Example 8: Dynamically Determine the Number of Observations and Variables in a SAS Data Set
        1. Details
        2. Program
        3. Program Description
        4. Log
      9. Example 9: Use Macro Logic to Determine If an External File Is Empty
        1. Details
        2. Program
        3. Program Description
      10. Example 10: Retrieve Each Word from a Macro Variable List
        1. Details
        2. Program
        3. Program Description
        4. Output
      11. Example 11: Loop through Dates Using a Macro %DO Loop
        1. Details
        2. Program
        3. Program Description
        4. Log
      12. Example 12: Using Macro Variables within a CARDS or DATALINES Statement
        1. Details
        2. Program
        3. Program Description
        4. Output
      13. Example 13: Print Information to the Output Window If a Data Set Is Empty
        1. Details
        2. Program
        3. Program Description
        4. Output
      14. Example 14: Create a Quoted List Separated by Spaces
        1. Details
        2. Program
        3. Program Description
        4. Log Output
      15. Example 15: Delete a File on UNIX or Windows If It Exists
        1. Details
        2. Program
        3. Program Description
      16. Example 16: Retrieve the File Size, Create Time, and Last Modified Date of an External File
        1. Details
        2. Program
        3. Program Description
      17. Example 17: Delete All User-defined Macro Variables
        1. Details
        2. Program
        3. Program Description
  6. Recommended Reading
  7. Glossary (1/2)
  8. Glossary (2/2)
  9. Index (1/4)
  10. Index (2/4)
  11. Index (3/4)
  12. Index (4/4)