|SAS Companion for UNIX Environments|
Under UNIX, you can use the FILENAME statement to assign
not only to external files and I/O devices, but also to a pipe. Pipes enable
your SAS application to receive input from any UNIX command that writes to
standard output and to route output to any UNIX command that reads from standard
input. In UNIX commands, the pipe is represented by a vertical bar (|). For
example, to find the number of files in your directory, you could redirect
the output of the
through a pipe to the
(word count) command by entering
ls | wc -w
The syntax of the FILENAME statement is
|FILENAME fileref PIPE 'UNIX-command' <options>;|
Whether you are using the command as input or output depends on whether you use the fileref in a reading or writing operation. For example, if the fileref is used in an INFILE statement, then the SAS System assumes that the input comes from a UNIX command; if the fileref is used in a FILE statement, then the SAS System assumes that the output goes to a UNIX command.
|Using the Fileref for Reading|
When the fileref is used for reading, the specified
UNIX command executes, and any output sent to its standard output or standard
error is read through the fileref. In this case, the standard input of the
command is connected to
For example, the following SAS program uses the PIPE
device-type keyword to send the output of the
ps (process) command to a SAS DATA step. The resulting SAS data set
contains data about every process currently running the SAS System:
filename ps_list pipe "ps -e|grep 'sas'"; data sasjobs; infile ps_list; length process $ 80; input process $ char80.; run; proc print data=sasjobs; run;The
ps -ecommand produces a listing of all active processes on the system, including the name of the command that started the task. In BSD-based UNIX systems, you use the
The operating environment uses pipes to send the output
ps to the
grep command, which searches for every occurrence
of the string
'sas'. The FILENAME statement connects the output
grep command to
the fileref PS_LIST. The DATA step then creates a data set named SASJOBS
from the INFILE statement that points to the input source. The INPUT statement
reads the first 80 characters on each input line.
In the next example, the STDIN fileref is used to read
input through a pipe into the SAS command which in turn executes the SAS program.
By placing the piping operation outside the SAS program, the program becomes
more general. The program in the previous example has been changed and stored
data sasjobs; infile stdin; length process $ 80; input process $ char80.; run; proc print data=sasjobs; run;To run the program, use pipes to send the output of
grepinto the SAS command:
ps -e|grep 'sas'|sas ps.sas &The output will be stored in
ps.lst; the log in
ps.logas described in The Default Routings for the SAS Log and Procedure Output.
|Using the Fileref for Writing|
When the fileref is used for writing, the output from the SAS System is read in by the specified UNIX command, which then executes.
In this example, any data sent to the MAIL fileref are
piped to the
filename mail pipe 'mail pat';
Consider this FILENAME statement:
filename letterq pipe 'remsh alpha lp -dbldga3';Any data sent to the LETTERQ fileref are passed to the UNIX command, which starts a remote shell on the machine named ALPHA.(footnote 1) The shell then prints the LETTERQ output on the printer identified by the destination BLDGA3. Any messages produced by the
lpcommand are sent to the SAS log.
Top of Page
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.