Archive for May 30th, 2007

Using SAS FILENAME statement to extract directory file listings into SAS

The filename statement’s pipe option allows you to direct the output of operating system commands into SAS for further processing. Usefully, the Windows dir command (with its /s switch) and the UNIX equivalent ls allow you get a file listing into SAS. For example, here’s how you extract the list of files in your UNIX home directory into SAS:

filename DIRLIST pipe ‘ls ~’;

data dirlist;

length filename $200;

infile dirlist length=reclen;

input buffer $varying200. reclen;

run;

Using the ftp option on the filename statement allows you to get a list of the files in a directory on a remote server, even one with a different operating system to that used on the client (PC or server), very useful for cases where cross-platform systems are involved. Here’s some example code:

filename dirlist ftp ‘ ‘ ls user=’user’ host=’host’ prompt;

data _null_;

length filename $200;

infile dirlist length=reclen;

input buffer $varying200. reclen;

run;

The PROMPT option will cause SAS to ask you for a password and the null string is where you would otherwise specify the name of a file.

  • As is commonly the case with places like these, all the views that you find expressed on here in postings and articles are mine alone and not those of any organisation with which I have any association, through work or otherwise. With regards to any comments left on the site, I reserve the right to reject any that are inappropriate. Otherwise, whatever is said is the sole responsibility of whoever is leaving the comment.

Private