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

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

/ * %STR(argument) %NRSTR(argument)

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

Dequote me on that: Using the dequote function to add some friendliness to SAS macros

Basic Macro Processing Prepared by Destiny Corporation

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

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

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

Introduction. Getting Started with the Macro Facility CHAPTER 1

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

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

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

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

Using GSUBMIT command to customize the interface in SAS Xin Wang, Fountain Medical Technology Co., ltd, Nanjing, China

SAS Macro Language: Reference

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

SAS Programming Techniques for Manipulating Metadata on the Database Level Chris Speck, PAREXEL International, Durham, NC

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

Understanding the Concepts and Features of Macro Programming 1

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

SAS Macro Programming for Beginners

PharmaSUG China Paper 70

WHAT ARE SASHELP VIEWS?

Sending Text Messages from SAS

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

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

Macro Language Dictionary

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

Exploring the SAS Macro Function %SYSFUNC

Using Macro Functions

Arthur L. Carpenter California Occidental Consultants, Oceanside, California

How to validate clinical data more efficiently with SAS Clinical Standards Toolkit

Efficiency Programming with Macro Variable Arrays

General Methods to Use Special Characters Dennis Gianneschi, Amgen Inc., Thousand Oaks, CA

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

Chapter 2, Part I Introduction to C Programming

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

Quality Control of Clinical Data Listings with Proc Compare

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

PharmaSUG 2013 CC26 Automating the Labeling of X- Axis Sanjiv Ramalingam, Vertex Pharmaceuticals, Inc., Cambridge, MA

ODS TAGSETS - a Powerful Reporting Method

Basic SAS Hash Programming Techniques Applied in Our Daily Work in Clinical Trials Data Analysis

SAS Macro. SAS Training Courses. Amadeus Software Ltd

Job Security: Using the SAS Macro Language to Full Advantage

Getting it Done with PROC TABULATE

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

Using SAS software to shrink the data in your applications

Using SAS Macros to Extract P-values from PROC FREQ

PharmaSUG Paper PO10

Unravelling the Knot of Ampersands

Amie Bissonett, inventiv Health Clinical, Minneapolis, MN

STEP 1 - /*******************************/ /* Manipulate the data files */ /*******************************/ <<SAS DATA statements>>

Introduction to the SAS Macro Facility

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

SAS Macros Advanced Techniques

Create a Format from a SAS Data Set Ruth Marisol Rivera, i3 Statprobe, Mexico City, Mexico

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

The Path To Treatment Pathways Tracee Vinson-Sorrentino, IMS Health, Plymouth Meeting, PA

Using PROC REPORT to Cross-Tabulate Multiple Response Items Patrick Thornton, SRI International, Menlo Park, CA


UNIT-IV: MACRO PROCESSOR

PharmaSUG China

How to Create Data-Driven Lists

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

The Ins and Outs of %IF

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 Kevin Russell, SAS Institute Inc., Cary, North Carolina

FSEDIT Procedure Windows

DATA Step Debugger APPENDIX 3

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

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

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

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

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

BreakOnWord: A Macro for Partitioning Long Text Strings at Natural Breaks Richard Addy, Rho, Chapel Hill, NC Charity Quick, Rho, Chapel Hill, NC

Greenspace: A Macro to Improve a SAS Data Set Footprint

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

Alias Macros, a Flexible Versioning System for Standard SAS Macros

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

Using ANNOTATE MACROS as Shortcuts

SAS Drug Development. SAS Macro API 1.3 User s Guide

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

PharmaSUG China. Systematically Reordering Axis Major Tick Values in SAS Graph Brian Shen, PPDI, ShangHai

Automated Checking Of Multiple Files Kathyayini Tappeta, Percept Pharma Services, Bridgewater, NJ

Your Own SAS Macros Are as Powerful as You Are Ingenious

PharmaSUG Paper TT11

Producing Summary Tables in SAS Enterprise Guide

Copy That! Using SAS to Create Directories and Duplicate Files

Taming a Spreadsheet Importation Monster

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

A Generalized Macro-Based Data Reporting System to Produce Both HTML and Text Files

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

1.7 Limit of a Function

Statistics, Data Analysis & Econometrics

ODS/RTF Pagination Revisit

Using Data Set Options in PROC SQL Kenneth W. Borowiak Howard M. Proskin & Associates, Inc., Rochester, NY

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

%ANYTL: A Versatile Table/Listing Macro

A Methodology for Truly Dynamic Prompting in SAS Stored Processes

Creating Your Own Worksheet Formats in exporttoxl

Transcription:

PharmaSUG China 2016 - Paper 81 Macro Quoting: Which Function Should We Use? Pengfei Guo, MSD R&D (China) Co., Ltd., Shanghai, China ABSTRACT There are several macro quoting functions in SAS and even some experienced programmers can't distinguish them exactly. We usually look up the SAS help document when encountering some log issue during using. This article will summarize the difference and list some fallible situations. At the end, we also provide a step-wise method to help user make easy decision. INTRODUCTION The macro language is character based. Every word has meaning to the macro processor. But sometimes, the processor needs to deal with some special characters which could make the processor confused. These special characters are not only simple texts but also functions in SAS program. The purpose of the macro quoting functions is resolving these ambiguities by masking these special characters. We have 7 functions as blew. %STR/%NRSTR/%QUOTE/%NRQUOTE/%BQUOTE/%NRBQUOTE/%SUPERQ HOW TO CHOOSE AN APPROPRIATE MACRO? In summaries, the special characters can divided into the following types. Type I: Operators + - * / < > = ^ ~ Mnemonics AND OR NOT EQ NE LE LT GE GT IN Miscellaneous blank, ; () # Type II: Unmatched Characters ( ) Type III: Macro Triggers & % Blew Table 1 is a summary of these functions. means this function can deal with this type of characters. Table 1 Macro quoting functions summary Type I Type II Type III %STR marked with a % sign %NRSTR marked with a % sign %QUOTE marked with a % sign %NRQUOTE marked with a % sign %BQUOTE %NRBQUOTE %SUPERQ Does it seem too complex? You could choose the appropriate function by check the 2 following questions: WHEN TO USE A MACRO QUOTING FUNCTION DO YOU NEED AMPERSANDS AND PERCENT SIGNS BE RESOLVED? 1

WHEN TO USE A MACRO QUOTING FUNCTION There are 2 categories for the macro quoting functions. The first is compilation functions including %STR/%NRSTR. It applies to open code such as %LET and the compiled program between %macro statement and %mend statement. In this situation, you need use %STR/%NRSTR for macro quoting, otherwise you may get a warning or error message from SAS log window. Take blew sample code 1 for example. %let name = %nrbquote(tom&jerry); %let footer1 = %nrbquote(%percent = n/n); Log will put warning as blew. How is macro quoting functions working? During the compilation period, the word scanner will check the code and translate them into a low-level pseudo code language. The macro processor masks special characters by adding prefix and suffix to the string with a hexadecimal character, which called delta character. The %QUOTE/%NRQUOTE/%BQUOTE/%NRBQUOTE will be treated as normal text in the compilation period. So the special characters will cause some ambiguities. The other type is execution functions. In the execution period, the SAS processor executes compiled macro program instructions. This type includes %QUOTE/%NRQUOTE/%BQUOTE/%NRBQUOTE. They make the macro processor to treat special characters that result from resolving a macro expression as text. The %STR/%NRSTR also won t mask special characters during this process. DO YOU NEED AMPERSANDS AND PERCENT SIGNS BE RESOLVED? If you don t want the macro triggers (& or %) to resolve a macro variable or call a macro, then you should use the functions which use NR for prefix. Here NR means Not Resolved. The sample code 2 blew will show the difference between Resolved and Not Resolved. In the first part, the macro variable name will be resolved to Tom&Jerry defined before. In the second part, it is not resolved and put as text directly. In summaries, you can use the following Figure 1 to help you do the decision. Figure 1 Decision Process to Find Proper Function 2

We don t choose %QUOTE and %NRQUOTE here because they can be replaced by %BQUOTE and %NRBQUOTE totally without using the preceding percent sign (%). SPECIAL USAGE Besides the normal usage of macro quoting functions above, there are some especial situations we need take care of. Here are some examples. WHERE TO USE EXECUTION FUNCTIONS? Although the SAS does not specify where you could use the execution functions to resolve macro variable or recall macro. You can either use them inside a macro for a macro parameter or in the invoking step for assigning the parameter. I suggest using them inside a macro in case of any unexpected value for good programming practice. Sometimes, it is not enough. You also need add extra functions to deal with some special situations. In the sample code 3, it will show some situations. %macro sample3(param1=,param2=,param3=); %if %bquote(&param1.) ne %then %put param1 is valid; %if %bquote(&param2.) ne %then %put param2 is valid; %if %bquote(&param3.) ne %then %put param3 is valid; %mend sample3; %*parameter lists a; %sample3(param1=%nrbquote(tom&jerry),param2= Monsters, Inc,param3=Schindler's List); %* use blew code to jump the loop; ;*';*";*/;quit;run; %*parameter lists b; %sample3(param1=%nrstr(tom&jerry),param2=%bquote( Monsters, Inc),param3=%bquote(Schindler's List)); For the lists a, Tom&Jerry is set to param1 which is not compiled before, so will cause a warning. The part after comma in param2 will add a parameter which is not defined in macro statement and this leads an error. In the last parameter-param3, the single quotation sign will mask the test after itself causing unexpected results. The log is shown as blew. After being changed to the list b, everything is run correctly. UNEXPECTED UNQUOTING The special characters under macro quoting functions will be masked until encounter a %UNQUOTE function or passed to next DATA step compiler, PROC step, SAS macro facility or other part of SAS system. However, it is unquoted when you use the %SCAN/%SUBSTR/%UPCASE. This may cause unexpected issue. Please see blew sample code 4 for an example. %let footer1 = %str(n=number of subjects who have AEs; N=number of subjects in the analysis population); %put footer1 = %upcase(&footer1.); The log will show an error caused by the first semicolon. 3

You can use %QSCAN/%QSUBSTR/%QUPCASE instead of the above functions. USING %SUPERQ TO AVOID WARNING Maybe you have noticed that we haven t mentioned about the %SUPERQ function. It is some kind like %NRBQUOTE. It quotes the first level value of that macro variable in itself without permitting any resolution to occur. It does not mean the variable is not resolved. It means that nothing in the value of that variable will be resolved. You should omit the leading ampersand when using this function. The argument of %SUPERQ is assumed to be the name of a macro variable. This means that if the argument contains an ampersand it will be resolved first and the result is assumed to be the name of a macro variable. DATA null; call symput("name","tom&jerry"); run; %*Part A - With warning in log; %put name = &name.; %*Part B - Without warning in log by using %superq; %let newname = %superq(name); %put name = &newname.; In the part A, the macro &name is resolved into Tom&Jerry first. Then the processer attempts to resolve the &Jerry. But it is not defined before. So it will put warning in log as blew. In the part B, the macro name will be translated into Tom&Jerry then treated as text forever. The value Tom&Jerry will never be resolved to deeper level. CONCLUSION Although we have 7 macro quoting functions, %STR/%NRSTR/%BQUOTE/%NRBQUOTE can deal with most of purpose. If you want to mask the special characters from program to compiler, then using the %STR/%NRSTR functions. Otherwise from compiler to executable code, using %BQUOTE/%NRBQUOTE. If you want the trigger will not be resolved, using the NR prefixed functions. REFERENCES Rosenbloom, Mary F. O.; Carpenter, Art. 2013. Macro Quoting to the Rescue: Passing Special Characters. Proceedings of the SAS Global Forum 2013. San Francisco, California. O Connor, Susan. 2003. Secrets of Macro Quoting Functions How and Why. Proceedings of the Twelfth Annual Northeast SAS User Group Conference. Washington, DC. Whitlock, Ian. 2003. A Serious Look at Macro Quoting. Proceedings of the Twenty-Eighth Annual SAS User Group International Conference, Seattle. WA. Patterson, Brian; Remigio, Mylene. 2007. Don t %QUOTE() Me on This: A Practical Guide to Macro Quoting Functions Proceedings of the SAS Global Forum 2007. Orlando, Florida 4

CONTACT INFORMATION Your comments and questions are valued and encouraged. Contact the author at: Name: Pengfei Guo Enterprise: MSD R&D (China) Co., Ltd. Address: Building A, Headquarters Park, Phase 2, 1582 Gumei Road, Xuhui District City, State ZIP: Shanghai, 200233 Work Phone: +86 21 2211 8543 Fax: +86 21 2211 8899 E-mail: peng.fei.guo1@merck.com 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