Reading from and Writing to UNIX Commands (PIPE)

What Are Pipes?

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:
ls | wc -w

Syntax of the FILENAME Statement to Assign a Fileref to a 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. Here is the syntax of the FILENAME statement:
FILENAME fileref PIPE 'UNIX-command' <options>;
fileref
is the name by which you reference the pipe from SAS.
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 commands must be enclosed in either double or single quotation marks.
options
control how the external file is processed. For an explanation of these options, see FILENAME Statement: UNIX.
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 SAS assumes that the input comes from a UNIX command. If the fileref is used in a FILE statement, then SAS assumes that the output goes to a UNIX command.

Using the Fileref for Reading

Specifying a 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.

Example 1: Sending the Output of the Process Command to a SAS DATA Step

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 SAS:
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 in 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.

Example 2: Using the Stdin Fileref to Read Input

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, and the log will be stored in ps.log, as described in The Default Routings for the SAS Log and Procedure Output in UNIX Environments.

Using the Fileref for Writing

Specifying a Fileref for Writing

When the fileref is used for writing, the output from SAS is read in by the specified UNIX command, which then executes.

Example 1: Sending Mail Using Pipes

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';

Example 2: Starting a Remote Shell and Printing Output

Consider this FILENAME statement:
filename letterq pipe 'remsh alpha lp -dbldga3';
Any data sent to the letterq fileref is passed to the UNIX command, which starts a remote shell on the computer named Alpha. Note that the form of the command that starts a remote shell varies among the various UNIX operating systems. The shell then prints the letterq output on the printer identified by the destination BLDGA3. Any messages that are produced by the lp command are sent to the SAS log.