rtsug 04feb2014: beyond directory listings in sas by: jim worley

9
RTSUG 04Feb2014: Beyond Directory Listings in SAS By: Jim Worley

Upload: cory-powers

Post on 29-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: RTSUG 04Feb2014: Beyond Directory Listings in SAS By: Jim Worley

RTSUG 04Feb2014:Beyond Directory Listings in SAS

By: Jim Worley

Page 2: RTSUG 04Feb2014: Beyond Directory Listings in SAS By: Jim Worley

Directory Listings in SAS – What is missing?

– There are multiple ways in SAS to get a list of directory contents, including using SAS file functions (e.g. FILENAME, DOPEN, DNUM, etc.)

– No additional information available, limits what can be done with automation

– Very specific information given, no ability to customize what data is received (e.g. last modified date vs. creation date)

RTSUG 04Feb2014: Beyond Directory Listings in SAS 2

Page 3: RTSUG 04Feb2014: Beyond Directory Listings in SAS By: Jim Worley

Using a Pipe in SAS

– A “pipe” allows you to take the output messages from a system command and redirect it to another location other than the command window

– In SAS, using PIPE on a FILENAME statement allows you to redirect messages from a command into a SAS dataset through the infile statement

– Syntax:

Filename DIRLIST pipe 'dir "C:\Users\jlw34091\examples" ';

RTSUG 04Feb2014: Beyond Directory Listings in SAS 3

Standard FILENAME statement

Add the word PIPE after the filename handle

Use a system command surrounded by single quotes

– Since the system command requires being surrounded by single quotes, adding macro variables to automate command is possible but not straight forward. It is beyond the scope of this presentation but more information can be found online

Page 4: RTSUG 04Feb2014: Beyond Directory Listings in SAS By: Jim Worley

The “DIR” command

– Windows based command to get information about files in a directory

– Has multiple flags to change what information is presented, run either “dir /?” on a command line or search Google

– Some flags include “/a-d” to exclude subdirectory names, “/b” to exclude details except filename, and “/s” to search subdirectories

– By default lists the modified date, file size, and file name

RTSUG 04Feb2014: Beyond Directory Listings in SAS 4

Page 5: RTSUG 04Feb2014: Beyond Directory Listings in SAS By: Jim Worley

Raw Input Dump

RTSUG 04Feb2014: Beyond Directory Listings in SAS 5

filename DIRLIST pipe 'dir "C:\Users\jlw34091\examples" '; data dirlist ; length buffer $256 ; infile dirlist length=reclen dlm="~" missover; input buffer $;run ;

Page 6: RTSUG 04Feb2014: Beyond Directory Listings in SAS By: Jim Worley

Only Keep Rows We Want

– Adding an IF statement allows us to only keep rows meeting the criteria we want. This example will only keep SAS files.

– The IF statement below does three things. First it only keeps rows that begin with a valid date. Next it deletes any rows that are a directory. Finally it only keep rows that end with “.SAS”. While the last criteria would be enough to filter out the rows we want, all three are included to show examples of different ways to filter the data.

RTSUG 04Feb2014: Beyond Directory Listings in SAS 6

data dirlist ; length buffer $256 ; infile dirlist length=reclen dlm="~" missover; input buffer $; if input(scan(buffer,1," "),??MMDDYY10.) and index(buffer,"<DIR>") = 0 and index(upcase(buffer),".SAS") > 0;run ;

Page 7: RTSUG 04Feb2014: Beyond Directory Listings in SAS By: Jim Worley

Creating Useful Variables

RTSUG 04Feb2014: Beyond Directory Listings in SAS 7

data dirlist ;

... buffer = compbl(buffer); *Remove extra spaces;

LastModDate = input(scan(buffer,1," "),MMDDYY10.); LastModTime = input(substr(buffer,12,8),Time.); FileSizeInBytes = input(scan(buffer,4," "),comma20.); FileName = substr(buffer,index(buffer,scan(buffer,5," ")));

format LastModDate date9. LastModTime Time5.;run ;

Page 8: RTSUG 04Feb2014: Beyond Directory Listings in SAS By: Jim Worley

Beyond the Basics

– Almost any system command for Windows and UNIX/Linux can utilize the PIPE feature of the SAS filename statement

– Similar to running an X command but with more feedback than just the exit status

– Can be used to help automate processes based on system information without the need of any manual intervention

– Some example Windows commands that can be used are: FC (for comparing two files), FIND (Search within a file), OPENFILES (to check for file locks), and WHERE (search for a file)

RTSUG 04Feb2014: Beyond Directory Listings in SAS 8

Page 9: RTSUG 04Feb2014: Beyond Directory Listings in SAS By: Jim Worley

Contact :

Jim WorleyPrincipal Programmer/Analyst

Email      [email protected]

RTSUG 04Feb2014: Beyond Directory Listings in SAS 9

Questions?(Time Permitting)