PLA YING WITH MACROS: TAKE THE WORK OUT OF LEARNING TO DO MACROS. Arthur L. Carpenter

Similar documents
Using ANNOTATE MACROS as Shortcuts

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

Arthur L. Carpenter California Occidental Consultants, Oceanside, California

PROC REPORT Basics: Getting Started with the Primary Statements

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

Job Security: Using the SAS Macro Language to Full Advantage

An Introduction to Macros Deb Cassidy

KEYWORDS Metadata, macro language, CALL EXECUTE, %NRSTR, %TSLIT

Advanced PROC REPORT: Getting Your Tables Connected Using Links

Using Macro Functions

Base and Advance SAS

SAS Macro Dynamics - From Simple Basics to Powerful Invocations Rick Andrews, Office of the Actuary, CMS, Baltimore, MD

Using PROC FCMP to the Fullest: Getting Started and Doing More

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

Introduction. Getting Started with the Macro Facility CHAPTER 1

Table Lookups: From IF-THEN to Key-Indexing

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

SAS Macro Language: Reference

Simplifying Your %DO Loop with CALL EXECUTE Arthur Li, City of Hope National Medical Center, Duarte, CA

Using PROC FCMP to the Fullest: Getting Started and Doing More

Reading and Writing RTF Documents as Data: Automatic Completion of CONSORT Flow Diagrams

Are You Missing Out? Working with Missing Values to Make the Most of What is not There

Arthur L. Carpenter California Occidental Consultants

SAS Macro Dynamics: from Simple Basics to Powerful Invocations Rick Andrews, Office of Research, Development, and Information, Baltimore, MD

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

Demystifying Inherited Programs

PharmaSUG Paper TT03. Kirk Paul Lafler, Software Intelligence Corporation, Spring Valley, California

SAS Macro Programming for Beginners

SAS CURRICULUM. BASE SAS Introduction

SAS Macro. SAS Training Courses. Amadeus Software Ltd

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

The DATA Statement: Efficiency Techniques

Importing CSV Data to All Character Variables Arthur L. Carpenter California Occidental Consultants, Anchorage, AK

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

SAS Online Training: Course contents: Agenda:

Sandra Hendren Health Data Institute

Unlock SAS Code Automation with the Power of Macros

Want Quick Results? An Introduction to SAS/GRAPH Software. Arthur L. Carpenter California Occidental Consultants

CONQUERING THE DREADED MACRO ERROR

Tired of CALL EXECUTE? Try DOSUBL

A Tutorial on the SAS Macro Language

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

Effectively Utilizing Loops and Arrays in the DATA Step

More About SAS Macros

Oracle Database: SQL and PL/SQL Fundamentals NEW

WHAT ARE SASHELP VIEWS?

&&&, ;;, and Other Hieroglyphics Advanced Macro Topics Chris Yindra, C. Y. Training Associates

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

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

A Macro that can Search and Replace String in your SAS Programs

This Too Shall Pass: Passing Simple and Complex Parameters In and Out of Macros

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

Program Validation: Logging the Log

SAS Institute Exam A SAS Advanced Programming Version: 6.0 [ Total Questions: 184 ]

Macros are a block of code that can be executed/called on demand

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

Don't quote me. Almost having fun with quotes and macros. By Mathieu Gaouette

Introduction / Overview

<:ards,. The SAS" Macro: An Aid to the User 3' pfbl:me~~&j1tbc(lpt; 2435 procopt; RESOLVED RESOLVED

Andrew H. Karp Sierra Information Services, Inc. San Francisco, California USA

Customized Flowcharts Using SAS Annotation Abhinav Srivastva, PaxVax Inc., Redwood City, CA

Demystifying PROC SQL Join Algorithms

Introduction to SQL/PLSQL Accelerated Ed 2

Advanced PROC REPORT: Doing More in the Compute Block

Functions vs. Macros: A Comparison and Summary

Regaining Some Control Over ODS RTF Pagination When Using Proc Report Gary E. Moore, Moore Computing Services, Inc., Little Rock, Arkansas

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

STAT:5400 Computing in Statistics. Other software packages. Microsoft Excel spreadsheet very convenient for entering data in flatfile

spin, Rotate, and view Three Dimensional Point Clouds: Dynamic Exploration of sets without SAS/INSIGHT 1H Arthur L.

How to Create Data-Driven Lists

%MISSING: A SAS Macro to Report Missing Value Percentages for a Multi-Year Multi-File Information System

Macros from Beginning to Mend A Simple and Practical Approach to the SAS Macro Facility

Summarizing Impossibly Large SAS Data Sets For the Data Warehouse Server Using Horizontal Summarization

ACT-R RPC Interface Documentation. Working Draft Dan Bothell

%MAKE_IT_COUNT: An Example Macro for Dynamic Table Programming Britney Gilbert, Juniper Tree Consulting, Porter, Oklahoma

DATA Step Debugger APPENDIX 3

Part 1. Getting Started. Chapter 1 Creating a Simple Report 3. Chapter 2 PROC REPORT: An Introduction 13. Chapter 3 Creating Breaks 57

Building and Using User Defined Formats

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

SURVIVING THE SAS MACRO JUNGLE BY USING YOUR OWN PROGRAMMING TOOLKIT

How to Incorporate Old SAS Data into a New DATA Step, or What is S-M-U?

Debugging. Where to start? John Ladds, SAS Technology Center, Statistics Canada.

ABSTRACT DATA CLARIFCIATION FORM TRACKING ORACLE TABLE INTRODUCTION REVIEW QUALITY CHECKS

PharmaSUG 2018 Paper AD-08 Parallel Processing Your Way to Faster Software and a Big Fat Bonus: Demonstrations in Base SAS. Troy Martin Hughes

Chapter 2 REXX STATEMENTS. SYS-ED/ Computer Education Techniques, Inc.

capabilities and their overheads are therefore different.

Oracle Database: Introduction to SQL/PLSQL Accelerated

Contents. About This Book...1

egrapher Language Reference Manual

Essential ODS Techniques for Creating Reports in PDF Patrick Thornton, SRI International, Menlo Park, CA

Easing into Data Exploration, Reporting, and Analytics Using SAS Enterprise Guide

Obtaining the Patient Most Recent Time-stamped Measurements

Anticipating User Issues with Macros

SAS Macro Programming Tips and Techniques

Paper A Simplified and Efficient Way to Map Variable Attributes of a Clinical Data Warehouse

Poster Frequencies of a Multiple Mention Question

The %let is a Macro command, which sets a macro variable to the value specified.

Chapter 2 SYSTEM OVERVIEW. SYS-ED/ Computer Education Techniques, Inc.

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Developing Data-Driven SAS Programs Using Proc Contents

Transcription:

PLA YING WITH MACROS: TAKE THE WORK OUT OF LEARNING TO DO MACROS Arthur L. Carpenter ABSTRACT The macro language can add an additional level of complexity to programs written in SAS~. Macro statements are often complicated and may use logic that appears to defy the rules used by all proper SAS programmers. Regardless of the complexities, learning to use the fundamentals of the macro language need not be difficult This workshop will take users familiar with the basics of the SAS System and introduce them not only to the basics of the macro language, but also to a process for writing their own macros. The course will start with macro variables and will examine their differences and similarities to data set variables. Existing code will then be converted to macros and named and positional parameters will be introduced. Examples will include discussion on the resolution of macro variables and the use of the double ampersand (&&). Several macro programming statements, such as, %IF will be introduced along with selected macro functions. This playshop has been designed to take the work out of learning the SAS macro language. Terminology Macro Text Macro Variable Stored text that contains SAS statements and macro language statements. Treated as character strings in the macro language. It consists of variable names, data set names, or any piece of SAS statements. Consists of a named single-value. It is treated as a text string. Macro Program Statement Controls what actions take place inside the macro processor. They are always preceded by a percent sign (%). Macro Expression One or more macro variable names, text, and/or macro functions combined together by one or more operators and/or parentheses. Macro Function Predefined routines for processing text in macros and macro variables. Operators Symbols that are used for comparisons, logical operation, or arithmetic calculations. Automatic Macro Variable Special-purpose SAS Institute provided system variables. These are reserved and should not be used for anything other than their intended purpose. MACRO VARIABLES Deiming Macro Variables Macro variables (symbolic variables) are very different from SAS data set variables. Data set variables reside as part of the Program Data Vector associated with a given DATA or PROC step. Macro variables are independent of all data sets and do not depend on either the data set or the observation being processed One of the easiest ways to define a macro variable is through the %LET statement. [Macro language statements always start with a %]. %LET dsn = clinics; 289

Using Macro Variables PROC COmENTS DATA=&dsn; TIlLE "DATA SET &dsn"; PROC PRINT DATA=&dsn (OBS=lO); Macro variables appearing inside of a quoted string will not be resolved unless double quotes ( " ) are used. The value of a macro variable may be changed at any time by issuing a new %LET statement. The most recent definition will be used at any given time. References to Macro Variables Suffix - A macro variable may be appended to SAS code including variables and data set names. %LET SEX=MALE; DATA ONLY &SEX; SET CLASS.PATIENTS; WHERE SEX="&SEX"; This resolves to: DATA ONL YMALE; SET CLASS.PATIENTS; WHERE SEX="MALE"; PrefIX - A macro variable may also precede portions of SAS code. A. (period) is used to end the macro variable name. %LET DSN=CLINICS; %LET DSNl =OLDDATA; DATA&DSN; SET &DSN.l &DSNI; This resolves to: DATA CLINICS; SET CLINICSl OLDDATA; A double.. (period) is used when a. (period) is desired in the text. %LET LIBREF=CLASS; DATA &LlBREF.. CLINICS; This will resolve to: DATA CLASS.CLINICS; Morethan one macro parameter may be joined to form a single result. %LET DSN=CLINICS; %LETN=S; %LET DSNS=FRED; Combination &DSN&N &&DSN&N USING MACROS Resolves To CLINICSS FRED Defming A Macro Macros are defined using two macro language statements: %MACRO and %MEND. Often the easiest way to create a macro is to surround existing code with these two statements. Every macro definition must begin and end with these two statements, they come in pairs! %MACRO LOOK; PROC COmENTS DATA=CLINICS; TITLE 'DATA SET CLINICS'; PROC PRINTDATA=CLINICS (OBS=lO); Invoking A Macro Macros are invoked or called by placing a % in front of the macro name. %LOOK 290

Options Debugging a macro can be, under the best of conditions, difficult The LOG is often very cryptic with its isolation oferrormessages. Several options specifically designed for use with macros may be useful during the writing and processing of macros. Options:MPRINT MLOGIC SYMBOLGEN; MPRINT prints the macro code, one statement per line, with macro variable references resolved. MLOGIC traces the macro logic and follows the pattern of execution. %IF statements are resolved to true or false. Macro invocation, start and finish are noted. This option is especially useful for nested macros. SYMBOLGEN causes a message to be printed in the LOG whenever a macro variable is resolved. This option is very useful when tracing macro variable references with multiple & e.g. &&DAT&I. MACRO PARAMETERS Defming Parameters Macros can become more flexible by adding the ability to pass parameters directly into the macro. This avoids getting bogged down in a series of %LET statements. There are two types of parameters positional and keyword (or named). Adding two positional parameters to the macro %LOOK: %MACRO LOOK(dsn,obs); Passing Parameter Values The macro call for %LOOK could be: %LOOK(CLINICS, I 0) All parameters need not be given a value. Alternative invocations of the LOOK macro might include: %LOOKO %LOOK(CLINICS) %LOOK(,IO) Macro variables that are not assigned a value resolve to a null string. Thus the %LOOK(,I 0) would resolve to: PROC CONTENTS DATA=; TITLE "DATA SET "; PROC PRINT DATA= (OBS=IO); The resolved code contains syntax errors and will not run. Care must be taken to construct macro code that will resolve to valid SAS code. The following macro sorts a data set with one to three BY variables. The macro would not be needed or used if there was not at least one BY variable. The macro call resolves to: %MACRO SORTIT(DSN,BYl,BY2,BY3); %MEND SORTIT; %SORTIT(CLINICS,LNAME,FNAME) PROC SORT DATA=CLINICS; BY LNAME FNAME; Keyword parameters may be named in the macro definition, Keyword parameters may be used in any order and may be assigned default values. %MACRO LOOK(dsn=CLlNICS,obs=); 291

Macro variables that are not assigned a value resolve to their default value or to a null string when no default has been specified. Thus the %LOOK(OBS=10) would resolve to: PROGRAM MACROS PROC CONTENTS DATA=CLINICS; TITLE "DATA SET CLINICS"; PROC PRINT DATA=CLINICS (OBS=IO); CONTROL THROUGH Macros That Invoke Macros It is often advantageous to nest macro calls. Consider the two macros %LOOK and %SORTIT, there is one common parameter (&dsn), and it might be nice to have a utility to do both steps at once. %MACRO DOBOTH; %SORTIT(CLINICS,LNAME,FNAME) %LOOK(OBS=IO) %MEND DOBOTH; %MACRO LOOK(dsn=CLINICS,obs=); %MACRO SORTIT(DSN,BY1,BY2,BY3); %MEND SORTIT; The macro call to %DOBOTH will be resolved by SAS as: PROC SORT DATA=CLINICS; BY LNAME FNAME; PROC CONTENTS DATA=CLINICS; TITLE "DATA SET CLINICS"; PROC PRINT DATA=CLINICS (OBS=10); The macro %DOBOTH would be more flexible if the parameters for the macros %SORTIT and %LOOK could be passed directly through %DOBOTH %MACRO DOBOTH(d,o,b 1,b2,b3); %SORTIT(&d,&b 1,&b2,&b3) %LOOK(&d,&o) %MEND DOBOTH; %MACRO LOOK(dsn,obs); %MACRO SORTIT(DSN,BYl,BY2,BY3); %MEND SORTIT; The macro call %DOBOTH(CLINICS,10,LNAME,FNAME) to: PROC SORT DATA=CLINICS; BY LNAME FNAME; PROC CONTENTS DATA=CLINICS; TITLE "DATA SET CLINICS"; resolves PROC PRINT DATA=CLINICS (OBS=lO); Conditional Execution of Macros The %IF-%TIffiN and %ELSE statements are similar to the IF-TIffiN-ELSE statements used in the data step, except the macro %IF statement is not constrained to a data step. The macro %DOBOTII in the previous example will fail with a syntax error if there aren't any BY variables passed. It would be nice if the %sortit macro was only 292

executed if at least one BY variable is not blank. %MACRO DOBOTII(d,o,b 1,b2,b3); %IF &B 1 "= %THEN %SORTIT(&d,&b 1,&b2,&b3); %LOOK(&d,&o) %MEND DOBOTII; Dynamic Code Building With Macros The %IF can be used to conditionally insert SAS code into a program. %MACRO DASTEP(STA TE); DATA SUBHOSP; SET %IF &STATE=CA %THEN CAHOSP; %ELSE AZHOSP; WHEREDATE>'19n.JN91'D; %MEND DASTEP; For %DASTEP(AZ) this resolves to: DATA SUBHOSP; SET AZHOSP WHEREDATE>'19n.JN9I'D; Macro DO Groups The %DO and iterative %DO statements in the macro language are very similar to the corresponding statements used in the data step. Like the %IF, however, these statements are not confined to the data step. They can be used any where inside of a macro. Each form of the %DO statement must be matched with an %END. The simplest form is the %DO block. %MACRO DATASET(V ALUE); %IF & VALUE = 1 %THEN %DO; PROC SORTDATA=PATIENTS; BY SYMPTOM; PROC PRINTDATA=PATIENTS; BY SYMPTOM; %ELSE%DO; PROC FREQ DATA=ICECREAM; TABLES TYPE; %MEND DATASET; The macros %SORTIT and %LOOK could easily be combined into %DOBOTH by using conditional processing with the %DO block. %MACRO DOBOTH(dsn,obs,byl,by2,by3); %IF &BYI "= %THEN%DO; PROC PRINT DATA=&dsn %IF &OBS>O %THEN %DO; (OBS=&obs); TITLE2 "FIRST &obs OBSERVATIONS" %MEND DOBOTH; Iterative %DO The form of the iterative %DO is similar to the DO statement except; the WHILE and UNTIL clauses cannot be added and irregular increments are not allowed. The incremental variable is not a SAS variable but rather a new macro variable. %MACRO ALL YR(START,STOP); %DO YEAR = &START %TO &STOP; SET YR& YEAR; YEAR = 1900 + & YEAR; 293

PROC APPEND BASE=ALL YEAR %MEND ALL YR; The call %ALL YR(88,90) produces: SETYR88; YEAR = 1900 + 88; PROC APPEND BASE=ALL YEAR SETYR89; YEAR = 1900 + 89; PROC APPEND SETYR90; YEAR = 1900 + 90; PROC APPEND BASE=ALL YEAR BASE=ALL YEAR ABOUT THE AUTHOR Art Carpenter's publications list includes the book Quick Results with SAS/GRAP~ ===_ Software, two chapters in Reportingfrom the!:...:.::~;~.~ Field, and over two dozen papers and posters presented at SUGI and WUSS. Art has been using SAS since 1976 and has served as a steering committee chairperson of both the Southern California SAS User's Group, SoCalSUG, and the San Diego SAS Users Group, SANDS; a conference cochair of the Western Users of SAS Software regional conference, WUSS; and Section Chair at the SAS User's Group International conference, SU GI. Art is a SAS Quality Partner and through California Occidental Consultants he teaches SAS courses and provides contract SAS programming support nationwide. Art Carpenter California Occidental Consultants P.O. Box 6199 Oceanside, CA 92058-6199 (760) 945-0613 72212.211@compuserve.com SAS is a registered trademark of SAS Institute, Inc. in the USA and other countries. TM indicates USA registration. SUMMARY Although there is a lot to learn in the macro language, one does not need to know how to %do it all in order to %put %macros to use. Start simple, %ifyou have the chance try new statements, and above all %else have fun. Much of the material in this paper is taken from a book on the SAS macro language which is being written by Art Carpenter and is due to be published in 1998. 294