ABSTRACT. Paper CC-031

Similar documents
Calculating Cardinality Ratio in Two Steps

Applications Design and Development, a case study of the EDA Summarize-Each-Variable Suite

Reading a Column into a Row to Count N-levels, Calculate Cardinality Ratio and Create Frequency and Summary Output In One Step

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

SQL Metadata Applications: I Hate Typing

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

A Journeyman s Reference: The Writing for Reading SAS Style Sheet: Tricks, Traps, Tips, and Templates, from SAS-L s Macro Maven

Review of PC-SAS Batch Programming

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

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

Planting Your Rows: Using SAS Formats to Make the Generation of Zero- Filled Rows in Tables Less Thorny

Learn to Please: Creating SAS Programs for Others

Building Intelligent Macros: Using Metadata Functions with the SAS Macro Language Arthur L. Carpenter California Occidental Consultants, Anchorage, AK

%SHOWCOMB: a macro to produce a data set with frequency of combinations of responses from multiple-response data

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

Paper S Data Presentation 101: An Analyst s Perspective

%SYSFUNC - The Brave New Macro World Chris Yindra, C. Y. Training Associates

Dictionary.coumns is your friend while appending or moving data

Paper B GENERATING A DATASET COMPRISED OF CUSTOM FORMAT DETAILS

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

SAS Certification Handout #10: Adv. Prog. Ch. 5-8

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

Uncommon Techniques for Common Variables

Taming a Spreadsheet Importation Monster

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

Arthur L. Carpenter California Occidental Consultants, Oceanside, California

Getting Up to Speed with PROC REPORT Kimberly LeBouton, K.J.L. Computing, Rossmoor, CA

How to Create Data-Driven Lists

A Better Perspective of SASHELP Views

An Easy Route to a Missing Data Report with ODS+PROC FREQ+A Data Step Mike Zdeb, FSL, University at Albany School of Public Health, Rensselaer, NY

Quick Data Definitions Using SQL, REPORT and PRINT Procedures Bradford J. Danner, PharmaNet/i3, Tennessee

Amie Bissonett, inventiv Health Clinical, Minneapolis, MN

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

Get into the Groove with %SYSFUNC: Generalizing SAS Macros with Conditionally Executed Code

Validation Summary using SYSINFO

Efficient Processing of Long Lists of Variable Names

1 Files to download. 3 Macro to list the highest and lowest N data values. 2 Reading in the example data file

Same Data Different Attributes: Cloning Issues with Data Sets Brian Varney, Experis Business Analytics, Portage, MI

%SYSFUNC( BRIDGE TO EXTERNAL DATA

Syntax Conventions for SAS Programming Languages

Useful Tips When Deploying SAS Code in a Production Environment

A SAS Macro Utility to Modify and Validate RTF Outputs for Regional Analyses Jagan Mohan Achi, PPD, Austin, TX Joshua N. Winters, PPD, Rochester, NY

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

Exploring DICTIONARY Tables and SASHELP Views

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

Preserving your SAS Environment in a Non-Persistent World. A Detailed Guide to PROC PRESENV. Steven Gross, Wells Fargo, Irving, TX

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

Using a HASH Table to Reference Variables in an Array by Name. John Henry King, Hopper, Arkansas

Autocall Macros A Quick Overview

e. That's accomplished with:

An Automation Procedure for Oracle Data Extraction and Insertion

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

SAS coding for those who like to be control

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

RUN_MACRO Run! With PROC FCMP and the RUN_MACRO Function from SAS 9.2, Your SAS Programs Are All Grown Up

WHAT ARE SASHELP VIEWS?

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

PROC CATALOG, the Wish Book SAS Procedure Louise Hadden, Abt Associates Inc., Cambridge, MA

SAS Macro Programming Tips and Techniques

SAS Programming Basics

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

Why Is This Subject Important? You Could Look It Up: An Introduction to SASHELP Dictionary Views. What Information is Listed in Dictionary Tables?

Building Sequential Programs for a Routine Task with Five SAS Techniques

Posters. Paper

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

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

The Basics of PROC FCMP. Dachao Liu Northwestern Universtiy Chicago

PhUSE Paper CC07. Slim Down Your Data. Mickael Borne, 4Clinics, Montpellier, France

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

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

A Comparison of the LUA Procedure and the SAS Macro Facility

Tips & Tricks. With lots of help from other SUG and SUGI presenters. SAS HUG Meeting, November 18, 2010

SAS 9 Programming Enhancements Marje Fecht, Prowerk Consulting Ltd Mississauga, Ontario, Canada

Format-o-matic: Using Formats To Merge Data From Multiple Sources

SAS Data Integration Studio Take Control with Conditional & Looping Transformations

Know Thy Data : Techniques for Data Exploration

Using a Control Dataset to Manage Production Compiled Macro Library Curtis E. Reid, Bureau of Labor Statistics, Washington, DC

Efficiency Programming with Macro Variable Arrays

Creation of SAS Dataset

If You Need These OBS and These VARS, Then Drop IF, and Keep WHERE Jay Iyengar, Data Systems Consultants LLC

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

A Hands-on Tour Inside the World of PROC SQL

Mimicking the Data Step Dash and Double Dash in PROC SQL Arlene Amodeo, Law School Admission Council, Newtown, PA

STATION

Macro Magic III. SNUG Presentation (30mins)

Create Metadata Documentation using ExcelXP

SAS Macro. SAS Training Courses. Amadeus Software Ltd

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

The Art of Defensive Programming: Coping with Unseen Data

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

While You Were Sleeping, SAS Was Hard At Work Andrea Wainwright-Zimmerman, Capital One Financial, Inc., Richmond, VA

Cover Your Assumptions with Custom %str(w)arning Messages

Introduction to Data-driven Programming Using SAS

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

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

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

PharmaSUG Paper TT11

Functions and CALL Routines

29 Shades of Missing

PharmaSUG 2017 Paper BB02

Transcription:

Paper CC-031 Using Functions SYSFUNC and IFC to Conditionally Execute Statements in Open Code Ronald J. Fehd, Centers for Disease Control and Prevention, Atlanta, GA, USA ABSTRACT Audience Keywords Information This paper explains how to conditionally execute statements in open code without having to wrap the code in a macro. This is accomplished by combining the macro function sysfunc with the data step function ifc. The result is that parameterized include programs gain the power of conditional processing %if... %then for which macros are usually used. This is another in the Journeymen s Tools series. advanced users and macro programmers. assertions, function, ifc, parameterized include program, sysfunc assertions, conditional processing, parameterized include programs In this paper This paper contains the following topics. Introduction 2 Function IFC 3 Function SysFunc 4 SysFunc and IFC 4 Usage Examples 5 Calling Routines..................... 6 Assertions......................... 8 Conclusion 10 1

INTRODUCTION Overview Program development proceeds through these stages: create ad hoc programs identify programs with similar algorithms consolidate into parameterized include program where necessary, convert to macro for conditional processing The techniques shown here eliminate the need for macros and replace conditional processing with inline statements. Example: %if statement used to conditionally execute blocks of code. %macro DoThis; %if <condition> %then %do; * statements for true; %end; %else %do; * statements for false; %end; run;%mend; %DoThis can be replaced with this: %sysfunc(ifc(<condition>,* statements for true;,* statements for false; )) 2

FUNCTION IFC Data Step Function IFC The ifc function has four parameters: 1. a logical expression 2. character value returned when true 3. value returned when false 4. value returned when missing, which is optional ifc-syntax.txt 1 IFC( logical-expression 2, value-returned-when-true 3, value-returned-when-false 4 <, value-returned-when-missing> 5 ) This example shows the function in a data step. ifc-data.sas 1 DATA TestIfc; 2 attrib N length = 4 3 Value length = $ %length(missing); 4 do N =., 0, 1; 5 Value = ifc(n 6, true 7, false 8, missing 9 ); 10 putlog N= Value=; 11 output; 12 end; 13 stop; 14 run; 15 16 Proc Print data = TestIfc; 1 Obs N Value 2 --- - ------- 3 1. missing 4 2 0 false 5 3 1 true ifc-data.lst 3

FUNCTION SYSFUNC Macro Function SysFunc SysFunc and GetOption The macro function sysfunc provides a method to access certain data step functions and the values of options. This example shows the combination of sysfunc and getoption writing a note to the log with the value of the option source2. put-sysfunc-getoption.log 1 1 options nosource2; 2 2 %Put option source2 is %sysfunc(getoption(source2)); 3 option source2 is NOSOURCE2 4 3 options source2; 5 4 %Put option source2 is %sysfunc(getoption(source2)); 6 option source2 is SOURCE2 SYSFUNC AND IFC Macro Statement PUT with IFC This example contains the previous usage of sysfunc and getoption. Its purpose is to show that the evaluation of the logical expression can produce conditional text. put-sysfunc-ifc.log 1 1 options nosource2; 2 2 %Put option source2 is 3 3 %sysfunc(ifc(%sysfunc(getoption(source2)) eq SOURCE2 4 4,True 5 5,False 6 6,Missing 7 7 ) ); 8 option source2 is False Note that the sysfunc(ifc(...)) is within the %put statement whose ending semicolon is on log line 7. IFC producing PUT The promise of sysfunc(ifc(...)) is to generate statements, based on the evaluation. This example shows the first test: a complete %put statement as the result of the evaluation. sysfunc-ifc-error.log 1 1 options nosource2; 2 2 %sysfunc(ifc(%sysfunc(getoption(source2)) eq SOURCE2 3 3,%Put True; 4 True 5 4,%Put False; 6 False 7 5,%Put Missing; 8 Missing 9 6 )) What happened here? All the macro %put statements were executed! 4

Nrstr of PUT This problem is the same as with call execute; which is explained by Fehd and Carpenter [4], sgf2007.113.the statements must be wrapped in macro function nrstr (No Rescan String) which delays their execution until after the ifc condition has been evaluated. sysfunc-ifc-nrstr.log 1 1 options nosource2; 2 2 %sysfunc(ifc(%sysfunc(getoption(source2)) eq SOURCE2 3 3,%nrstr(%Put True;) 4 4,%nrstr(%Put False;) 5 5,%nrstr(%Put Missing;) 6 6 ) ) 7 False Note that the sysfunc(ifc(...)) is not a statement; the function calls are completed by the two closing parentheses on log line 6. USAGE EXAMPLES Overview These are common tests: boolean: zero, one or greater than one comparison of two values existence Boolean Many functions return either the set of boolean values (0,1) or zero and a negative or positive integer which evaluates as true. 0,1 %sysfunc(cexist(&catalog.)) 0,1, ge 1 &Nobs. -1,0,1, ge 1 %sysfunc(attrn(&dsid.,nobs)) Comparison Any logical expression can be used for the first argument of the ifc function. &NobsData1. eq &NobsData2. &Nobs. gt 100 %sysfunc(fileref(&fileref.)) eq 0 %upcase(%substr(&type.,1,1)) eq C or &Type. eq 2 There are pitfalls when comparing user-supplied values; refer to Fehd [2], sas-wiki.cond- Exec-Global-Stmnts for examples and fixes. 5

Test Suite Each of the following programs has been tested as a parameterized include file, illustrated by this example: assert-cexist-catalog-test.sas 1 options source2; 2 Proc Format library = Work; value Test 0= zero ;run; 3 %Let Catalog = Work.Formats; 4 %Include Project(assert-cexist-catalog); 5 %Let Catalog = Library.Formats; 6 %Include Project(assert-cexist-catalog); Both the program *.sas and its test *-Test.sas are available in a.zip at Fehd [2], saswiki.cond-exec-global-stmnts CALLING ROUTINES Branching with Includes Evaluation of the logical expression can be used to branch to the execution of either of two programs. cond-inc-which.sas 1 * cond-inc-which; 2 options source2; 3 %Let Data = sashelp.class; 4 %*Let Data = sashelp.classx;* test not exist; 5 %let dsid = %sysfunc(open(&data. )); 6 %let Nobs = 7 %sysfunc(ifc(&dsid. 8,%nrstr( %sysfunc(attrn(&dsid.,nobs)); 9 %let rc = %sysfunc(close(&dsid.)); ) 10,%nrstr(0) )); 11 %sysfunc(ifc(&nobs. 12,%nrstr(%Include Project(cond-inc-1);) 13,%nrstr(%Include Project(cond-inc-0);) )) cond-inc-1.sas 1 %Put Note2: file is cond-inc-1; cond-inc-0.sas 1 %Put Note2: file is cond-inc-0; Procedure By Type Evaluation of the logical expression can be used to branch to the execution of either of two sets of statements. The elaborate test of the value of Type ensures that this code can be called with a list processing data set created by either Proc Contents whose Type are in (1,2) and Proc SQL whose Type are in ( char, num ). proc-type.sas 1 title3 "proc-type of &Libname..&Memname.;"; 2 %sysfunc(ifc( %upcase(%substr(&type.,1,1)) eq C 3 or &Type eq 2 4,%nrstr(Proc Freq data = &Libname..&Memname.; 5 tables &Name.;) 6,%nrstr(Proc Means data = &Libname..&Memname.; 7 var &Name.;) )) 8 run; 6

Calling Proc-Type This code is similar to the conditional processing in Fehd [1], sugi25.038, the SmryEachVar data review suite. proc-type-test.sas 1 options source2; 2 %Let Libname = sashelp; 3 %Let Memname = Class; 4 %Let Name = Sex; 5 %Let Type = C; 6 %Include Project(proc-type); 7 8 %Let Name = Height; 9 %Let Type = 1; 10 %Include Project(proc-type); proc-type-caller-sql.sas 1 options source2; 2 %Let Libname = sashelp; 3 %Let Memname = Class; 4 Proc SQL; select catt( %let Name =,Name, ; 5, %let Type =,Type, ; 6, %Include Project(proc-type) ) 7 into :List separated by ; 8 from Dictionary.Columns 9 where Libname eq "%upcase(&libname.)" 10 and Memname eq "%upcase(&memname.)" 11 and Memtype eq DATA ; 12 quit; 13 &List.; proc-type-caller-contents.sas 1 options source2; 2 %Let Libname = sashelp; 3 %Let Memname = Class; 4 Proc Contents data = &Libname..&Memname. 5 noprint 6 out = Work.Contents; 7 DATA _Null_; 8 attrib Stmnt length = $72; 9 do until(endofile); 10 set Work.Contents end = EndoFile; 11 Stmnt = catt( %Let Name =,Name, ; ); 12 link ExecStmt; 13 Stmnt = catt( %Let Type =,Type, ; ); 14 link ExecStmt; 15 Stmnt = %Include Project(proc-type); ; 16 link ExecStmt; 17 end; 18 return; 19 ExecStmt: call execute(catt( %nrstr(,stmnt, ) )); 20 return; 21 stop; 22 run; 7

Number of Rows The attrn function can be used to get the number of observations of a data set. The data set must be opened before the function can return the value. If the data set exists then the function can be applied; otherwise when the open of the data set fails, it is not necessary to close the data set. The values returned by attrn(...,nobs) are in: -1 for a view 0 empty ge 1 number of rows assert-nobs.sas 1 %let dsid = %sysfunc(open(&data. )); 2 %let Nobs = 3 %sysfunc(ifc(&dsid. 4,%nrstr( %sysfunc(attrn(&dsid.,nobs)); 5 %let rc = %sysfunc(close(&dsid.)); ) 6,%nrstr(0) )); 7 %sysfunc(ifc(&nobs. 8,%nrstr(%Put Note2: Data &Data. available Nobs: &Nobs.;) 9,%nrstr(%Put %sysfunc(sysmsg()); 10 endsas;) )) This may seem strange, but minus one evaluates as true. 21 DATA Work.ClassView / view = Work.ClassView; 22 set sashelp.class(obs=1); 23 run; NOTE: DATA STEP view saved on file WORK.CLASSVIEW. 24 %Let Data = Work.ClassView; 25 %Include Project(assert-nobs); Note2: Data Work.ClassView available Nobs: -1 ASSERTIONS Concept Assertions are tests at the beginning of a program that determine whether the program input parameters are valid. The assertion of existence follows the rules of simple boolean evaulation: the functions return zero or one. Example of Assertion Usage * name: MyProgram; * confirm input exists; %let Object = ObjectName; %Include Asserts(ExistObject);* false==endsas; *processing; Exist Catalog The cexist function can be used to test the existence of a catalog. assert-cexist-catalog.sas 1 %sysfunc(ifc(%sysfunc(cexist(&catalog.)) 2,%nrstr(%Put Note2: Catalog &Catalog. exists;) 3,%nrstr(%Put Note3: not exist &Catalog.; 4 endsas;) )) 8

Exist Data The exist function can be used to test whether a data set exists. assert-exist-data.sas 1 %sysfunc(ifc(%sysfunc(exist(&data.)) 2,%nrstr(%Put Note2: Data &Data. exists;) 3,%nrstr(%Put Note3: not exist &Data.; 4 endsas;) )) Exist Fileref The fexist function can be used to test the existence of a fileref which is a directoryspecification or folder. assert-fexist-fileref.sas 1 %sysfunc(ifc(%sysfunc(fexist(&fileref.)) 2,%nrstr(%Put Note2: FileRef &FileRef. exists;) 3,%nrstr(%Put Note3: not exist &FileRef.; 4 endsas;) )) Exist File The fileexist function can be used to test the existence of a file-specification. assert-fileexist-filename.sas 1 %sysfunc(ifc(%sysfunc(fileexist(&filenameext.)) 2,%nrstr(%Put Note2: FileNameExt &FileNameExt. exists;) 3,%nrstr(%Put Note3: not exist &FileNameExt.; 4 endsas;) )) Exist Fileref and File The fileref function is a special case. Instead of returning one as success and zero as failure, it returns this set of values: -1 fileref exists but file associated with the fileref does not exist 0 fileref and external file both exist +1 fileref not assigned Thus success is defined as eq 0! assert-fileref.sas 1 %sysfunc(ifc(%sysfunc(fileref(&fileref.)) eq 0 2,%nrstr(%Put Note2: FileRef &FileRef. exists;) 3,%nrstr(%Put Note3: not exist &FileRef.; 4 endsas;) )) For an example of usage of function fileref see the OnLine Doc for function finfo. Summary of Assertion Usage Each of these examples show the failure as sufficient to terminate endsas; the program. In practice consider adding other programs which provide information written to a log or data set. Nelson et al. [6], sesug2003.001 discuss a system of event management where subroutines log program completion and e-mail notifications of failure are sent to interested users. 9

Conclusion Further Reading The combination of functions sysfunc and ifc can be use to reduce the need for macros where only conditional processing is needed. Assertions Nelson et al. [6], sesug2003.001 describe assertions in an event management and notification system. Call Execute and Nrstr Fehd and Carpenter [4], sgf2007.113 demonstrate the problem of using call execute to generate macro calls and how using function nrstr solves that problem. Conditional Executions of Includes Fehd [1], sugi25.038 provides a trick for conditional execution of included programs. Macro IFF Henderson [5], saspress.60282 provides a macro to get around the problem of user-supplied values in comparisons. Using Parameterized Includes Fehd [1], sugi25.038 provides a data review suite of 80 programs with modules, routines and subroutines of parameterized includes. REFERENCES [1] Ronald Fehd. The Writing for Reading SAS Style Sheet: Tricks, traps, tips, and templates, from SAS-L s Macro Maven. In Proceedings of the 25th SAS User Group International Conference, 2000. URL http://www2.sas. com/proceedings/sugi25/25/ad/25p038.pdf. Coders Corner, 4 pp.; topics: avoiding common mistakes when writing macro statements, recommendations; info: conditional execution of includes. [2] Ronald J. Fehd. Conditionally executing global statements. In SAS Community Wikipedia, 2008. URL http:// www.sascommunity.org/wiki/conditionally_executing_global_statements. topics: combining functions sysfunc and ifc; info: example assertions, caveats on logical comparisons. [3] Ronald J. Fehd. SmryEachVar: A data-review routine for all data sets in a libref. In Proceedings of the SAS Global Forum Annual Conference, 2008. URL http://www2.sas.com/proceedings/forum2008/003-2008.pdf. Applications Development, 24 pp.; topics: data review; info: utilities to repair missing elements in data structure. [4] Ronald J. Fehd and Art Carpenter. List processing basics: Creating and using lists of macro variables. In Proceedings of the SAS Global Forum Annual Conference, 2007. URL http://www2.sas.com/proceedings/ forum2007/113-2007.pdf. Hands On Workshop, 20 pp. including examples; topics: arrays of macro variables; info: using call execute and nrstr to call macros, testing. [5] Don Henderson. Building Web Applications with SAS/IntrNet: A Guide to the Application Dispatcher. SAS Institute, 2006. URL http://www.sas.com/apps/pubscat/bookdetails.jsp?catid=1&pc=60282. ISBN 978-1-59994-189-9. [6] Greg Barnes Nelson, Danny Grasse, and Deborah Pine. Automated testing and real-time event management. In Proceedings of the SouthEast SAS User Group Conference, 2003. URL http://www.thotwave.com/ Document/papers/SESUG_03/EventSys/eventmngmt.pdf. 6 pp. topics: testing, definition of events, batch scheduler, program agent, execution monitor, real-time notification. Author: Ronald Fehd Centers for Disease Control 4770 Buford Hwy NE Atlanta GA 30341-3724 mailto:rjf2@cdc.gov The code examples in this paper are available on the web at Fehd [2], sas-wiki.cond-exec-global-stmnts 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. R indicates USA registration. 10