Acknowledgments xi Preface xiii About the Author xv About This Book xvii New in the Macro Language xxi

Similar documents
From Carpenter's Complete Guide to the SAS Macro Language, Third Edition. Full book available for purchase here.

Understanding the Concepts and Features of Macro Programming 1

Base and Advance SAS

More About SAS Macros

SAS Macro. SAS Training Courses. Amadeus Software Ltd

SAS Macro Language: Reference

Basic Macro Processing Prepared by Destiny Corporation

SAS Certification Handout #11: Adv. Prog. Ch. 9-10

SAS CURRICULUM. BASE SAS Introduction

Introduction. Getting Started with the Macro Facility CHAPTER 1

Contents. About This Book...1

Advanced Macro Topics Steven First, Systems Seminar Consultants, Madison, WI

INTRODUCTION TO SAS HOW SAS WORKS READING RAW DATA INTO SAS

Macro Internals for the User Developer s Overview. Susan O Connor, SAS Institute Inc., Cary, NC

Macro Facility. About the Macro Facility. Automatic Macro Variables CHAPTER 14

Using Macro Functions

Contents of SAS Programming Techniques

CHAPTER 7 Using Other SAS Software Products

Macro Language Dictionary

An Introduction to SAS Macros Steven First, Systems Seminar Consultants, Madison, WI

Why & How To Use SAS Macro Language: Easy Ways To Get More Value & Power from Your SAS Software Tools

From An Introduction to SAS University Edition. Full book available for purchase here.

SAS TM Macro Basics and Fundamentals Timothy J Harrington, Trilogy Consulting Corporation, Waukegan, IL

SAS Online Training: Course contents: Agenda:

Get Started Writing SAS Macros Luisa Hartman, Jane Liao, Merck Sharp & Dohme Corp.

Surviving the SAS Macro Jungle by Using Your Own Programming Toolkit Kevin Russell, SAS Institute Inc., Cary, North Carolina

Run your reports through that last loop to standardize the presentation attributes

Introduction. Keeping Efficiency in Perspective. CHAPTER 11 Writing Efficient and Portable Macros

Foundations and Fundamentals. SAS System Options: The True Heroes of Macro Debugging Kevin Russell and Russ Tyndall, SAS Institute Inc.

Introduction to the SAS Macro Facility

I like macros. But sometimes they can be hard to understand.

BASICS BEFORE STARTING SAS DATAWAREHOSING Concepts What is ETL ETL Concepts What is OLAP SAS. What is SAS History of SAS Modules available SAS

Functions vs. Macros: A Comparison and Summary

Contents. Tutorials Section 1. About SAS Enterprise Guide ix About This Book xi Acknowledgments xiii

What Is SAS? CHAPTER 1 Essential Concepts of Base SAS Software

Contents I Introduction 1 Introduction to PL/SQL iii

Macro Basics. Introduction. Defining and Using Macro Variables. Defining and Using Macros. Macro Parameters. Part 1. Chapter 1. Chapter 2.

Storing and Reusing Macros

SAS Macro Programming Tips and Techniques

Autocall Macros A Quick Overview

SAS Macro Language 1: Essentials. Course Notes

Ryan Stephens. Ron Plew Arie D. Jones. Sams Teach Yourself FIFTH EDITION. 800 East 96th Street, Indianapolis, Indiana, 46240

Tales from the Help Desk 5: Yet More Solutions for Common SAS Mistakes Bruce Gilsen, Federal Reserve Board

GET A GRIP ON MACROS IN JUST 50 MINUTES! Arthur Li, City of Hope Comprehensive Cancer Center, Duarte, CA

A Time Saver for All: A SAS Toolbox Philip Jou, Baylor University, Waco, TX

PROBLEM SOLVING WITH FORTRAN 90

ORACLE: PL/SQL Programming

title1 "Visits at &string1"; proc print data=hospitalvisits; where sitecode="&string1";

The SAS Interface to REXX

Program Validation: Logging the Log

Your Own SAS Macros Are as Powerful as You Are Ingenious

Summary of Contents LIST OF FIGURES LIST OF TABLES

AN INTRODUCTION TO MACRO VARIABLES AND MACRO PROGRAMS Mike Zdeb, School of Public Health

SAS Training BASE SAS CONCEPTS BASE SAS:

A Web-Based Introduction

The SQL Guide to Pervasive PSQL. Rick F. van der Lans

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Efficiency Programming with Macro Variable Arrays

Macro Quoting: Which Function Should We Use? Pengfei Guo, MSD R&D (China) Co., Ltd., Shanghai, China

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Learning C# 3.0. Jesse Liberty and Brian MacDonald O'REILLY. Beijing Cambridge Farnham Köln Sebastopol Taipei Tokyo

Why & How To Use SAS Macro Language: Easy Ways To Get More Value & Power from Your SAS Software Tools

Going Under the Hood: How Does the Macro Processor Really Work?

Arthur L. Carpenter California Occidental Consultants, Oceanside, California

Oracle BPM 10g R3 Programming 1 Essentials

"Charting the Course... MOC C: Querying Data with Transact-SQL. Course Summary

Table of Contents at a Glance

"Charting the Course to Your Success!" MOC D Querying Microsoft SQL Server Course Summary

Overview 14 Table Definitions and Style Definitions 16 Output Objects and Output Destinations 18 ODS References and Resources 20

Logging the Log Magic: Pulling the Rabbit out of the Hat

Excel Programming with VBA (Macro Programming) 24 hours Getting Started

SD10 A SAS MACRO FOR PERFORMING BACKWARD SELECTION IN PROC SURVEYREG

From Getting Started with the Graph Template Language in SAS. Full book available for purchase here.

Oracle Database 11g: SQL Fundamentals I

Review of PC-SAS Batch Programming

DSCI 325: Handout 15 Introduction to SAS Macro Programming Spring 2017

APPENDIX 4 Migrating from QMF to SAS/ ASSIST Software. Each of these steps can be executed independently.

MOBILE MACROS GET UP TO SPEED SOMEWHERE NEW FAST Author: Patricia Hettinger, Data Analyst Consultant Oakbrook Terrace, IL

Procedure for Stamping Source File Information on SAS Output Elizabeth Molloy & Breda O'Connor, ICON Clinical Research

Chapter 1 Readme.doc definitions you need to know 1

Model Viva Questions for Programming in C lab

Job Security: Using the SAS Macro Language to Full Advantage

A Second Look at SAS Macro Design Issues Ian Whitlock, Kennett Square, PA

SAS 101. Based on Learning SAS by Example: A Programmer s Guide Chapter 21, 22, & 23. By Tasha Chapman, Oregon Health Authority

PROGRAMMING WHEN YOU DON T KNOW OBJECT NAMES: AN INTRO TO DICTIONARIES

Introduction p. 1 The Logical and Physical View of Tables p. 1 Database Types p. 4 NULLs p. 6 DDL and DML Statements p. 7 Column and Table Constraint

An Introduction to SAS Macros

An Animated Guide: An Introduction to SAS Macro Quoting Russ Lavery Bryn Mawr, PA

Installing and Administering a Satellite Environment

Contents About SAS Enterprise Guide About This Book xi Acknowledgments xiii

Practical C++ Programming

Make Your Life a Little Easier: A Collection of SAS Macro Utilities. Pete Lund, Northwest Crime and Social Research, Olympia, WA

IBM. TSO/E REXX User's Guide. z/os. Version 2 Release 3 SA

CHAPTER 1: INTRODUCING C# 3

Paper AP14 Modifying The LogParse PassInfo Macro to Provide a Link between Product Usage in Rtrace Log and Time Used in Job Log

Changes and Enhancements

CROSSREF Manual. Tools and Utilities Library

Paper 149 JAY A. JAFFE

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

ощ 'ршорвшэш! цвн-эориэу ощ 'sajbpossv # PIPG DUJ 'ssjmoossv ^ PIPG pipa w н OX ЛЮН VAV

Transcription:

Contents Part 1 Acknowledgments xi Preface xiii About the Author xv About This Book xvii New in the Macro Language xxi Macro Basics Chapter 1 Introduction 3 1.1 Macro Facility Overview 3 1.2 Terminology 4 1.3 Macro Execution Phases 5 1.4 Referencing Environments or Scopes 8 1.5 Chapter Summary 9 Chapter 2 Defining and Using Macro Variables 11 2.1 Naming Macro Variables 12 2.2 Defining Macro Variables 13 2.3 Using Macro Variables 13 2.4 Displaying Macro Variables 16 2.5 Resolving Macro Variables 19 2.5.1 Using the macro variable as a suffix 19 2.5.2 Using the macro variable as a prefix 20 2.5.3 Appending two macro variables to each other 21 2.6 Automatic Macro Variables 22 2.6.1 &SYSDATE, &SYSDATE9, &SYSDAY, and &SYSTIME 22 2.6.2 &SYSLAST and &SYSDSN 23 2.6.3 &SYSERR and &SYSCC 24 2.6.4 &SYSPARM 25 2.6.5 &SYSRC 26 2.6.6 &SYSSITE, &SYSSCP, &SYSSCPL AND &SYSUSERID 27 2.6.7 &SYSMACRONAME 27 2.7 Defining and Using Macro Variables in a PROC SQL Step 27 2.7.1 Counting observations in a data set 28 2.7.2 Building a list of values 28 2.8 Removing Macro Variables 30 2.9 Chapter Summary 30 2.10 Chapter Exercises 31

iv Contents Chapter 3 Defining and Using Macros 33 3.1 Defining a Macro 34 3.1.1 Creating a macro 35 3.1.2 Using a macro to comment a block of code 36 3.2 Invoking a Macro 37 3.3 System Options Used with the Macro Facility 38 3.3.1 General macro options 38 3.3.2 Debugging options 39 3.3.3 Autocall facility options 41 3.3.4 Compiled stored macros 42 3.3.5 Macro search order 44 3.3.6 Memory control options 44 3.4 Display Manager Command-Line Macros 45 3.5 Statement- and Command-Style Macros 46 3.5.1 Turning on system options 46 3.5.2 Defining statement- and command-style macros 47 3.6 Controlling System Initialization 47 3.6.1 Using AUTOEXEC.SAS 47 3.6.2 Controlling AUTOEXEC execution 49 3.7 Chapter Summary 50 3.8 Chapter Exercises 50 Chapter 4 Macro Parameters 53 4.1 Introducing Macro Parameters 54 4.2 Positional Parameters 54 4.2.1 Defining positional parameters 54 4.2.2 Passing positional parameters 55 4.3 Keyword Parameters 56 4.3.1 Defining keyword parameters 57 4.3.2 Passing keyword parameters 57 4.3.3 Documenting your macro 58 4.4 Choosing between Keyword and Positional Parameters 59 4.4.1 Selecting parameter types 59 4.4.2 Using keyword and positional parameters together 60 4.5 Chapter Summary 61 4.6 Chapter Exercises 61

Contents v Part 2 Using Macros Chapter 5 Program Control through Macros 65 5.1 Macros That Invoke Macros 66 5.1.1 Passing parameters between macros 66 5.1.2 Controlling macro calls 70 5.1.3 Nesting macro definitions 71 5.2 Using Conditional Macro Statements 72 5.2.1 Executing macro statements 73 5.2.2 Building SAS code dynamically 74 5.2.3 Using the IN operator 76 5.3 Iterative Execution Using Macro Statements 77 5.3.1 %DO block 78 5.3.2 Iterative %DO loops 80 5.3.3 %DO %UNTIL loops 83 5.3.4 %DO %WHILE loops 84 5.4 Macro Program Statements 85 5.4.1 Macro comments 86 5.4.2 %GLOBAL and %LOCAL 88 5.4.3 %GOTO and %label 91 5.4.4 Using %SYSEXEC 93 5.4.5 Creating macro %WINDOWs 94 5.4.6 Abnormal termination of macro execution with the %ABORT statement 95 5.4.7 Normal termination of macro execution with the %RETURN statement 96 5.5 Chapter Summary 96 5.6 Chapter Exercises 97 Chapter 6 Interfacing with Data 101 6.1 Using the SYMPUT Routine 102 6.1.1 First argument of SYMPUT 104 6.1.2 Second argument of SYMPUT 104 6.1.3 SYMPUT example 105 6.1.4 Using SYMPUTX 105 6.2 Using a SAS Data Set As a Control File 107 6.2.1 Macro variable values 107 6.2.2 Assigning macro variable names as well as values 113 6.3 Macro Variable Forms Used in Dynamic Programs 116 6.3.1 Elements of a dynamic program 116 6.3.2 Indirect macro variable references 118 6.3.3 Sources of control information 118

vi Contents 6.4 Moving Text from Macro to DATA Step Variables 119 6.4.1 Assignment and RETAIN statements 119 6.4.2 Using the SYMGET function 120 6.4.3 Using the RESOLVE function 122 6.5 Doing More with the SQL Step 127 6.5.1 Placing a single value into a single macro variable 128 6.5.2 Creating more than one macro variable 128 6.5.3 Placing a list of values into a series of macro variables 129 6.5.4 Using the SQL dictionary tables 132 6.5.5 Automatic SQL generated macro variables 133 6.6 Execution of Macro Code Using CALL EXECUTE 134 6.6.1 Executing non-macro code 135 6.6.2 Executing macro code 135 6.6.3 Timing issues 138 6.7 Chapter Summary 141 6.8 Chapter Exercises 142 Chapter 7 Using Macro Functions 143 7.1 Quoting Functions 145 7.1.1 Using the %BQUOTE function 147 7.1.2 %STR 149 7.1.3 Considerations when quoting 150 7.1.4 Basic types of quoting functions and why we care 155 7.1.5 A bit about the %QUOTE and %NRQUOTE functions 156 7.1.6 %UNQUOTE 156 7.1.7 %SUPERQ 157 7.1.8 Quoting function summary 158 7.1.9 Marking and quoting mismatched symbols with the %STR and %QUOTE functions 159 7.2 Text Functions 160 7.2.1 %INDEX 160 7.2.2 %LENGTH 161 7.2.3 %SCAN and %QSCAN 162 7.2.4 %SUBSTR and %QSUBSTR 164 7.2.5 %UPCASE and %QUPCASE 165 7.3 Evaluation Functions 166 7.3.1 Explicit use of %EVAL 166 7.3.2 Implicit use of %EVAL 167 7.3.3 Using %SYSEVALF 168 7.4 Using DATA Step Functions and Routines 170 7.4.1 Using %SYSCALL 171 7.4.2 Using %SYSFUNC and %QSYSFUNC 172

Contents vii 7.5 Building Your Own Macro Functions 178 7.5.1 Introduction 178 7.5.2 Building the function 179 7.5.3 Using the function 182 7.6 Other Useful Macro Functions 183 7.6.1 One-liners 183 7.6.2 Functions for the DATA step 189 7.6.3 Macro functions with logic 191 7.7 Chapter Summary 197 7.8 Chapter Exercises 197 Chapter 8 Using Macro References with the SAS Component Language (SCL) 199 8.1 The Problem Is 200 8.2 Using Macro Variables 201 8.2.1 Defining macro variables 202 8.2.2 Macro variables in SUBMIT blocks 202 8.2.3 Using automatic macro variables 203 8.2.4 Passing macro values between SCL entries 204 8.2.5 Using &&VAR&I macro arrays in SCL programs 204 8.3 Calling Macros from within SCL Programs 205 8.3.1 Run-time macros 205 8.3.2 Compile-time macros 205 8.4 Chapter Summary 207 Part 3 Advanced Macro Topics, Utilities, and Examples Chapter 9 Writing Dynamic Code 211 9.1 Elements of Dynamic Programs 212 9.1.1 Logical branches 213 9.1.2 Iterative step execution 213 9.1.3 Building statements 214 9.2 Writing Applications without Hard-coded Data Dependencies 216 9.2.1 Generalized and controlled repeatability 217 9.2.2 Setting up control files 218 9.2.3 Building macro variables from control files 220 9.3 Using &&VAR&I Constructs as Macro Arrays 221 9.3.1 Resolving &&VAR&i 222 9.3.2 Stepping through a list of data sets 222

viii Contents 9.3.3 Checking for observations with duplicate KEY values 222 9.3.4 Coordinating two macro variable lists 224 9.4 Building SAS Statements Dynamically 227 9.4.1 Using the DATA _NULL_ and %INCLUDE 228 9.4.2 Using the CALL EXECUTE routine 231 9.4.3 Using macro lists rather than macro arrays 233 9.5 Using SASHELP Views 234 9.5.1 Overview of the SASHELP views 235 9.5.2 Using a view 236 9.6 Chapter Summary 237 Chapter 10 Controlling Your Environment 239 10.1 Operating System Operations 240 10.1.1 Copy members of a catalog 240 10.1.2 Write the first N lines of a series of flat files 242 10.1.3 Storing system clock values in macro variables 244 10.1.4 Checking for write access 245 10.1.5 Appending unknown data sets 246 10.1.6 Making a directory 251 10.1.7 Executing a series of SAS programs 252 10.1.8 Using %SYSGET to access system variables 255 10.2 Controlling Your Output 255 10.2.1 Combining titles 256 10.2.2 Renumbering listing pages 260 10.2.3 Coordinating titles (or footnotes) 261 10.3 Adapting Your SAS Environment 262 10.3.1 Maintaining system options 262 10.3.2 Building and maintaining formats 265 10.3.3 Working with libraries and directories 268 10.4 Coordinating with the Output Delivery System (ODS) 269 10.4.1 Why we might need to automate with macros 270 10.4.2 Controlling ODS locations 270 10.4.3 Using ODS to control the destination 271 10.4.4 Graphics devices 271 10.4.5 Using WEBFRAME with GRSEG catalog entries 272 10.4.6 Building an index with PROC PRINT 274 10.4.7 Creating drill-down graphs and charts 275 10.5 Chapter Summary 277

Contents ix Chapter 11 Working with SAS Data Sets 279 11.1 Creating Flat Files 280 11.1.1 Column-specified flat file 280 11.1.2 Creating comma-delimited files 284 11.2 Subsetting a SAS Data Set 286 11.2.1 Selection of top percentage using SQL 287 11.2.2 Selection of top percentage using the POINT option 288 11.2.3 Random selection of observations 288 11.2.4 Building a WHERE clause dynamically 292 11.3 Checking the Existence of SAS Data Sets 295 11.4 Working with Data Set Variables 296 11.4.1 Create a list of variable names from the PDV 296 11.4.2 Create a list of variable names from an ID variable 304 11.4.3 Creating individual macro variables from an existing list 304 11.4.4 Counting words within a macro variable 306 11.4.5 Placing commas between words 308 11.4.6 Quoting words in a list 311 11.4.7 Check for existence of variables 313 11.4.8 Remove repeated words from a list 314 11.4.9 Working with a list of class variables 316 11.5 Counting Observations in a Data Set 319 11.5.1 Using %SYSFUNC and ATTRN 320 11.5.2 Controlling observations in PRINT listings 321 11.5.3 Using a DATA _NULL_ step 323 11.5.4 Using SQL 324 11.6 Chapter Summary 326 Chapter 12 Building and Using Macro Libraries 327 12.1 Library Overview 328 12.1.1 Using %INCLUDE as a macro library 329 12.1.2 Using compiled stored macros 330 12.1.3 Using the autocall facility 331 12.2 Macro Library Essentials 332 12.2.1 The macro library search order 332 12.2.2 Establishing a macro library structure and strategy 333 12.2.3 Interactive macro development 334 12.2.4 Modifying the SASAUTOS system variable 335 12.3 SAS Autocall Macros 335 12.3.1 %VERIFY 337 12.3.2 %LEFT 338 12.3.3 %CMPRES 339 12.3.4 %LOWCASE 340

x Contents 12.3.5 %TRIM 342 12.3.6 %DATATYP 343 12.3.7 %COMPSTOR 344 12.4 Chapter Summary 345 Chapter 13 Miscellaneous Macro Topics 347 13.1 Other Specialized Tasks 348 13.1.1 &&&VAR Using triple-ampersand macro variables 348 13.1.2 Totals based on a list of macro variables 349 13.1.3 Selecting elements from macro arrays 351 13.1.4 Calculating permutations 352 13.1.5 Checking if a macro variable exists 354 13.1.6 Extending the use of %SYMDEL 355 13.2 Doubly Subscripted Macro Arrays 357 13.2.1 Subscript resolution issues 358 13.2.2 Naming row and column indicators 358 13.2.3 Using the &&&VAR&I variable form 361 13.2.4 Using the %SCAN function to identify array elements 363 13.3 Programming Smarter 368 13.3.1 Efficiency issues 368 13.3.2 Programming with style 371 13.3.3 Debugging your macros 373 13.3.4 The DATA step versus the macro language 375 13.3.5 Little things with a big bite 380 13.4 Working with Macro Parameter Buffers 389 13.4.1 Calling a macro using /PARMBUFF 390 13.4.2 Using the PARMBUFF macro switch 391 13.5 Understanding Recursion in the Macro Language 393 13.6 Determining Macro Variable Scopes 395 13.7 Chapter Summary 397 Appendix 1 Exercise Solutions 399 Appendix 2 Utilities Locator 415 Appendix 3 Example Locator 419 Appendix 4 Macro Locator 425 Appendix 5 Example Data Sets 431 Glossary 439 Bibliography 443 Index 463