Exploring the SAS Macro Function %SYSFUNC

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

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

Introduction. Getting Started with the Macro Facility CHAPTER 1

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

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

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

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

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

SAS Macro. SAS Training Courses. Amadeus Software Ltd

Functions vs. Macros: A Comparison and Summary

A Format to Make the _TYPE_ Field of PROC MEANS Easier to Interpret Matt Pettis, Thomson West, Eagan, MN

Submitting SAS Code On The Side

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

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

An Efficient Tool for Clinical Data Check

The 'SKIP' Statement

Using Macro Functions

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

SAS Drug Development. SAS Macro API 1.3 User s Guide

New Macro Features Added in SAS 9.3 and SAS 9.4

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

Paper HOW-06. Tricia Aanderud, And Data Inc, Raleigh, NC

Storing and Reusing Macros

SAS Macro Language: Reference

DATA Step Debugger APPENDIX 3

Tired of CALL EXECUTE? Try DOSUBL

Program Validation: Logging the Log

CC13 An Automatic Process to Compare Files. Simon Lin, Merck & Co., Inc., Rahway, NJ Huei-Ling Chen, Merck & Co., Inc., Rahway, NJ

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

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

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

Understanding the Concepts and Features of Macro Programming 1

Create Metadata Documentation using ExcelXP

SAS Macros Advanced Techniques

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

Using Recursion for More Convenient Macros

The Power of Combining Data with the PROC SQL

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

SDTM Attribute Checking Tool Ellen Xiao, Merck & Co., Inc., Rahway, NJ

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

When Powerful SAS Meets PowerShell TM

APPENDIX 2 Customizing SAS/ASSIST Software

Macro Architecture in Pictures Mark Tabladillo PhD, marktab Consulting, Atlanta, GA Associate Faculty, University of Phoenix

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

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

MACRO REFERENCING ENVIRONMENTS

Using Templates Created by the SAS/STAT Procedures

WHAT ARE SASHELP VIEWS?

SAS Model Manager 15.1: Quick Start Tutorial

Displaying Multiple Graphs to Quickly Assess Patient Data Trends

Posters. Workarounds for SASWare Ballot Items Jack Hamilton, First Health, West Sacramento, California USA. Paper

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

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

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

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

Geocoding Crashes in Limbo Carol Martell and Daniel Levitt Highway Safety Research Center, Chapel Hill, NC

A Mass Symphony: Directing the Program Logs, Lists, and Outputs

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

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

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

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

Clone an Existing Drive

Exporting Variable Labels as Column Headers in Excel using SAS Chaitanya Chowdagam, MaxisIT Inc., Metuchen, NJ

A Tool to Reduce the Time Used in the Preparation of the Statistical Review Aid Used for Electronic Submission

An Introduction to SAS/SHARE, By Example

Basic Macro Processing Prepared by Destiny Corporation

Tips for Mastering Relational Databases Using SAS/ACCESS

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

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

SAS Viya 3.2 Administration: Licensing

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

Better Metadata Through SAS II: %SYSFUNC, PROC DATASETS, and Dictionary Tables

Automation of makefile For Use in Clinical Development Nalin Tikoo, BioMarin Pharmaceutical Inc., Novato, CA

Virtual Accessing of a SAS Data Set Using OPEN, FETCH, and CLOSE Functions with %SYSFUNC and %DO Loops

PharmaSUG Paper AD03

What Do You Mean My CSV Doesn t Match My SAS Dataset?

HOW TO DEVELOP A SAS/AF APPLICATION

SAS Viya 3.2 Administration: External Credentials

Make it a Date! Setting up a Master Date View in SAS

ABSTRACT: INTRODUCTION: WEB CRAWLER OVERVIEW: METHOD 1: WEB CRAWLER IN SAS DATA STEP CODE. Paper CC-17

The GEOCODE Procedure and SAS Visual Analytics

The SAS Interface to REXX

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

Data Grids in Business Rules, Decisions, Batch Scoring, and Real-Time Scoring

A Tutorial on the SAS Macro Language

Unravelling the Knot of Ampersands

Making the most of SAS Jobs in LSAF

Jeff Phillips, ARC Professional Services Group Veronica Walgamotte, ARC Professional Services Group Derek Drummond, ARC Professional Services Group

Data Representation. Variable Precision and Storage Information. Numeric Variables in the Alpha Environment CHAPTER 9

A Better Perspective of SASHELP Views

SOPHISTICATED DATA LINKAGE USING SAS

PharmaSUG Paper SP04

Introduction. LOCK Statement. CHAPTER 11 The LOCK Statement and the LOCK Command

Creating and Executing Stored Compiled DATA Step Programs

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

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

Programming Gems that are worth learning SQL for! Pamela L. Reading, Rho, Inc., Chapel Hill, NC

Amie Bissonett, inventiv Health Clinical, Minneapolis, MN

from the source host, use the FTP put command to copy a file from the source host to the target host.

A Juxtaposition of Tables and Graphs Using SAS /GRAPH Procedures

Transcription:

Paper CC11 Exploring the SAS Macro Function %SYSFUNC Lin Yan and Helen Wang Department of Scientific Programming Merck Research Labs, Merck & Co., Inc. Rahway, New Jersey 07065 ABSTRACT The SAS macro function %SYSFUNC is very useful because it makes possible the use of SAS data step functions in the SAS macro environment. However, when the argument in the nested SAS data step function contains a macro variable or another macro such as %sysfunc(substr(&&ab&c,1)), it is hard to interpret the result without an understanding of the functionalities of %SYSFUNC and the interaction of %SYSFUNC with the nested SAS functions. Through a series of tests, the authors explored the functionalities of %SYSFUNC and the working order of %SYSFUNC with the nested SAS data step functions. The macro variable in a data step function is resolved first, then the data step function starts to work on the resolved value of the macro variable, and finally, %SYSFUNC resolves macros or macro variables in the output of data step function - but only once. INTRODUCTION The SAS macro function %SYSFUNC facilitates the use of SAS data step functions in the SAS macro environment saving a lot of time and effort. However, when the argument in the nested SAS function contains macro variables or other macros, quite often questions are asked about the mechanism behind the combinations, for example: What is the working order of %SYSFUNC with the nested SAS data step functions? Is a macro variable resolved before or after it is fed into the SAS data step function? If the output from the data step function contains a macro variable, when is it resolved? If the macro variable in the argument of a data step function is associated with another macro, how is it passed into the function? If the argument in the nested SAS function contains several macro variables or macros, can they be resolved to plain text? This paper will address the above questions through examples. WORKING ORDER OF %SYSFUNC WITH NESTED SAS DATA STEP FUNCTIONS 1. If the argument of %SYSFUNC is a data step function and if the argument of the data step function has a macro variable, the macro variable is resolved first. Next, the resolved value is fed into the argument of SAS data step function. Finally, the data step function starts execution. In the following example, macro variable A is resolved to string TEST first; then the string TEST is fed into a data step function SUBSTR; finally, the data step function executes. Therefore, the output is Y=TEST. %let A=TEST; %put Y = %sysfunc(substr(&a,1));

** The resolved value of Y in SAS log is: Y = TEST; 2. If the output of a data step function contains a macro variable, the macro variable is resolved when the output is nested within %SYSFUNC. In the sample code below, macro variable X is assigned value &A; the output from the data step function SUBSTR is &A; %SYSFUNC resolves it to TEST: %let A=TEST; %let X=%nrstr(&A); ** The resolved value of Y in SAS log is: Y = TEST; 3. Function %SYSFUNC resolves macro variables or macros after the execution of the data step functions and is illustrated in the following sample codes: %let T=DATE(); %let Y=%nrstr(&T); %put Z=%sysfunc(substr(&Y, 1, 2)); ** The resolved value of Z in SAS log is: Z = DATE(); In the second line of the above sample code, macro variable Y is stored as &T, which is also the output from SUBSTR in the third line. The %SYSFUNC resolves the output from function substr(&y, 1, 2), which is &T, to DATE(). This resolution is performed after the execution of the data step function SUBSTR. Therefore, the result from %sysfunc(substr(&y, 1, 2)) is DATE() rather than DA. The next example shows that %SYSFUNC resolves the macro variable in the end if the output from the data step function that is called by %SYSFUNC contains a macro variable. The order of the process can be more clearly illustrated by the following more complicated example code: %macro proca; & %mend proca; %macro procb; D %mend procb; %let D = TEST; %let C=%nrstr(%procB); %let X=%str(%procA&C); What can we realize from the following code? %put Y1 = %sysfunc(substr(&x, 1)); %put Y3 = %sysfunc(substr(&x, 3));

Macro variable X is stored in macro symbol table as &%procb which is passed to SUBSTR. The output of substr(&x, 1), &%procb, is resolved by %SYSFUNC to &D which is the result of Y1. As the output from substr(&x, 3) is procb which contains no macro variable or macro, the result of Y3 is procb. We can test the process further from the output as follows: ** In the code below, X = &%procb and the result of %procb is D; %put Y11 = %sysfunc(substr(&x, 1, 1)); ** The result from SUBSTR function is & and Y11 = &; %put Y21 = %sysfunc(substr(&x, 1, 3)); ** There is a message in log: WARNING: Apparent invocation of macro P not resolved.y21 = &%p; %put Y31 = %sysfunc(substr(&x, 1, 7)); ** The result from SUBSTR function is &%procb and Y31 = &D; THE RESOLUTION OF MACRO VARIABLES IN THE NESTED SAS FUNCTIONS 1. The values of macro variables in the arguments of a data step function depend on the directly stored value. If a stored macro variable is associated with another macro variable, the stored value of the macro variable in argument is fed into the data step function. For example, assign macro variable T to the value of DATE() and macro variable X to the value of &T, then the stored value of X is DATE(). The output from %sysfunc(&x, date7.) is the date of the execution date, say 04NOV07. If X is assigned as %nrstr(&t), the stored value of X is &T which is passed to %SYSFUNC. Sine &T is a character string not a date, %SYSFUNC looks for a data step function instead. When %SYSFUNC fails to find a data step function in its argument, SAS issues an error message in the log file. We can see the difference from the code below: %let T=DATE(); %let X=&T; ** The stored value of X is DATE(); %put %sysfunc(&x, date7.); ** The result is 04NOV07; %let Z=%nrstr(&T); ** The stored value of Z is Z=&T; %put %sysfunc(&z, date7.); ** ERROR: Function name missing in %SYSFUNC or %QSYSFUNC macro function reference. The code below clearly explains how and when a macro variable in a data step function called by %SYSFUNC is resolved. %macro proca;

& %mend proca; %macro procb; D %mend procb; %let D=TEST; ** if C is masked by %NRSTR; %let C=%nrstr(%procB); ** The stored value of C is %procb; %let X=%nrstr(%procA&C); ** The stored value of X is %proca&c; ** The resolved value in SAS log is: Y= &%procb; ** if C is unmasked by %STR; %let X=%str(%procA&C); ** The stored value of X is &%procb; ** The resolved value in SAS log is: Y= &D; 2. Function %SYSFUNC resolves the macro variable in the output of the data step function once, and not to the end of plain text. Let us continue from the example above. If we reset X = %nrstr(%proca%procb), the output from statement substr(&x, 1) is %proca%procb, then %SYSFUNC resolves %proca and %procb to & and D respectively. The final result is &D, not the value of macro variable D, TEST. %let X=%nrstr(%procA%procB); **The stored value of X is %proca%procb; ** The resolved value in SAS log is: Y = &D, not TEST; Function %SYSFUNC resolves all macro variables no matter whether macro variables are masked with %NRBQUOTE or not. Function %NRBQUOTE cannot stop %SYSFUNC from resolving the macro variables in data step function called by %SYSFUNC. In the sample below, X = %proca%procb which has a value of TEST. The output from %sysfunc(substr(%nrbquote(&x),1, 2)) is TE not TEST as X is resolved as TEST before it is fed into SUBSTR. %let X=%procA%procB; ** The stored value of X is X = TEST; %put Y = %sysfunc(substr(%nrbquote(&x),1, 2)); ** The resolved value in SAS log is: Y = TE;

3. Function %QSYSFUNC can prevent the macro variables in the output of data set function from being resolved. If there is a need to mask some characters or mnemonics, use %QSYSFUNC. For example, if X is assigned %proa%prob, then output from statement %qsysfunc(substr(&x,1)) is %proca%procb. %let X=%nrstr(%procA%procB); %put Y = %qsysfunc(substr(&x,1)); ** The resolved value in SAS log is: Y = %proca%procb; CONCLUSIONS From the examples above, we know that first the macro variable in a data step function is resolved according to the directly stored value of the macro variable. Function %SYSFUNC resolves macro variables or macros, if any, after the data step function executes. %SYSFUNC will resolve any macro variable or macro in the output of the data step function only once. When there is a need to mask some characters or mnemonics, %QSYSFUNC must be used. ACKNOWLEDGEMENT Authors thank the support from the Department of Scientific Programming Rahway Site, Merck Co., & Inc. REFERENCES SAS Institute (1999) Software: Reference, Version 8, Copyright 1999 by SAS Institute Inc., Cary, NC, USA CONTACT INFORMATION Your comments and questions are valuable and appreciated. Authors can be reached at Lin Yan Merck & Co., Inc. Rahway, NJ 07065 U.S.A. Tel: 732-594-5949 Email: lin_yan2@merck.com Helen Wang Merck & Co., Inc. Rahway, NJ 07065 U.S.A. Email: Helen_wang@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.