SAS Macros as File Management Utility Programs



Similar documents
A Method for Cleaning Clinical Trial Analysis Data Sets

PharmaSUG Paper AD11

How To Write A Clinical Trial In Sas

Post Processing Macro in Clinical Data Reporting Niraj J. Pandya

Building a Customized Data Entry System with SAS/IntrNet

Applications Development ABSTRACT PROGRAM DESIGN INTRODUCTION SAS FEATURES USED

Web Reporting by Combining the Best of HTML and SAS

UNIX Comes to the Rescue: A Comparison between UNIX SAS and PC SAS

Optimizing System Performance by Monitoring UNIX Server with SAS

Better Safe than Sorry: A SAS Macro to Selectively Back Up Files

Search and Replace in SAS Data Sets thru GUI

SUGI 29 Coders' Corner

Preparing your data for analysis using SAS. Landon Sego 24 April 2003 Department of Statistics UW-Madison

Using FILEVAR= to read multiple external files in a DATA Step

AN INTRODUCTION TO UNIX

ABSTRACT TECHNICAL DESIGN INTRODUCTION FUNCTIONAL DESIGN

LabVIEW Report Generation Toolkit for Microsoft Office

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

SUGI 29 Applications Development

We begin by defining a few user-supplied parameters, to make the code transferable between various projects.

A Macro to Create Data Definition Documents

DiskPulse DISK CHANGE MONITOR

SAS UNIX-Space Analyzer A handy tool for UNIX SAS Administrators Airaha Chelvakkanthan Manickam, Cognizant Technology Solutions, Teaneck, NJ

ABSTRACT INTRODUCTION FILE IMPORT WIZARD

ABSTRACT INTRODUCTION

Using DDE and SAS/Macro for Automated Excel Report Consolidation and Generation

Implementing an Audit Trail within a Clinical Reporting Tool Paul Gilbert, Troy A. Ruth, Gregory T. Weber DataCeutics, Inc.

Webapps Vulnerability Report

Let There Be Highlights: Data-driven Cell, Row and Column Highlights in %TAB2HTM and %DS2HTM Output. Matthew Flynn and Ray Pass

Managing Tables in Microsoft SQL Server using SAS

You have got SASMAIL!

Viewing Form Results

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

A Quick and Dirty Method for Delivering SAS Reports Via the World Wide Web

Using Pharmacovigilance Reporting System to Generate Ad-hoc Reports

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

Importing Excel Files Into SAS Using DDE Curtis A. Smith, Defense Contract Audit Agency, La Mirada, CA

Computer Programming In QBasic

KEY FEATURES OF SOURCE CONTROL UTILITIES

Using SVN to Manage Source RTL

WebPublish User s Manual

Product: DQ Order Manager Release Notes

LabVIEW Report Generation Toolkit for Microsoft Office User Guide

This script is called by an HTML form using the POST command with this file as the action. Example: <FORM METHOD="POST" ACTION="formhandler.

Remove Voided Claims for Insurance Data Qiling Shi

MICROSOFT ACCESS 2003 TUTORIAL

SSN validation Virtually at no cost Milorad Stojanovic RTI International Education Surveys Division RTP, North Carolina

Preparing Real World Data in Excel Sheets for Statistical Analysis

A robust and flexible approach to automating SAS jobs under Unix Mike Atkinson, with the Ministry of Health Services, British Columbia

WS_FTP Professional 12

TECHNIQUES FOR BUILDING A SUCCESSFUL WEB ENABLED APPLICATION USING SAS/INTRNET SOFTWARE

ABSTRACT INTRODUCTION SESUG Paper PO-08

SAS, Excel, and the Intranet

Word 2010: Mail Merge to with Attachments

Applications Development

Understand for FORTRAN

Using SVN to Manage Source RTL

Automation of Large SAS Processes with and Text Message Notification Seva Kumar, JPMorgan Chase, Seattle, WA

User's Guide. Using RFDBManager. For 433 MHz / 2.4 GHz RF. Version

Top 10 Things to Know about WRDS

Exploit SAS Enterprise BI Server to Manage Your Batch Scheduling Needs

Using

Creating a Simple Macro

Internet/Intranet, the Web & SAS. II006 Building a Web Based EIS for Data Analysis Ed Confer, KGC Programming Solutions, Potomac Falls, VA

How to Create Dynamic HTML and Javascript using your Data Jennifer Sinodis, Bank One, Phoenix, AZ

Using SAS/GRAPH Software to Create Graphs on the Web Himesh Patel, SAS Institute Inc., Cary, NC Revised by David Caira, SAS Institute Inc.

Unix Scripts and Job Scheduling

1. To start Installation: To install the reporting tool, copy the entire contents of the zip file to a directory of your choice. Run the exe.

Using SAS to Create Graphs with Pop-up Functions Shiqun (Stan) Li, Minimax Information Services, NJ Wei Zhou, Lilly USA LLC, IN

The Einstein Depot server

ABSTRACT INTRODUCTION HOW WE RANDOMIZE PATIENTS IN SOME OF THE CLINICAL TRIALS. allpt.txt

Importing Excel File using Microsoft Access in SAS Ajay Gupta, PPD Inc, Morrisville, NC

List of FTP commands for the Microsoft command-line FTP client

Scheduling in SAS 9.4 Second Edition

AN ANIMATED GUIDE: SENDING SAS FILE TO EXCEL

WOW! YOU DID THAT WITH SAS STORED PROCESSES? Dave Mitchell, Solution Design Team, Littleton, Colorado

Scheduling in SAS 9.3

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

An Introduction to Using the Command Line Interface (CLI) to Work with Files and Directories

MOVES Batch Mode: Setting up and running groups of related MOVES run specifications. EPA Office of Transportation and Air Quality 11/3/2010

SAS Hints. data _null_; infile testit pad missover lrecl=3; input answer $3.; put answer=; run; May 30, 2008

Building a Web Based EIS for Data Analysis Ed Confer, KGC Programming Solutions, Potomac Falls, VA

Hypercosm. Studio.

An Approach to Creating Archives That Minimizes Storage Requirements

ABSTRACT INTRODUCTION %CODE MACRO DEFINITION

JobScheduler - Quickstart

Backing Up TestTrack Native Project Databases

An macro: Exploring metadata EG and user credentials in Linux to automate notifications Jason Baucom, Ateb Inc.

Macros from Beginning to Mend A Simple and Practical Approach to the SAS Macro Facility

ABSTRACT INTRODUCTION SAS AND EXCEL CAPABILITIES SAS AND EXCEL STRUCTURES

Interfacing SAS Software, Excel, and the Intranet without SAS/Intrnet TM Software or SAS Software for the Personal Computer

Job Scheduler Daemon Configuration Guide

Configuring a SAS Business Intelligence Client with the SAS Server to Support Multilingual Data Wei Zheng, SAS Institute Inc.

Improving Your Relationship with SAS Enterprise Guide

Introduction to Shell Programming

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

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

REx: An Automated System for Extracting Clinical Trial Data from Oracle to SAS

Enhanced Connector Applications SupportPac VP01 for IBM WebSphere Business Events 3.0.0

Transcription:

Paper 219-26 SAS Macros as File Management Utility Programs Christopher J. Rook, EDP Contract Services, Bala Cynwyd, PA Shi-Tao Yeh, EDP Contract Services, Bala Cynwyd, PA ABSTRACT This paper provides several SAS macros for file management and file conversion tasks. The SAS product used in this paper is SAS BASE, with SAS system installed on the UNIX and Windows operating systems. INTRODUCTION The SAS macros provided in this paper are for file management utility purposes. Each macro is a stand-alone program to perform a specific task for the platform specified. Such tasks include file concatenation of existing external files, common page concatenation of existing external files and file conversion to HTML for existing external files. This paper contains several sections. Each section discusses a SAS macro with SAS code and macro invocation example. / MACRO PARAMETERS: prmptcmd: This is the command to be executed at the UNIX prompt that will provide a list of the desired files to concatenate. catfile: This is the name of the file that will be created to contain all output files concatenated together. / %macro concat1(prmptcmd=, catfile=); filename lstfiles pipe "&prmptcmd"; data allfiles; length fname $50; infile lstfiles; input fname $; call execute('x "cat ' fname ' >> &catfile";'); %mend concat1; SAS MACROS FOR FILE CONCATENATION: The SAS macros concat1, concat2 and concat3, presented in this paper, can be used to concatenate existing external files. This type of file management is useful for a task such as printing a large number of existing external files. In this case it may be much more time efficient for you to combine all of the external files into a single file and simply print that file. The SAS macro concat1 will accomplish this task on the UNIX operating system. The SAS macro concat2 is for use with the Windows operating system and has extra features such as the option to insert a page feed character between the external files in the concatenated file. The SAS macro concat3 is also for use on the UNIX operating system and is different from concat1 because it allows you to select the specific page from each file that is to be concatenated together. As an example, the macro concat3 could be used when QCing several SAS output files and you would like to print the first page of each to check that column total counts are consistent across tables. You can call the macro concat3 specifying the first page of each output file and this will result in a single file being created that contains the first page of each output file in the specified directory. File Concatenation Macro for UNIX Macro Name: CONCAT1 The following SAS macro concat1 is for file concatenation on the UNIX operating system. This macro is designed to take several existing external files and concatenate them into a single file. Example Invocation of SAS Macro CONCAT1: The following SAS code shows an example invocation of concat1. All source files are in the same subdirectory. All files with an extension of "lst" will be concatenated together and saved with the new file name "allout.lst". %concat1(prmptcmd=ls.lst, catfile=allout.lst); File Concatenation Macro for Windows Macro Name: CONCAT2 The following SAS macro concat2 is for file concatenation under the Windows operating system. This macro makes use of some added features that are not present in the macro concat1. Namely, this macro checks to see if the file specified by the parameter catfile exists, and if so, deletes it. In addition, an option to insert a page feed character between concatenated files is included. There is also a fundamental way in which this macro differs from concat1 and that is that it doesn't execute the concatenation statements directly from the command prompt, but creates a batch file in MS-DOS that is executed as the final step in the process. The batch file is named concat.bat and resides in the current SAS working directory. This will ensure that it remains temporary since it will be deleted as soon as the

SAS session has ended. (The file concat.bat is created by specifying the current working directory in the file statement in a data step. Put statements then add the desired content to the file. The last line of the batch file contains the word "exit" to close the DOS-Prompt window and return to the current SAS session. SAS then continues to process the remaining code in the program.) / MACRO PARAMETERS: dir: Directory where outputs reside. prmptcmd: This is the list of options to be added to the "dir" statement in MS-DOS which will provide a list of the desired files to standard output. These are the files that will be concatenated together. catfile: Name of file to contain all of the concatenated files. pagefeed: Set to Y if a page feed is needed between files. / cmd = "type %sysfunc(pathname(work))\pagefeed.txt >> %trim(&dir)%left(&catfile)"; put cmd; % if last then do; put "exit"; call execute('x "%sysfunc(pathname(work))\concat";'); %mend concat2; Example Invocation of SAS Macro CONCAT2: The following SAS code shows an example invocation of concat2. All files with an extension of "lst" in the subdirectory "u:\concat\" will be concatenated together and saved with the new file name "allfiles.lst". In addition, a page feed will be inserted between each of the concatenated files. %macro concat2(dir=, prmptcmd=, catfile=, pagefeed=n); filename filelist pipe "dir %cmpres(&dir)&prmptcmd"; / Delete external file with the same name as the concatenation file. (If it exists.) / filename tmp "%cmpres(&dir)%cmpres(&catfile)"; %let rc=%sysfunc(fdelete(tmp)); / If a page feed is requested between each output, create a file in the work directory that contains a single page feed. / %if %upcase(&pagefeed)=y %then %do; file "%sysfunc(pathname(work))\pagefeed.txt"; pagefeed=byte(12); put @1 pagefeed; % / - Create batch file in the current work directory used to concatenate files. - If a page feed is requested between each file, append the pagefeed.txt file between each output file. - Execute the batch file. / length filename $200; infile filelist end=last; input filename $; file "%sysfunc(pathname(work))\concat.bat"; cmd = "type %trim(&dir)" compress(filename) " >> %trim(&dir)%left(&catfile)"; put cmd; %if %upcase(&pagefeed)=y %then %do; if not(last) then do; %concat2(dir=u:\concat\, prmptcmd=.lst /b, catfile=allfiles.lst, pagefeed=y); Common Page File Concatenation Macro for UNIX Macro Name: CONCAT3 The following SAS macro concat3 is for file concatenation under the UNIX operating system. This macro contains the added feature of selecting only one page from each file and concatenating all of the single pages together. The page common to each output file to be concatenated together is specified by the parameter pagenum. / MACRO PARAMETERS: dir: Directory where outputs reside. filetype: This is the extension of the files from the directory specified by the parameter "dir" to concatenate together. catfile: Name of file to contain all of the concatenated files. (This file will be given the same extension as that specified by the parameter filetype.) pagenum: Set to the number of the page of each file that is to be concatenated. / %macro concat3(dir=, filetype=, catfile=, pagenum=1); / Create macro to read in an external file, specified by para- meter filname, sending the page specified by the parameter pagenum to a new file with the same name, located in the work directory. /

%macro getpagex(filname=); infile "&dir.&filname" missover end=last; input @1 fstchar $1.; retain cnt 1; pagefeed=byte(12); if (cnt=&pagenum) then do; file "%sysfunc(pathname(work))/&filname"; if (fstchar ne pagefeed) then put _infile_; if last or (fstchar eq pagefeed) then put @1 pagefeed; else if (cnt > &pagenum) then stop; if fstchar=pagefeed then cnt=cnt+1; %mend getpagex; / Create data set containing 1 record per list file. Send first page of each list file to the work directory using the same name as the file containing all pages. Uses macro "getpagex" created previously. / %concat3(dir=u:/concat/,filetype=lst, catfile=allfiles,pagenum=1); Notes: 1.) The "pipe" option on the filename statement is used to execute an operating system statement from the command prompt directing the output away from standard output and into a SAS data set through an infile statement. This is all done within the SAS program. 2.) Any temporary files that are created by the above macros are stored in the SAS work directory, which ensures they are deleted once the SAS session is over. This is easily accomplished by first determining the physical location of the SAS work directory, then directing the content to that location using file and put statements. There are many ways of determining the physical location of the current work directory and the method we use in this paper is the macro code: "%sysfunc(pathname(work))". filename lstfiles pipe "ls &dir..&filetype"; data allfiles; length fullname $100; infile lstfiles; input fullname $; filename=reverse(scan(reverse(fullname),1,'/')); call execute('%getpagex (filname=' compress(filename) ');'); / If a file already exists with the same name as the output file, then delete it. / filename allfiles "&dir.&catfile..&filetype"; %if %sysfunc(fexist(allfiles)) %then %let rc=%sysfunc(fdelete(allfiles)); / Concatenate the chosen page of each of the list files together, creating a new file using the name of the parameter catfile. / set allfiles; call execute('x "cat ' "%sysfunc(pathname(work))/" compress(filename) ' >> ' "&dir.&catfile..&filetype" '";'); %mend concat3; Example Invocation of SAS Macro CONCAT3: The following SAS code shows an example invocation of the macro concat3. The first page of each file with an extension of of "lst" in the subdirectory "u:\concat\" will be concatenated together and saved with the new file name "allfiles.lst". Macro for File Conversion to HTML for Windows Macro Name: CONVHTML SAS Web Formatters are good tools for producing your SAS output in HTML format. In some regulatory situations, you are not allowed to rerun your old SAS program to regenerate the old SAS output with newer date on the output. The following SAS macro is one solution for the above situation. It reads in the existing SAS output file and puts HTML tags <PRE> and </PRE> at the beginning and at the end of the output file to convert it to HTML format. / MACRO Parameters: filein: This is the entire file location (including the directory) of the SAS output file to be converted to an HTML file. fileout: This is the entire file name and location where the HTML file will be written to. / %macro convhtml(filein=, fileout=); filename readin "&filein"; infile readin end=last; file "&fileout"; input; if _n_=1 then put @1 '<PRE>';

if not last then do; put _infile_; else put @1 '</PRE>'; %mend convhtml; Example Invocation of SAS Macro CONVHTML: The following SAS code shows an example invocation of the macro convhtml. Select a target SAS output file with its location and assign the information to macro parameter filein. Assign a new file name for the converted file with file name extension of html to macro parameter fileout. %convhtml(filein=c:\ar\file1.lis, fileout=file1.html); Note: In HTML files, white space is usually ignored by the browser. When using the <PRE> and </PRE> tags, however, white space is preserved and the resulting file is viewed through the browser exactly as it appears in the file. This lends itself rather nicely to viewing SAS tables and listings through web browsers. As an aside, link tags and character styles can also be used between the <PRE> and </PRE> tags. CONCLUSION The purpose of this paper is to provide you with 4 SAS macros that function as file management utility programs for the UNIX and Windows operating systems, including example invocations of each macro. To review, these SAS macros are: 1.) concat1 - This SAS macro concatenates all existing output files in a specified UNIX directory. 2.) concat2 - This SAS macro concatenates all existing output files in a specified directory on the Windows operating system. (Includes some added features that do not exist in the macro concat1.) 3.) concat3 - This SAS macro concatenates a common page of all existing output files in a specified UNIX directory. (The page number to be concatenated from each file is specified as an input parameter.) 4.) convhtml - This SAS macro converts an existing SAS output file to an HTML file. (The program that generated the output does not have to be rerun, which preserves any date and time stamps that exist on the pages of the output file.) Figure 1. Converting an Existing SAS Output File to HTML Format While SAS software is widely used in many industries to generate statistical summary tables and data listings in clear tabular formats, it can also be used in place of shell scripting (on UNIX platform) or batch programming (on MS-DOS operating system) to manage these output tables and listings rather effectively. REFERENCES LeBouton, K. (2000), Smokin With UNIX Pipes, Proceedings of the Twenty-Fifth Annual SAS Users Group International Conference, 25, 555-558. SAS is a registered trademark of SAS Institute Inc., in the USA and other countries. Windows is either a trademark or registered trademark of Microsoft Corporation in the United States and/or other countries. Figure 2. Converted SAS Output File on Browser MS-DOS is either a trademark or registered trademark of Microsoft Corporation in the United States and/or other countries. indicates USA registration.

CONTACT INFORMATION Christopher J. Rook (610) 917-5089 (W) E-mail: cjrook@nni.com Shi-Tao Yeh, Ph. D. (610) 917-5883 (W) E-mail: shitaoyeh@us.sina.com