Using SAS to Control and Automate a Multi SAS Program Process Patrick Halpin, dunnhumby USA, Cincinnati, OH

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

Using UNIX Shell Scripting to Enhance Your SAS Programming Experience

Copy That! Using SAS to Create Directories and Duplicate Files

Chasing the log file while running the SAS program

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

PharmaSUG Paper PO10

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

Using SAS Enterprise Guide with the WIK

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

A Guided Tour Through the SAS Windowing Environment Casey Cantrell, Clarion Consulting, Los Angeles, CA

Automate Secure Transfers with SAS and PSFTP

An Introduction to SAS/SHARE, By Example

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

Using UNIX Shell Scripting to Enhance Your SAS Programming Experience

Text Generational Data Sets (Text GDS)

Exploring UNIX: Session 5 (optional)

Paper A Simplified and Efficient Way to Map Variable Attributes of a Clinical Data Warehouse

Patricia Guldin, Merck & Co., Inc., Kenilworth, NJ USA

Transitioning from Batch and Interactive SAS to SAS Enterprise Guide Brian Varney, Experis Business Analytics, Portage, MI

Building Flexible Jobs in DataFlux

SAS Visual Analytics Environment Stood Up? Check! Data Automatically Loaded and Refreshed? Not Quite

Parallelizing Windows Operating System Services Job Flows

UNIX Spawner Program. Starting the UNIX Spawner Program CHAPTER 36

Paper PS05_05 Using SAS to Process Repeated Measures Data Terry Fain, RAND Corporation Cyndie Gareleck, RAND Corporation

Paper CC-013. Die Macro Die! Daniel Olguin, First Coast Service Options, Jacksonville, FL

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

Rupinder Dhillon Dec 14, 2012 TASS-i

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

When Powerful SAS Meets PowerShell TM

Extending the Scope of Custom Transformations

Document and Enhance Your SAS Code, Data Sets, and Catalogs with SAS Functions, Macros, and SAS Metadata. Louise S. Hadden. Abt Associates Inc.

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

Let SAS Help You Easily Find and Access Your Folders and Files

Speed Dating: Looping Through a Table Using Dates

Unlock SAS Code Automation with the Power of Macros

Cisco IOS Shell. Finding Feature Information. Prerequisites for Cisco IOS.sh. Last Updated: December 14, 2012

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

SAS Simulation Studio 14.1: User s Guide. Introduction to SAS Simulation Studio

Dynamic Projects in SAS Enterprise Guide How to Create and Use Parameters

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

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

Bryan K. Beverly, UTA/DigitalNet

Using Java to Front SAS Software: A Detailed Design for Internet Information Delivery

ABSTRACT INTRODUCTION MACRO. Paper RF

I KNOW HOW TO PROGRAM IN SAS HOW DO I NAVIGATE SAS ENTERPRISE GUIDE?

Using Virtualization Environments to Support Migration to SAS 9.2

Using DDE with Microsoft Excel and SAS to Collect Data from Hundreds of Users

Download and Installation Instructions. Java JDK Software for Windows

SAS Strategy Management 5.2 Batch Maintenance Facility

Unix Scripts and Job Scheduling. Overview. Running a Shell Script

Applications Development. Paper 38-28

ET01. LIBNAME libref <engine-name> <physical-file-name> <libname-options>; <SAS Code> LIBNAME libref CLEAR;

Someone Changed My SAS Visual Analytics Report! How an Automated Version Control Process Can Rescue Your Report and Save Your Sanity

SAS Data Integration Studio Take Control with Conditional & Looping Transformations

Perceptive Content Licensing

Paper ###-YYYY. SAS Enterprise Guide: A Revolutionary Tool! Jennifer First, Systems Seminar Consultants, Madison, WI

While You Were Sleeping - Scheduling SAS Jobs to Run Automatically Faron Kincheloe, Baylor University, Waco, TX

Using Dynamic Data Exchange

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

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

The Next Generation Smart Program Repository

A Picture Is Worth A Thousand Data Points - Increase Understanding by Mapping Data. Paul Ciarlariello, Sinclair Community College, Dayton, OH

Macro Method to use Google Maps and SAS to Geocode a Location by Name or Address

Providing Users with Access to the SAS Data Warehouse: A Discussion of Three Methods Employed and Supported

Shells. A shell is a command line interpreter that is the interface between the user and the OS. The shell:

CHAPTER 3 SHELL PROGRAMS: SCRIPTS

Introduction to the OpenVMS Operating Environment

Out of Control! A SAS Macro to Recalculate QC Statistics

SAS Drug Development Program Portability

Create Metadata Documentation using ExcelXP

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

Write SAS Code to Generate Another SAS Program A Dynamic Way to Get Your Data into SAS

SAS Macro Technique for Embedding and Using Metadata in Web Pages. DataCeutics, Inc., Pottstown, PA

In this paper, we will build the macro step-by-step, highlighting each function. A basic familiarity with SAS Macro language is assumed.

The Ins and Outs of %IF

5/8/2012. Specifying Instructions to the Shell Chapter 8

Improving Your Relationship with SAS Enterprise Guide Jennifer Bjurstrom, SAS Institute Inc.

Installation and Configuration Instructions. SAS Model Manager API. Overview

Useful Tips When Deploying SAS Code in a Production Environment

Multitasking. In this chapter: Running a Command in the Backg round Checking on a Process Cancelling a Process

Bash Programming. Student Workbook

Applications Development. Paper 37-27

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

Submitting Code in the Background Using SAS Studio

Cover the Basics, Tool for structuring data checking with SAS Ole Zester, Novo Nordisk, Denmark

Different Methods for Accessing Non-SAS Data to Build and Incrementally Update That Data Warehouse

Shell Programming Overview

Using Metadata Queries To Build Row-Level Audit Reports in SAS Visual Analytics

SAS/ACCESS Interface to R/3

Storing and Reusing Macros

22-Sep CSCI 2132 Software Development Lecture 8: Shells, Processes, and Job Control. Faculty of Computer Science, Dalhousie University

Let UNIX Drive Your Oracle Interfaces

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

The Output Bundle: A Solution for a Fully Documented Program Run

Creating an ADaM Data Set for Correlation Analyses

SESUG Paper AD A SAS macro replacement for Dynamic Data Exchange (DDE) for use with SAS grid

Introduction: What is Unix?

for Western Users of SAS Software 2014

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

Paper PO06. Building Dynamic Informats and Formats

Transcription:

Paper T05-2007 Using SAS to Control and Automate a Multi SAS Program Process Patrick Halpin, dunnhumby USA, Cincinnati, OH ABSTRACT Often times a project is comprised of many SAS programs that need to run in a given order. Additionally, the SAS programs are dependent on previous SAS programs completing. One way to accomplish and automate this task is to use Done files and the SLEEP command in SAS. Done files are files that are created when a SAS program finishes. SAS programs dependent on previous jobs finishing look for those Done files to be created. Once the Done files are created the next SAS job in the process will start. INTRODUCTION How many times has one sat around waiting for a program to end to kick off the next one? Just batch the second to run later. What if you don t know when it will finish? Create a shell script that runs it after the first. What if you have multiple dependent programs? Buy scheduling software. What if it is just too expensive? Then read this! When a project consists of multiple SAS programs and they need to run in a given order the use of a UNIX shell script, Done files and the SLEEP Command can be very useful tools to not only accomplish this task but also help in the automation of it. The UNIX shell script runs the SAS programs in the correct order. The Done files can be files or SAS data sets that are created when a SAS program finishes. The SLEEP Command is used in SAS programs that are dependent on previous SAS jobs finishing. This paper will go into detail on how to accomplish this task step by step: 1) Creating the UNIX control Shell Script 2) Creating Done files 3) Using the SLEEP command 4) Putting it all together STEP 1 CREATING UNIX SHELL SCRIPTS The first thing you will need to do is list out the SAS programs in the order they need to run. You have to keep in mind what programs are dependent on other programs and which programs can be run at the same time. In the example below there will be 5 SAS programs in the process: Prog_1 and prog_2 can be run at the same time Prog_3 needs prog_2 to finish Prog_4 needs prog_1 and prog_3 to finish Prog_5 needs prog_4 to finish Figure 1 Program Flow 1 2 3 Need to understand dependencies and precedence s to each part of the program. 4 5 The UNIX shell script below is written in the Korn shell (program name batching.csh). #!/bin/ksh cd /Program_Directory/ sas prog_1.sas & 1

sas prog_2.sas sas prog_3.sas sas prog_4.sas sas prog_5.sas The first line of the shell script tells UNIX what language the program is written in. The second line changes to the directory the programs are in. The third line starts the first program in the process running. The & after prog_1.sas allows prog_1.sas and prog_2.sas to run at the same time. When prog_2.sas is complete prog_3.sas runs. After prog_3.sas completes prog_4.sas runs and after completion prog_5.sas runs. The last point is NOT what we want to do; we want prog_4.sas to start after BOTH prog_3.sas AND prog_1.sas. This is where the DONE files and SLEEP command come in. STEP 2 DONE FILES The second piece of the puzzle is creating Done files. A Done file is a file that is created when a SAS program has finished running. For this paper the Done file will actually be a SAS data set (the appendix shows how to use text files as Done files). Creating these Done files is easy. Two steps need to be added to a SAS program. At the start of the program you need to delete the Done file if it already exists. %delete_done(program_name); The delete_done macro checks for an existing done file by using the exist function and program name parameter passed to the macro. If the data set exists then it will be deleted. At the end of the program you will need to create the Done file %create_done(program_name); The create_done macro creates a SAS dataset out in the done library via a simple data step. All one needs to do is pass the macro the program name. STEP3 THE SLEEP COMMAND The SLEEP command allows one to pause the current SAS program. This is often done when one is opening and using SAS to control EXCEL. In that case one will usually let SAS sleep a few seconds so Excel can open. We are going to use the same theory but we will use SLEEP to wait until a precedent program/process is complete. The syntax is simple: Data _null_; X=Sleep(10,1) The value in parenthesis is the number of seconds SAS will pause processing. In the above example SAS will pause for 10 seconds. STEP4 PUTTING IT ALL TOGETHER Now that the basics for the Shell Script, Done files and SLEEP command have been reviewed, it is time to put it all together. The example process will have 5 SAS programs and the run order and conditions will match the logic explained in the UNIX SHELL SCRIPT section. 2

Figure 2 Program Order 1 2 3 4 5 1 & 2 can run together 3 runs after 2 has completed 4 only runs once 3 and 1 are completed Finally 5 runs after 4 has completed For this example we will need to add code to programs 1, 3, 4, and 5. In prog_1 delete any existing Done files and create a Done file once the program is completed /* Code added the top of prog_1 */ %delete_done(prog_1); /* Code added to the bottom of prog_1 */ %create_done(prog_1); In prog_3 delete any existing Done files and create a Done file once the program is completed, the same as one did in prog_1. /* Code added the top of prog_3 */ %delete_done(prog_3); /* Code added to the bottom of prog_3 */ %create_done(prog_3); Now we will edit prog_4 to look for the Done files from prog_1 and prog_3. To do this we need a SLEEP command 3

and a loop to check for the Done files. In this example one will look for the Done files every minute and will check for 2 hours. If after the 2 hours of looking for the Done files from prog_1 and prog_3 and not finding them prog_4 will not run. This time limit is put in so that prog_4 does not loop indefinitely. The time for the loop to look for the Done files should be long enough that prog_1 and prog_3 should have already completed, but note that you can have as long as you need for the time limit or not have one at all. /*Code added to the top of prog_4 */ %delete_done(prog_4); /* Code to check for both Done files */ %global counter max; %let counter=0; %let max=120; /* minutes to check */ %macro done_check; %if ((%sysfunc(exist(lib_done.done_prog_1)))=0 or (%sysfunc(exist(lib_done.done_prog_3)))=0) %then %do; %do %until (((%sysfunc(exist(lib_done.done_prog_1)))=1 and (%sysfunc(exist(lib_done.done_prog_3)))=1) or (&counter+0 > &max+0)); *** Let SAS sleep for a minute ***; data _null_; sleep_time=sleep(60,1); *** code to increment counter ***; %let counter=%eval(&counter.+1); *** Stop program if time out ***; %if &counter+0 > &max+0 %then endsas; %mend done_check; %done_check; Macro, done_check, checks for the existence of prog_1 and prog_3 Done files. If both are not found then it will look every minute and recheck for the Done files. Once found the macro will stop and the rest of the code will run. If after the time limit the Done files are not found the last %if will cause the program to stop. When this happens no Done file will be created for prog_4. Remember one will also need to add code to create a Done file to prog_4 to tell prog_5 that prog_4 completed and did not time out. /* Code added to the bottom of prog_4 */ %create_done(prog_4); In the start of prog_5, you need to check and see that prog_4 ran and did not time out looking for Done files. If prog_4 timed out there is no need to run prog_5. The code below checks to see if prog_4 ran and if not prog_5 will then terminate. /* Code added to the top of prog_5 */ %macro check(var1); 4

%if sysfunc(exist(lib_done.done_file&var1))=0 %then endsas; %mend check; %check(prog_4); The check macro looks for the existence of the prog_4 Done file. If not found the program will end. Now that all the additional code has been added to the programs one can run the shell script to run the entire process. This can be done at a UNIX prompt by running the batching.csh program (csh batching.csh) CONCLUSION Projects often consist of multiple SAS programs, the dependences and order of processing can become increasingly complex. What we have discussed above is a simple solution that can be used anywhere SAS exists. Using just a UNIX shell script and a couple of commands within your programs, processes can be streamlined. The above was just a simple example of how to do this many more bells and whistles can be added to the UNIX script and the Done files to build out the process further to increase robustness and efficiency. APPENDIX Below is the code one can use to create Done files that are text files /* Code to create Done text file */ filename donefile /program_directory/done_&var1..txt ; Data _NULL_; File donefile; Done= YES ; put; Run; %create_done(program_name); /* Code to delete Done text file */ X cd /program_directory/ ; X rm f done_&var1..txt ; %delete_done(program_name); /* Code to look for Done text files */ filename donfile1 /program_directory/done_prog_1.txt ; filename donfile2 /program_directory/done_prog_2.txt ; %global counter max; %let counter=0; %let max=120; /* minutes to check */ %macro done_check; %if ((%sysfunc(fexist(donfile1)))=0 or (%sysfunc(fexist(donfile2)))=0) %then %do; %do %until (((%sysfunc(fexist(donfile1)))=1 and (%sysfunc(fexist(donfile2)))=1) or (&counter+0 > &max+0)); *** Let SAS sleep for a minute ***; data _null_; sleep_time=sleep(60,1); *** code to increment counter ***; %let counter=%eval(&counter.+1); *** Stop program if time out ***; %if &counter+0 > &max+0 %then endsas; %mend done_check; %done_check; 5

/* Code to stop program if Done text file is not found */ filename donefile /program_directory/done_&var..txt ; %macro check(var1); %if sysfunc(fexist(&var1))=0 %then endsas; %mend check; %check(donefile); REFERENCES SAS Institute: SAS OnlineDoc, Version 9, 2005 SAS Institute: SAS Language Guide, Version 6.12 CONTACT INFORMATION Your comments and questions are valued and encouraged. Contact the author at: Patrick Halpin dunnhumby USA 302 West 3 rd Street Suite 300 Cincinnati, OH, 45202 Work Phone: 513-632-1170 E-mail: Patrick.halpin@us.dunnhumby.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. 6