Get Funky with %SYSFUNC



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

Programming Idioms Using the SET Statement

Managing very large EXCEL files using the XLS engine John H. Adams, Boehringer Ingelheim Pharmaceutical, Inc., Ridgefield, CT

Obtaining A List of Files In A Directory Using SAS Functions

Post Processing Macro in Clinical Data Reporting Niraj J. Pandya

A Recursive SAS Macro to Automate Importing Multiple Excel Worksheets into SAS Data Sets

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

Writing cleaner and more powerful SAS code using macros. Patrick Breheny

Tales from the Help Desk 3: More Solutions for Simple SAS Mistakes Bruce Gilsen, Federal Reserve Board

OBJECT_EXIST: A Macro to Check if a Specified Object Exists Jim Johnson, Independent Consultant, North Wales, PA

AN INTRODUCTION TO MACRO VARIABLES AND MACRO PROGRAMS Mike S. Zdeb, New York State Department of Health

Using Macros to Automate SAS Processing Kari Richardson, SAS Institute, Cary, NC Eric Rossland, SAS Institute, Dallas, TX

Facebook Twitter YouTube Google Plus Website

5. Crea+ng SAS Datasets from external files. GIORGIO RUSSOLILLO - Cours de prépara+on à la cer+fica+on SAS «Base Programming»

Macro Quoting. Overview of Macro Quoting CHAPTER 7

How to Use SDTM Definition and ADaM Specifications Documents. to Facilitate SAS Programming

Nine Steps to Get Started using SAS Macros

Let SAS Do the Downloading: Using Macros to Generate FTP Script Files Arthur Furnia, Federal Aviation Administration, Washington DC

Developing Applications Using BASE SAS and UNIX

SAS Programming Tips, Tricks, and Techniques

Data Presentation. Paper Using SAS Macros to Create Automated Excel Reports Containing Tables, Charts and Graphs

Accessing a Remote SAS Data Library. Transcript

Search and Replace in SAS Data Sets thru GUI

Arthur L. Carpenter, California Occidental Consultants

An Introduction to SAS/SHARE, By Example

Analyzing the Server Log

ing Automated Notification of Errors in a Batch SAS Program Julie Kilburn, City of Hope, Duarte, CA Rebecca Ottesen, City of Hope, Duarte, CA

Introduction to SAS Informats and Formats

AN ANIMATED GUIDE: SENDING SAS FILE TO EXCEL

Overview. NT Event Log. CHAPTER 8 Enhancements for SAS Users under Windows NT

It s not the Yellow Brick Road but the SAS PC FILES SERVER will take you Down the LIBNAME PATH= to Using the 64-Bit Excel Workbooks.

Module 816. File Management in C. M. Campbell 1993 Deakin University

SAS PASSTHRU to Microsoft SQL Server using ODBC Nina L. Werner, Madison, WI

Labels, Labels, and More Labels Stephanie R. Thompson, Rochester Institute of Technology, Rochester, NY

Illustration 1: Diagram of program function and data flow

SUGI 29 Applications Development

Flat Pack Data: Converting and ZIPping SAS Data for Delivery

CHAPTER 1 Overview of SAS/ACCESS Interface to Relational Databases

You have got SASMAIL!

The SAS Data step/macro Interface

The C Programming Language course syllabus associate level

How To Create An Audit Trail In Sas

Storing and Using a List of Values in a Macro Variable

SAS/ACCESS 9.3 Interface to PC Files

SAS ODS HTML + PROC Report = Fantastic Output Girish K. Narayandas, OptumInsight, Eden Prairie, MN

A Method for Cleaning Clinical Trial Analysis Data Sets

ODBC Chapter,First Edition

SAS Macro Autocall and %Include

Carl R. Haske, Ph.D., STATPROBE, Inc., Ann Arbor, MI

DBF Chapter. Note to UNIX and OS/390 Users. Import/Export Facility CHAPTER 7

Before You Begin... 2 Running SAS in Batch Mode... 2 Printing the Output of Your Program... 3 SAS Statements and Syntax... 3

SAS Client-Server Development: Through Thick and Thin and Version 8

How to Reduce the Disk Space Required by a SAS Data Set

Managing Tables in Microsoft SQL Server using SAS

A Comparison of SAS versus Microsoft Excel and Access s Inbuilt VBA Functionality

Box-and-Whisker Plots with The SAS System David Shannon, Amadeus Software Limited

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Automating SAS Macros: Run SAS Code when the Data is Available and a Target Date Reached.

Essential Project Management Reports in Clinical Development Nalin Tikoo, BioMarin Pharmaceutical Inc., Novato, CA

Enhancing the SAS Enhanced Editor with Toolbar Customizations Lynn Mullins, PPD, Cincinnati, Ohio

Effective Use of SAS/CONNECT ~ Cheryl Garner SAS Institute Inc., Cary, NC


Dynamic Decision-Making Web Services Using SAS Stored Processes and SAS Business Rules Manager

B) Mean Function: This function returns the arithmetic mean (average) and ignores the missing value. E.G: Var=MEAN (var1, var2, var3 varn);

The SET Statement and Beyond: Uses and Abuses of the SET Statement. S. David Riba, JADE Tech, Inc., Clearwater, FL

SAS/IntrNet 9.4: Application Dispatcher

Eliminating Tedium by Building Applications that Use SQL Generated SAS Code Segments

Version 1.0 Revision MODBUS/TCP Ethernet Communication Interface API for the Intelligent Instrumentation WinCE Platform SETUP MANUAL

Course Outline Basic Web Development

Data Mining Commonly Used SQL Statements

Web Development using PHP (WD_PHP) Duration 1.5 months

SAS. 9.4 Stored Processes. Developer's Guide Third Edition. SAS Documentation

SAS Comments How Straightforward Are They? Jacksen Lou, Merck & Co.,, Blue Bell, PA 19422

SAS/IntrNet 9.3: Application Dispatcher

New Tricks for an Old Tool: Using Custom Formats for Data Validation and Program Efficiency

PC/SAS Archive and Restore System

Debugging Complex Macros

Darshan Institute of Engineering & Technology PL_SQL

Applications Development ABSTRACT PROGRAM DESIGN INTRODUCTION SAS FEATURES USED

PharmaSUG 2014 Paper CC23. Need to Review or Deliver Outputs on a Rolling Basis? Just Apply the Filter! Tom Santopoli, Accenture, Berwyn, PA

Using SAS to Control and Automate a Multi SAS Program Process. Patrick Halpin November 2008

1. Base Programming. GIORGIO RUSSOLILLO - Cours de prépara+on à la cer+fica+on SAS «Base Programming»

Using Pharmacovigilance Reporting System to Generate Ad-hoc Reports

Copyright 2013 wolfssl Inc. All rights reserved. 2

THE CHALLENGE OF ADMINISTERING WEBSITES OR APPLICATIONS THAT REQUIRE 24/7 ACCESSIBILITY

All MySQL and PHP training students receive a copy of Apress' Beginning PHP and MySQL 5: From Novice to Professional and other related courseware.

Instant Interactive SAS Log Window Analyzer

1 Introduction FrontBase is a high performance, scalable, SQL 92 compliant relational database server created in the for universal deployment.

SQL Pass-Through and the ODBC Interface

SAS. 9.2 Integration Technologies. Directory Services Reference

Transcription:

Experts in SAS Business Intelligence Get Funky with %SYSFUNC David Shannon Amadeus Software Limited

Topics to cover Introduction About %SYSFUNC Six Examples 1. Check the existence of a SAS data set 2. Check an external file exists 3. Read the contents of a directory 4. Storing and restoring global options 5. Writing macro functions 6. Formatting macro variables %QSYSFUNC Formatting macro variables Conclusions Slide 2 Copyright 2009 Amadeus Software Limited

Introduction What are %SYSFUNC and %QSYSFUNC? Two powerful macro functions Enable use of most of Base SAS and SAS Component Language (SCL) functions Formatting macro variables %SYSFUNC vs. Data Step SCL functions are also available through the data step environment Data step has faster processing time than macro Data step is limited by step boundaries Slide 3 Copyright 2009 Amadeus Software Limited

About %SYSFUNC %SYSFUNC is a Macro function that executes SAS functions Can reference Base SAS functions, SCL functions or custom made functions created with Proc FCMP Syntax: %SYSFUNC( data-step-function(arguments) ) %SYSFUNC( data-step-function(arguments), format) %QSYSFUNC( data-step-function(arguments) <,format>) Slide 4 Copyright 2009 Amadeus Software Limited

About %SYSFUNC All data step functions except: All variable information functions DIF IORCMSG LBOUND LEXPERK PUT ALLCOMB DIM INPUT LEXCOMB LEXPERM RESOLVE ALLPERM HBOUND LAG LEXCOMBI MISSING SYMGET Note: use INPUTC, INPUTN, PUTC and PUTN for character or numeric formats Slide 5 Copyright 2009 Amadeus Software Limited

About %SYSFUNC Some SAS functions: ATTRC ATTRN CEXIST CLOSE CUROBS DCLOSE DINFO DNUM DOPEN DOPTNAME DOPTNUM DREAD DROPNOTE DSNAME EXIST FAPPEDN FCLOSE FCOL FDELETE FETCH FETCHOBS FEXIST FGET FILEEXIST FILEREF FINFO FNOTE FOPEN FOPTNMAE OPTNUM FPOINT FPOS FPUT FREAD FREWIND FRLEN FSEP FWRITE GETOPTION GETVARC GETVARN LIBNAME LIBREF MOPEN NOTE OPEN PATHNAME POINT REWIND SPEDIS SYSGET SYSMSG SYSRC VARFMT VARINFMT VARLABEL VARLEN VARNAME VARNUM VARTYPE Note: functions as per SAS 9.2 online documentation Slide 6 Copyright 2009 Amadeus Software Limited

1. Check a Data Set Exists Uses the EXIST() data step function %let exist=%sysfunc(exist(sashelp.shoes)); The exist function will return: 1 if the SAS data set exists 0 if it does not exist To retrieve the value of the exist variable use macro notation (&exist) Slide 7 Copyright 2009 Amadeus Software Limited

1. Check a Data Set Exists Checking the existence of a data set and writing to the log window: %macro dataset_exist; %let exist=%sysfunc(exist(sashelp.shoes)); %if &exist=0 %then %put Data set does not exist; %else %if &exist=1 %then %put Data set exists!; %mend; %dataset_exist; Slide 8 Copyright 2009 Amadeus Software Limited

2. Check an External File Exists Uses the FILEEXIST() data step function: %let rc=%sysfunc(fileexist(c:\elena\data\10feb2004.csv)); And the fileexist function will return: 1 it the CSV file exists 0 if it doesn t exist Slide 9 Copyright 2009 Amadeus Software Limited

2. Check an External File Exists Checking the existence of a file before deleting %macro delete_file; %let rc=%sysfunc(fileexist(c:\elena\data\data10feb2004.csv)); %if &rc=1 %then %do; %let rc1=%sysfunc(filename(rawfile, c:\elena\data\data10feb2004.csv)); %let rc2=%sysfunc(fdelete(&rawfile)); %if &rc2=0 %then %let message=file was deleted; %else %let message=file could not be deleted; %end; %else %let message=file does not exist; %put &message; %mend; %delete_file; Note: also consider operating system security Slide 10 Copyright 2009 Amadeus Software Limited

3. Read the Contents of a Directory Import all CSV files found under a network directory %macro read_files; %let rc=%sysfunc(filename(rawdata,c:\elena\data)); %let did=%sysfunc(dopen(&rawdata)); %let dnum=%sysfunc(dnum(&did)); %do i=1 %to &dnum; %let name_file=%sysfunc(dread(&did,&i)); %put file=&name_file; %let csv=%sysfunc(scan(&name_file,2,.)); %if &csv=csv %then %do; %let name_file_short=%sysfunc(scan(&name_file,1,.)); %put file=&name_file_short; PROC IMPORT OUT= WORK.&name_file_short DATAFILE= "C:\Elena\data\&name_file" DBMS=CSV REPLACE; RUN; %end; %end; %mend; %read_files; Slide 11 Copyright 2009 Amadeus Software Limited

4. Storing & Restoring Global Options Useful in generic, validated macros Use GETOPTION() data step function Slide 12 Copyright 2009 Amadeus Software Limited

5. Writing Macro Functions Use %SYSFUNC to return a value Lets write a function to validate a SAS name Use IFC function result=ifc( expression, True, False <,Missing> ); %let name = new_column_name; %sysfunc( ifc( %sysfunc(nvalid(&name.)), &name., %sysfunc(nliteral(&name.)) ) ); Slide 13 Copyright 2009 Amadeus Software Limited

5. Writing Macro Functions No semicolon! Slide 14 Copyright 2009 Amadeus Software Limited

6. Formatting Macro Variables When formatting macro variables SYSFUNC can reduce the number of steps required Include the date at the start of a title using data step processing: data _null_; fdate=put(date(),worddate.); call symputx('wdate',fdate); run; title "&wdate Report title"; Slide 15 Copyright 2009 Amadeus Software Limited

6. Formatting Macro Variables Using SYSFUNC: title "%sysfunc(date(),worddate.) Report title"; Include the date at the end of a title title "Report title %sysfunc(date(),worddate.)"; Slide 16 Copyright 2009 Amadeus Software Limited

6. Formatting Macro Variables Remove the leading blank spaces using the left function: title "Report title %sysfunc(left(%sysfunc(date(),worddate.)))"; But if we execute the code we obtain: ERROR: The function LEFT referenced by the %SYSFUNC or %QSYSFUNC macro function has too many arguments. Step by step processing: title "Report title %sysfunc(left(%sysfunc(date(),worddate.)))"; title "Report title %sysfunc(left( May 6, 2009))"; First argument of the left function Second argument Slide 17 Copyright 2009 Amadeus Software Limited

%QSYSFUNC Solution: Use %QSYSFUNC quoting function Same as SYSFUNC but it can mask special characters, including & and %, and mnemonic operators title "Report title %sysfunc(left(%qsysfunc(date(),worddate.)))"; Slide 18 Copyright 2009 Amadeus Software Limited

Conclusions %SYSFUNC Funky or not? + Alternative to data step processing with SCL functions + SYSFUNC and SCL can increase the number of functions available to a SAS programmer in the macro environment + Very powerful formatting tool - Processing time can be longer compared to data step Slide 19 Copyright 2009 Amadeus Software Limited

Thank you for listening! Elena Muriel Amadeus Software Ltd. Tel: +44 (0)1993 878287 E-Mail: elena.muriel@amadeus.co.uk Web: www.amadeus.co.uk Slide 20 Copyright 2009 Amadeus Software Limited David Shannon Amadeus Software Ltd. Tel: +44 (0)1993 878287 E-Mail: david.shannon@amadeus.co.uk Web: www.amadeus.co.uk