Chapter Contents

Previous

Next
SAS Companion for UNIX Environments

Reading from and Writing to UNIX Commands (PIPE)

Under UNIX, you can use the FILENAME statement to assign filerefs 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 ls command through a pipe to the wc (word count) command by entering

ls | wc -w

The syntax of the FILENAME statement is

FILENAME fileref PIPE 'UNIX-command' <options>;

fileref
is the name by which you reference the pipe from the SAS System.

PIPE
identifies the device-type as a UNIX pipe.

'UNIX-command'
is the name of a UNIX command, executable program, or shell script to which you want to route output or from which you want to read input. The command(s) must be enclosed in either double or single quotes.

options
control how the external file is processed. See FILENAME for an explanation of these 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 /dev/null.

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 -e command 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 ps -ax command.

The operating environment uses pipes to send the output from ps to the grep command, which searches for every occurrence of the string 'sas'. The FILENAME statement connects the output of the 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 in file ps.sas:

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 ps to grep and from grep into the SAS command:
ps -e|grep 'sas'|sas ps.sas &
The output will be stored in ps.lst; the log in ps.log as 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 mail command and sent to user PAT:

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 lp command are sent to the SAS log.
FOOTNOTE 1:  The form of the command that starts a remote shell varies among the various UNIX operating systems. [arrow]


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.