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 SAS2
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 SAS3 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
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 SAS4
Raw Input Dump RTSUG 04Feb2014: Beyond Directory Listings in SAS5 filename DIRLIST pipe 'dir "C:\Users\jlw34091\examples" '; data dirlist ; length buffer $256 ; infile dirlist length=reclen dlm="~" missover; input buffer $; run ;
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 SAS6 data dirlist ; length buffer $256 ; infile dirlist length=reclen dlm="~" missover; input buffer $; if input(scan(buffer,1," "),??MMDDYY10.) and index(buffer," ") = 0 and index(upcase(buffer),".SAS") > 0; run ;
Creating Useful Variables RTSUG 04Feb2014: Beyond Directory Listings in SAS7 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 ;
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 SAS8
Contact : Jim Worley Principal Programmer/Analyst RTSUG 04Feb2014: Beyond Directory Listings in SAS9 Questions? (Time Permitting)