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 by entering

 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. The syntax of the FILENAME statement is

  • 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 command(s) must be enclosed in either double or single quotation marks.

options

  • control how the external file is processed . See "FILENAME Statement" on page 293 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 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

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 -egrep '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.

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 -egrep '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 in UNIX Environments" on page 155.

Using the 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 are passed to the UNIX command, which starts a remote shell on the machine named Alpha. [2] 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.

[2] The form of the command that starts a remote shell varies among the various UNIX operating systems.




SAS 9.1 Companion for UNIX Environments
SAS 9.1 Companion For Unix Enivronments
ISBN: 1590472101
EAN: 2147483647
Year: 2004
Pages: 185
Authors: SAS Institute

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net