Contents Page: iii About This Book Page: ix Purpose Page: ix Is This Book for You? Page: ix Prerequisites Page: ix Scope of This Book Page: ix About the Examples Page: x Software Used to Develop the Book's Content Page: x Example Code and Data Page: x Additional Resources Page: x Keep in Touch Page: xi To Contact the Author through SAS Press Page: xi SAS Books Page: xi SAS Book Report Page: xi Publish with SAS Page: xi About The Author Page: xiii Acknowledgments Page: xv Part 1 Understanding the Concepts and Features of Macro Programming Page: 1 Chapter 1 Introduction Page: 3 What Is the SAS Macro Facility? Page: 4 Example 1.1: Using a Macro Variable to Select Observations to Process Page: 5 Example 1.2: Using a Macro Program to Execute the Same PROC Step on Multiple Data Sets Page: 5 What Are the Advantages of the SAS Macro Facility? Page: 5 Example 1.3: Defining and Using Macro Variables Page: 6 Where Can the SAS Macro Facility Be Used? Page: 11 Examples of the SAS Macro Facility Page: 12 Example 1.4: Displaying System Information Page: 12 Example 1.5: Conditional Processing of SAS Steps Page: 13 Example 1.6: Iterative Processing of SAS Steps Page: 15 Example 1.7: Passing Information between Program Steps Page: 16 Example 1.8: Interfacing Macro Language and SAS Language Functions Page: 18 Example 1.9: Building and Saving a Library of Utility Routines Page: 19 Chapter 2 Mechanics of Macro Programming Page: 21 Introduction Page: 21 The Vocabulary of SAS Processing Page: 21 SAS Processing without Macro Activity Page: 22 Understanding Tokens Page: 23 Tokenizing a SAS Program Page: 24 Comparing Macro Language Processing and SAS Language Processing Page: 26 Processing a SAS Program That Contains Macro Language Page: 27 Chapter 3 Macro Variables Page: 37 Introduction Page: 37 Basic Concepts of Macro Variables Page: 38 Referencing Macro Variables Page: 39 Example 3.1: Defining and Referencing Macro Variables Page: 39 Understanding Macro Variable Resolution and the Use of Single and Double Quotation Marks Page: 41 Example 3.2: Resolving Macro Variables Enclosed in Quotation Marks Page: 41 Displaying Macro Variable Values Page: 42 Using the %PUT Statement Page: 43 Example 3.3: Submitting %PUT _AUTOMATIC_ Page: 44 Example 3.4: Submitting %PUT _GLOBAL_ Page: 45 Example 3.5: Submitting %PUT Statements to Display Text and Macro Variable Values Page: 45 Displaying Macro Variable Values As They Resolve by Enabling the SYMBOLGEN System Option Page: 46 Example 3.6: Displaying Macro Variable Values with the SYMBOLGEN Option Page: 47 Understanding Automatic Macro Variables Page: 48 Example 3.7: Using Automatic Variables Page: 50 Understanding User-Defined Macro Variables Page: 51 Creating Macro Variables with the %LET Statement Page: 52 Example 3.8: Using the %LET Statement Page: 52 Combining Macro Variables with Text Page: 55 Placing Text before a Macro Variable Reference Page: 55 Example 3.9: Placing Text before a Macro Variable Reference Page: 55 Placing Text after a Macro Variable Reference Page: 57 Example 3.10: Placing Text after a Macro Variable Reference Page: 57 Concatenating Permanent SAS Data Set Names and Catalog Names with Macro Variables Page: 58 Example 3.11: Referencing Permanent SAS Data Set Names and Macro Variables Page: 59 Referencing Macro Variables Indirectly Page: 60 Example 3.12: Resolving Two Ampersands That Precede a Macro Variable Reference Page: 60 Example 3.13: Illustrating the Resolution of Macro Variable References When Combining Them Page: 61 Example 3.14: Resolving Multiple Ampersands That Precede a Macro Variable Page: 64 Chapter 4 Macro Programs Page: 67 Introduction Page: 67 Creating Macro Programs Page: 68 Example 4.1: Demonstrating a Macro Program Page: 71 Example 4.2 Page: 73 Executing a Macro Program Page: 74 Example 4.3 Page: 74 Displaying Notes about Macro Program Compilation in the SAS Log Page: 75 Displaying Messages about Macro Program Processing in the SAS Log Page: 77 Using MPRINT to Display the SAS Statements Submitted by a Macro Program Page: 77 Using the MLOGIC Option to Trace Execution of a Macro Program Page: 78 Passing Values to a Macro Program through Macro Parameters Page: 79 Specifying Positional Parameters in Macro Programs Page: 80 Example 4.4: Defining a Macro Program with Positional Parameters Page: 80 Specifying Keyword Parameters in Macro Programs Page: 82 Example 4.5: Defining a Macro Program with Keyword Parameters Page: 83 Specifying Mixed Parameter Lists in Macro Programs Page: 86 Example 4.6: Defining a Macro Program with Positional Parameters and Keyword Parameters Page: 86 Defining a Macro Program That Can Accept a Varying Number of Parameter Values Page: 89 Example 4.7: Defining a Macro Program with the PARMBUFF Option Page: 90 Chapter 5 Understanding Macro Symbol Tables and the Processing of Macro Programs Page: 95 Introduction Page: 95 Understanding Macro Symbol Tables Page: 95 Understanding the Global Macro Symbol Table Page: 97 Example 5.1: Referencing a Global Macro Variable in Open Code and from within a Macro Program Page: 98 Understanding Local Macro Symbol Tables Page: 101 Example 5.2: Referencing a Macro Variable Defined as a Parameter to a Macro Program – Version 1 Page: 102 Example 5.3: Referencing a Macro Variable Defined as a Parameter to a Macro Program – Version 2 Page: 105 Example 5.4: Referencing a Macro Variable Defined as a Parameter to a Macro Program – Version 3 Page: 106 Working with Global Macro Variables and Local Macro Variables Page: 108 Defining the Domain of a Macro Variable by Using the %GLOBAL or %LOCAL Macro Language Statements Page: 110 Example 5.5: Using the Same Name for a Macro Variable Defined Globally and Defined Locally Page: 111 Processing of Macro Programs Page: 114 How a Macro Program Is Compiled Page: 114 How a Macro Program Executes Page: 120 Chapter 6 Macro Language Functions Page: 129 Introduction Page: 129 Macro Character Functions Page: 130 Example 6.1: Using %SUBSTR to Extract Text from a Macro Variable Value Page: 131 Example 6.2: Using %SCAN to Extract the Nth Item from a Macro Variable Value Page: 131 Example 6.3: Using %UPCASE to Convert a Macro Variable Value to Uppercase Page: 132 Macro Evaluation Functions Page: 133 Macro Quoting Functions Page: 135 Macro Variable Attribute Functions Page: 137 Example 6.4: Using Macro Variable Attribute Functions to Determine Domain and Existence of Macro Va Page: 138 Other Macro Functions Page: 141 Using the %SYSFUNC and %QSYSFUNC Macro Functions Page: 141 Example 6.5: Using %SYSFUNC to Format a Date in the TITLE Statement Page: 142 Example 6.6: Using %SYSFUNC to Execute a SAS Language Function and Assign the Result to a Macro Vari Page: 142 Example 6.7: Using %SYSFUNC and the NOTNAME and NVALID SAS Language Functions to Determine If a Val Page: 143 Example 6.8: Using %SYSFUNC to Apply a SAS Statistical Function to Macro Variable Values Page: 144 Example 6.9: Using the %SYSFUNC Function to Apply Several SAS Language Functions That Obtain and Di Page: 145 SAS Supplied Autocall Macro Programs Used Like Functions Page: 147 Example 6.10: Determining with %VERIFY and %UPCASE If a Value Is in a Defined Set of Characters Page: 148 Chapter 7 Macro Expressions and Macro Programming Statements Page: 151 Introduction Page: 151 Macro Language Statements Page: 151 Constructing Macro Expressions Page: 154 Understanding Arithmetic Expressions Page: 155 Understanding Logical Expressions Page: 156 Understanding the IN Operator As Used in Macro Language Statements Page: 157 Conditional Processing with the Macro Language Page: 158 Example 7.1: Using Logical Expressions Page: 158 Example 7.2: Using Macro Language to Select SAS Steps for Processing Page: 160 Example 7.3: Using %IF-%THEN/%ELSE Statements to Modify and Select Statements within a Step Page: 162 Example 7.4: Writing %IF-%THEN/%ELSE Statements That Use the IN Operator Page: 166 Iterative Processing with the Macro Language Page: 167 Writing Iterative %DO Loops in the Macro Language Page: 167 Example 7.5: Building PROC Steps with Iterative %DO Loops Page: 167 Example 7.6: Building SAS Statements within a Step with Iterative %DO Loops Page: 169 Conditional Iteration with %DO %UNTIL Page: 170 Example 7.7: Building SAS Steps with %DO %UNTIL Loops Page: 170 Conditional Iteration with %DO %WHILE Page: 172 Example 7.8: Building SAS Steps with %DO %WHILE Loops Page: 172 Branching in Macro Processing Page: 174 Example 7.9: Using %GOTO to Branch in a Macro Program Page: 174 Chapter 8 Masking Special Characters and Mnemonic Operators Page: 177 Introduction Page: 177 Why Are Quoting Functions Called Quoting Functions? Page: 178 Illustrating the Need for Macro Quoting Functions Page: 179 Describing the Commonly Used Macro Quoting Functions Page: 180 %STR and %NRSTR Page: 180 %BQUOTE Page: 181 %SUPERQ Page: 181 Other Macro Quoting Functions: %NRBQUOTE, %QUOTE, and %NRQUOTE Page: 181 Understanding How Macro Quoting Functions Work Page: 181 Applying Macro Quoting Functions Page: 182 Example 8.1: Using %STR to Prevent Interpretation of the Semicolon As a SAS Statement Terminator Page: 182 Example 8.2: Using %STR to Prevent Interpretation of the Comma as an Argument Delimiter Page: 183 Example 8.3: Using %STR to Preserve Leading and Trailing Blanks Page: 184 Example 8.4: Using %NRSTR to Mask Macro Triggers Page: 185 Example 8.5: Using %STR to Mask Unbalanced Quotation Marks and Preceding Percent Signs Page: 186 Example 8.6: Masking Macro Triggers and Unbalanced Quotation Marks with %NRSTR and Preceding Percen Page: 187 Example 8.7: Using %BQUOTE to Prevent Interpretation of Mnemonic Operators Page: 187 Example 8.8: Using %SUPERQ to Prevent Resolution of Special Characters in a Macro Variable Value Page: 188 Specifying Macro Program Parameters That Contain Special Characters or Mnemonic Operators Page: 189 Example 8.9: Masking Special Characters in Parameter Values Page: 190 Example 8.10: Preventing Misinterpretation of Special Characters in Parameter Values Page: 191 Example 8.11: Masking Special Characters and Mnemonic Operators in Parameter Values Page: 193 Unmasking Text and the %UNQUOTE Function Page: 198 Example 8.12: Using %UNQUOTE to Cause Interpretation of a Masked Character Page: 198 Using Quoting Versions of Macro Character Functions and Autocall Macro Programs Page: 198 Example 8.13: Using %QSYSFUNC to Mask the Result from Applying a SAS Language Function Page: 199 Example 8.14: Using %QSUBSTR to Mask the Results of %SUBSTR Page: 200 Chapter 9 Interfaces to the Macro Facility Page: 201 Introduction Page: 201 Understanding DATA Step Interfaces to the Macro Facility Page: 201 Understanding the SYMGET Function Page: 202 Example 9.1: Using a Data Set Variable Name as the Argument to the SYMGET Function Page: 204 Example 9.2: Retrieving Macro Variable Values and Creating Numeric Data Set Variables with SYMGET Page: 206 Example 9.3: Using the Resolution of a Character Expression As an Argument to SYMGET Page: 207 Understanding the SYMPUT and SYMPUTX Call Routines Page: 209 Example 9.4: Saving the Sum of a Variable in a Macro Variable by Executing CALL SYMPUTX Once at the Page: 211 Example 9.5: Executing CALL SYMPUTX Multiple Times in a DATA Step Page: 213 Example 9.6: Creating Several Macro Variables with CALL SYMPUTX Page: 213 Example 9.7: Creating a Macro Variable with CALL SYMPUTX and Specifying Its Symbol Table Page: 214 Understanding the CALL EXECUTE Routine Page: 216 Example 9.8: Illustrating the Timing of CALL EXECUTE When It Invokes a Macro Program That Submits M Page: 217 Example 9.9: Illustrating the Timing of CALL EXECUTE When It Invokes a Macro Program That Submits M Page: 218 Example 9.10: Using CALL EXECUTE to Conditionally Call a Macro Program Page: 220 Example 9.11: Using CALL EXECUTE to Call a Specific Macro Program Page: 223 Understanding the RESOLVE Function Page: 227 Example 9.12: Obtaining Macro Variable Values with RESOLVE by Resolving Character Expressions Page: 228 Example 9.13: Using RESOLVE to Call a Macro Program within a DATA Step That Assigns Text to a Data Page: 230 Using Macro Facility Features in PROC SQL Page: 231 Creating and Updating Macro Variables with PROC SQL Page: 232 Example 9.14: Using the INTO Clause in PROC SQL to Save Summarizations in Macro Variables Page: 233 Example 9.15: Demonstrating the Default Action of the INTO Clause in Saving the First Row of a Tabl Page: 234 Example 9.16: Using the INTO Clause in PROC SQL to Create a Macro Variable for Each Row in a Table Page: 236 Example 9.17: Storing All Unique Values of a Table Column in One Macro Variable with PROC SQL Page: 237 Example 9.18: Storing All Values of a PROC SQL Dictionary Table Column in One Macro Variable Page: 237 Using the Macro Variables Created by PROC SQL Page: 238 Example 9.19: Using the PROC SQL SQLOBS Automatic Macro Variable Page: 239 Displaying Macro Option Settings with PROC SQL and Dictionary Tables Page: 241 Example 9.20: Accessing Macro Option Settings with PROC SQL and Dictionary Tables Page: 242 Example 9.21: Accessing Macro Variable Characteristics with PROC SQL and Dictionary Tables Page: 243 Part 2 Applying Your Knowledge of Macro Programming Page: 245 Chapter 10 Storing and Reusing Macro Programs Page: 247 Introduction Page: 247 Saving Macro Programs with the Autocall Facility Page: 248 Creating an Autocall Library Page: 248 Making Autocall Libraries Available to Your Programs Page: 250 Maintaining Access to the Autocall Macro Programs That Ship with SAS Page: 251 Defining Filerefs under Windows and Using Them to Identify Autocall Libraries Page: 251 Explicitly Specifying the Directory Locations of Autocall Libraries on the OPTIONS Statement Page: 252 Identifying Autocall Libraries That Are Stored in SAS Catalogs Page: 252 Listing the Names of the Autocall Libraries That Are Defined in the SAS Session Page: 252 Using the Autocall Facility under UNIX and z/OS Systems Page: 252 Using the Autocall Facility under UNIX Page: 252 Using the Autocall Facility under MVS Batch Page: 253 Saving Macro Programs with the Stored Compiled Macro Facility Page: 254 Setting SAS Options to Create Stored Compiled Macro Programs Page: 254 Creating Stored Compiled Macro Programs Page: 255 Example 10.1: Creating a Stored Compiled Macro Program Page: 256 Saving and Retrieving the Source Code of a Stored Compiled Macro Program Page: 257 Example 10.2: Saving the Source Code of a Stored Compiled Macro Program Page: 257 Encrypting a Stored Compiled Macro Program Page: 258 Example 10.3: Encrypting a Stored Compiled Macro Program Page: 258 Resolving Macro Program References When Using the Autocall Facility and the Stored Compiled Macro Fa Page: 258 Chapter 11 Building a Library of Utilities Page: 261 Introduction Page: 261 Writing a Macro Program to Behave Like a Function Page: 261 Example 11.1: Examining Specific Data Set Characteristics Page: 261 Example 11.2: Editing Character Data for Comparisons Page: 264 Programming Routine Tasks Page: 266 Example 11.3: Standardizing RTF Output Page: 266 Example 11.4: Documenting Characteristics of a Data Set Page: 269 Chapter 12 Debugging Macro Programming and Adding Error Checking to Macro Programs Page: 273 Introduction Page: 273 Understanding the Types of Errors That Can Occur in Macro Programming Page: 273 Minimizing Errors in Developing SAS Programs That Contain Macro Language Page: 274 Categorizing and Checking for Common Problems in Macro Programming Page: 275 Understanding the Tools That Can Debug Macro Programming Page: 278 Using SAS System Options to Debug Macro Programming Page: 278 Using Macro Language Statements to Debug Macro Programming Page: 279 Using Macro Functions to Debug Macro Programming Page: 280 Using Automatic Macro Variables to Debug Macro Programming Page: 280 Examples of Solving Errors in Macro Programming Page: 281 Example 12.1: Reviewing System Options When Macro Facility Warnings and Error Messages Are Absent Page: 281 Example 12.2: Attempting to Process a Macro Program When the Macro Processor Does Not Detect a %MEND Page: 283 Example 12.3: Tracing Problems in Expression Evaluation with the %PUT Statement and the MLOGIC Syste Page: 285 Example 12.4: Using %PUT to Trace a Problem at Execution Page: 289 Example 12.5: Finding a Logic Error in the Execution of a Macro Program with the MLOGIC Option Page: 292 Example 12.6: Using the MPRINT Option to Find Errors in SAS Language That Was Generated by Macro Lan Page: 296 Improving Your Macro Programming by Including Error Checking Page: 298 Example 12.7: Evaluating Parameter Values Page: 299 Example 12.8: Reviewing SAS Processing Messages Page: 301 Example 12.9: Reviewing SAS Processing Results Page: 303 Chapter 13 A Stepwise Method for Writing Macro Programs Page: 307 Introduction Page: 307 Building a Macro Program in Four Steps Page: 307 Applying the Four Steps to an Example Page: 308 Step 1: Write, test, and debug the SAS program(s) that you want the macro program to build Page: 309 Program for Report A with No Macro Facility Features Page: 309 Program for Report B with No Macro Facility Features Page: 310 Program for Report C with No Macro Facility Features Page: 314 Step 2: Remove hard-coded programming constants from the program(s) in Step 1 and replace these cons Page: 317 Program for Report A with Step 2 Modifications Page: 317 Program for Report B with Step 2 Modifications Page: 318 Program for Report C with Step 2 Modifications Page: 320 Step 3: Create macro program(s) from the program(s) in Step 2 Page: 321 Program for Report A with Step 3 Modifications Page: 321 Program for Report B with Step 3 Modifications Page: 322 Program for Report C with Step 3 Modifications Page: 324 Step 4: Refine and generalize the macro program(s) in Step 3 by adding macro language statements lik Page: 325 Executing the REPORT Macro Program Page: 328 Obtaining the Contents of Report A Using the REPORT Macro Program Page: 329 Obtaining the Contents of Report B Using the REPORT Macro Program Page: 331 Obtaining the Contents of Report C Using the REPORT Macro Program Page: 335 Enhancing the Macro Program REPORT Page: 338 Part 3 Appendixes Page: 339 Appendix A Sample Data Set Page: 341 Appendix B Reference to Examples In This Book Page: 343 Index Page: 355 A Page: 355 B Page: 355 C Page: 355 D Page: 355 E Page: 356 F Page: 356 G Page: 357 H Page: 357 I Page: 357 J Page: 357 K Page: 357 L Page: 357 M Page: 358 N Page: 361 O Page: 361 P Page: 361 Q Page: 362 R Page: 363 S Page: 363 T Page: 366 U Page: 367 V Page: 367 W Page: 367 Y Page: 367 Z Page: 367 Symbols Page: 367
Description:The macro facility is a popular part of SAS. Macro programming is a required skill for many SAS programming jobs, and the SAS Advanced Programming Certification Exam tests macro processing concepts. Whether you're looking to become certified, land a job, or increase your skills, you'll benefit from SAS Macro Programming Made Easy, Third Edition.
By following Michele Burlew's examples and step-by-step instructions, you'll be able to rapidly perform repetitive programming tasks, to pass information between programming steps more easily, and to make your programming easier to read.
Updated for SAS 9.4, this book teaches you the elements of the macro facility (macro variables, macro programs, macro language), how to write a macro program, techniques for macro programming, 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.
Beginning macro programmers will learn to write SAS macro programs quickly and efficiently. More experienced programmers will find this book useful to refresh their conceptual knowledge and expand on their macro programming skills. Ultimately, any user interested in automating their programs—including analysts, programmers, and report writers—will find Michele Burlew's book an excellent tutorial.
This book is part of the SAS Press program.