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

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

Journey to the center of the earth Deep understanding of SAS language processing mechanism Di Chen, SAS Beijing R&D, Beijing, China

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

Essentials of PDV: Directing the Aim to Understanding the DATA Step! Arthur Xuejun Li, City of Hope National Medical Center, Duarte, CA

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

Effectively Utilizing Loops and Arrays in the DATA Step

Understanding and Applying the Logic of the DOW-Loop

Unlock SAS Code Automation with the Power of Macros

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

Simplifying Effective Data Transformation Via PROC TRANSPOSE

Arthur L. Carpenter California Occidental Consultants, Oceanside, California

Introduction. Getting Started with the Macro Facility CHAPTER 1

Tired of CALL EXECUTE? Try DOSUBL

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

SAS Macro. SAS Training Courses. Amadeus Software Ltd

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

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

Sandra Hendren Health Data Institute

DATA Step Debugger APPENDIX 3

CHAPTER 7 Using Other SAS Software Products

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

SAS Macros Advanced Techniques

SURVIVING THE SAS MACRO JUNGLE BY USING YOUR OWN PROGRAMMING TOOLKIT

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

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

Syntax Conventions for SAS Programming Languages

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

Copy That! Using SAS to Create Directories and Duplicate Files

Are you Still Afraid of Using Arrays? Let s Explore their Advantages

Base and Advance SAS

Understanding the Concepts and Features of Macro Programming 1

Ditch the Data Memo: Using Macro Variables and Outer Union Corresponding in PROC SQL to Create Data Set Summary Tables Andrea Shane MDRC, Oakland, CA

New Macro Features Added in SAS 9.3 and SAS 9.4

PROC MEANS for Disaggregating Statistics in SAS : One Input Data Set and One Output Data Set with Everything You Need

Files Arriving at an Inconvenient Time? Let SAS Process Your Files with FILEEXIST While You Sleep

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

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

Keh-Dong Shiang, Department of Biostatistics & Department of Diabetes, City of Hope National Medical Center, Duarte, CA

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

PharmaSUG Paper PO10

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

SAS Macro Programming for Beginners

Developing Data-Driven SAS Programs Using Proc Contents

Paper CC16. William E Benjamin Jr, Owl Computer Consultancy LLC, Phoenix, AZ

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

Quick and Efficient Way to Check the Transferred Data Divyaja Padamati, Eliassen Group Inc., North Carolina.

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

Exploring the SAS Macro Function %SYSFUNC

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

Mapping Clinical Data to a Standard Structure: A Table Driven Approach

Utilizing the Stored Compiled Macro Facility in a Multi-user Clinical Trial Setting

PharmaSUG 2017 Paper BB02

Template Versatility Using SAS Macro Language to Generate Dynamic RTF Reports Patrick Leon, MPH

Tales from the Help Desk 6: Solutions to Common SAS Tasks

Functions vs. Macros: A Comparison and Summary

The GEOCODE Procedure and SAS Visual Analytics

Efficiency Programming with Macro Variable Arrays

More About SAS Macros

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

Anatomy of a Merge Gone Wrong James Lew, Compu-Stat Consulting, Scarborough, ON, Canada Joshua Horstman, Nested Loop Consulting, Indianapolis, IN, USA

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

Anticipating User Issues with Macros

Creating and Executing Stored Compiled DATA Step Programs

Job Security: Using the SAS Macro Language to Full Advantage

An Introduction to SAS Macros

A Macro to Create Program Inventory for Analysis Data Reviewer s Guide Xianhua (Allen) Zeng, PAREXEL International, Shanghai, China

Submitting SAS Code On The Side

Program Validation: Logging the Log

The Power of Combining Data with the PROC SQL

A Tutorial on the SAS Macro Language

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

SAS Macro Language: Reference

SCL Arrays. Introduction. Declaring Arrays CHAPTER 4

9 Ways to Join Two Datasets David Franklin, Independent Consultant, New Hampshire, USA

Using UNIX Shell Scripting to Enhance Your SAS Programming Experience

SAS automation techniques specification driven programming for Lab CTC grade derivation and more

PharmaSUG China Paper 70

Countdown of the Top 10 Ways to Merge Data David Franklin, Independent Consultant, Litchfield, NH

The Power of PROC SQL Techniques and SAS Dictionary Tables in Handling Data

Speed Dating: Looping Through a Table Using Dates

Let Hash SUMINC Count For You Joseph Hinson, Accenture Life Sciences, Berwyn, PA, USA

A SAS Macro to Generate Caterpillar Plots. Guochen Song, i3 Statprobe, Cary, NC

An Efficient Method to Create Titles for Multiple Clinical Reports Using Proc Format within A Do Loop Youying Yu, PharmaNet/i3, West Chester, Ohio

Using SAS Macros to Extract P-values from PROC FREQ

Merging Data Eight Different Ways

PharmaSUG Paper SP04

Contents. Overview How SAS processes programs Compilation phase Execution phase Debugging a DATA step Testing your programs

Using PROC SQL to Calculate FIRSTOBS David C. Tabano, Kaiser Permanente, Denver, CO

Basic Macro Processing Prepared by Destiny Corporation

Advanced Visualization using TIBCO Spotfire and SAS

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

Quality Control of Clinical Data Listings with Proc Compare

Storing and Reusing Macros

Clinical Data Visualization using TIBCO Spotfire and SAS

Let SAS Write and Execute Your Data-Driven SAS Code

Purrfectly Fabulous Feline Functions

ABSTRACT MORE THAN SYNTAX ORGANIZE YOUR WORK THE SAS ENTERPRISE GUIDE PROJECT. Paper 50-30

Paper PO06. Building Dynamic Informats and Formats

DBLOAD Procedure Reference

HOW TO DEVELOP A SAS/AF APPLICATION

Transcription:

PharmaSUG 2017 BB07 Simplifying Your %DO Loop with CALL EXECUTE Arthur Li, City of Hope National Medical Center, Duarte, CA ABSTRACT One often uses an iterative %DO loop to execute a section of a macro repetitively. An alternative method is to utilize the implicit loop in the DATA step with the EXECUTE routine to generate a series of macro calls. One of the advantages in the latter approach is eliminating the needs of using indirect referencing. To better understand the use of the CALL EXECUTE, it is essential for programmers to understand the mechanism and the timing of macro processing to avoid programming errors. These technical issues will be discussed in detail in this paper. INTRODUCTION A common task for writing a macro program is to execute a macro program with different parameters. One often utilizes an iterative %DO statement to loop along the value of the parameters. Using the iterative %DO statement often involves the use of indirect referencing within the loop, which can be difficult to comprehend by a novice programmer. The DATA step EXECUTE routine provides an alternative approach to executing macros without using the %DO statement. The syntax of CALL EXECUTE is as follows: CALL EXECUTE(argument); The argument in CALL EXECUTE can be a text expression that is enclosed in single or double quotation marks, the name of a character variable, or a character expression that is resolved by the DATA step to a macro text expression or a SAS statement. If the argument in CALL EXECUTE produces macro language elements that are enclosed in single quotes, those elements execute immediately during the DATA step execution phase. On the other hand, if the generated macro language elements that are enclosed in double quotes, those elements will execute immediately during the DATA step compilation phase. Single quote notation is commonly used in most applications. If argument in CALL EXECUTE produces SAS language statements, or if the macro language elements from ARGUMENT generate SAS language statements, the SAS language statements will be placed into the input stack as an additional program code and will execute after the end of the current DATA step's execution. AN EXAMPLE BY USING THE %DO STATEMENT Suppose that we have a series of text files, named Atascadero_F.txt, Atascadero_M.txt, LongBeach_F.txt, LongBeach_M.txt, Riverside_F.txt, Riverside_M.txt, etc. The content format for each file is identical. By looking at the file name, you will be able to identify the subjects residential city and gender. For example, Riverside_F.txt contains information for females living in Riverside. Program 1 is a macro program for reading one single text file by providing city name and gender information. Program 1: %macro cr1file(city, gender); data &city&gender; infile "C:\Users\Arthur\Documents\CALL Execute\data\&city._&gender..txt"; input id $ 1-3 race 4 age 6-8; *create variable townname and gender; townname ="&City"; gender = "&gender"; %mend cr1file; %cr1file(atascadero, M) To read multiple files for both genders and then concatenating them together, you can use iterative loops within a macro program, like in Program 2. 1

Program 2: %macro crall(citylist); %let num = %sysfunc(countw(&citylist)); %local i; %do i =1 %to # %let city&i = %scan(&citylist, &i); %cr1file(&&city&i,m); %cr1file(&&city&i,f); %end; data allcitygender; set %do i =1 %to # &&city&i..m &&city&i..f %end; ; %mend crall; %crall(atascadero LongBeach Riverside) In program 2, the first iterative %DO loop is used to call the CR1FILE macro to create an individual file for male and female separately. The second %DO loop in the DATA step, that is nested within the SET statement, is used to stack all the files together in the end. In order to properly use the %DO loop, the indirect referencing is needed to generate references to a series of macro variables. ALTERNATIVE SOLUTION BY USING CALL EXECUTE We can accomplish the same task by using CALL EXECUTE like in Program 3. Instead of passing a list of cities as the parameter value, the names of the city are stored in a SAS dataset, which will be used as the value for calling the macro CRALL_NEW. Program 3: %macro crall_new(citydat); set &citydat; call execute(cats('%cr1file(', city, ',', "M", ')')); call execute(cats('%cr1file(', city, ',', "F", ')')); set &citydat end=last; if _N_ = 1 then call execute('data all; set'); call execute(cats(city, 'M')); call execute(cats(city, 'F')); if last then call execute('; '); %mend crall_new; data city; length city $20; input city; datalines; Atascadero LongBeach Riverside ; %crall_new(city) 2

The first DATA step in the macro program is used to call the CR1FILE macro multiple times, depending upon the number of cities that are stored in the given dataset. The implicit loop from the DATA step, along with the CATS function, generates macro calls that are used in the EXECUTE routine. For example, when the DATA step reads the first observation from &CITYDAT, the value of the DATA step variable CITY contains Atascadero. The CATS function then generates the following macro calls: %cr1file(atascadero, M) %cr1file(atascadero, F) The second DATA step utilizes CALL EXECUTE to generate the final dataset that concatenates all the generated datasets together. That is to say, it generates the code below: data all; set AtascaderoM AtascaderoF LongBeachM LongBeachF RiversideM RiversideF; LIMITATION If you invoke a macro by enclosing the macro call as the argument of CALL EXECUTE, the macro call will execute immediately. If the macro call generates any macro language element, such as the %IF-%THEN statement or macro references, these macro language elements execute immediately. However, any of the SAS language statements that are generated by the macro call will be pushed to the input stack and executed after the end of the current DATA step, which contains CALL EXECUTE. This will create problems if you invoke a macro that contains references for macro variables that are created by CALL SYMPUT(X). CALL SYMPUT(X) is not considered a part of the macro language; instead, it is just DATA step CALL routines. That is to say the macro references to the macro variables created by CALL SYMPUT(X) will execute before they are even created. In Program 4, macro FOO creates a macro variable VALUE by using CALL SYMPUTX. This program illustrates the differences in invoking FOO between using CALL EXECUTE and without using CALL EXECUTE. The use of CALL EXECUTE in both DATA steps avoids use of indirect referencing. Program 4: option mprint mlogic symbolgen; %macro foo; %local value; data bar; a = 5; call symputx('value', a); %put value inside macro foo: &value; %mend; %foo %put value outside macro foo &value; call execute('%foo'); %put value outside macro foo &value; 3

SAS Log from Program 10: 792 %foo MLOGIC(FOO): Beginning execution. MLOGIC(FOO): %LOCAL VALUE MPRINT(FOO): data bar; MPRINT(FOO): a = 5; MPRINT(FOO): call symputx('value', a); MPRINT(FOO): NOTE: The data set WORK.BAR has 1 observations and 1 variables. MLOGIC(FOO): %PUT value inside macro foo: &value value inside macro foo: 5 MLOGIC(FOO): Ending execution. WARNING: Apparent symbolic reference VALUE not resolved. 793 %put value outside macro foo &value; value outside macro foo &value 794 795 796 call execute('%foo'); 797 MLOGIC(FOO): Beginning execution. MLOGIC(FOO): %LOCAL VALUE MPRINT(FOO): data bar; MPRINT(FOO): a = 5; MPRINT(FOO): call symputx('value', a); MPRINT(FOO): MLOGIC(FOO): %PUT value inside macro foo: &value value inside macro foo: MLOGIC(FOO): Ending execution. NOTE: CALL EXECUTE generated line. 1 + data bar; a = 5; call symputx('value', a); NOTE: The data set WORK.BAR has 1 observations and 1 variables. 0.01 seconds 0.01 seconds 798 %put value outside macro foo &value; value outside macro foo 5 When invoking FOO the first time without using CALL EXECUTE, the macro variable VALUE is created and stored in the local symbol table. The VALUE is then deleted from the local symbol table at the end of the macro execution. When using CALL EXECUTE to invoke FOO, the %LOCAL statement executes immediately, which assigns the 4

macro variable VALUE to a NULL value. The DATA step within the macro FOO was pushed to the input stack. The %PUT statement executes next; notice that at this point, VALUE contains a null value. The DATA step that creates BAR executes after the execution of FOO, which creates the macro variable VALUE. Since the macro execution has already ended, the VALUE is then stored in the global symbol table, which is not what you intended. CONCLUSION The use of the EXECUTE routine provides an efficient solution to eliminate the need of using the iterative %DO loop along with indirect referencing in the macro program. However, knowing when best to use CALL EXECUTE and understanding the mechanism of macro processing are essential to writing an accurate macro program. REFERENCES Li, Arthur. Is Your Failed Macro Due To Misjudged Timing? SAS Global Forum 2012 Proceedings. SAS Institute. (2010). SAS 9.4 Functions and CALL Routines Reference. Cary, NC: SAS Institute. Usov, Artur. Call Execute: Let Your Program Run Your Macro. PhUSE 2014 Proceedings. CONTACT INFORMATION Arthur X. Li City of Hope National Medical Center Division of Information Science 1500 East Duarte Road Duarte, CA 91010-3000 Work Phone: (626) 256-4673 ext. 65121 Fax: (626) 471-7106 E-mail: arthurli@coh.org SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. indicates USA registration. Other brand and product names are trademarks of their respective companies. 5