Previous Page | Next Page

Using Unnamed and Named Pipes under Windows

Using Unnamed Pipes


Introduction to Unnamed Pipes

Unnamed pipes enable you to invoke a program outside of SAS and redirect the program's input, output, and error messages to SAS. This capability enables you to capture data from a program external to SAS without creating an intermediate data file.

For unnamed pipes to work with Windows applications external to SAS, the application program must read data from standard input (STDIN), write output to standard output (STDOUT), and write errors to standard error (STDERR). These files have numeric file handles associated with them, as follows:

File File Handle
STDIN 0
STDOUT 1
STDERR 2

When SAS captures STDERR from another application, the error messages are routed by default to the SAS log. If you want to write to STDIN in another application, you can use a PUT statement in a SAS DATA step. Because SAS can write to STDIN and capture from STDOUT in the same application, unnamed pipes can be used to send data to an external program, as well as to capture the output and error messages of the same program. You can use redirection sequences to redirect STDIN, STDOUT, and STDERR.

When you start SAS from the Windows desktop, STDIN and STDOUT are not available to your programs.

For more information, see Using Redirection Sequences or your Windows documentation.


Unnamed Pipe Syntax

To use an unnamed pipe, issue a FILENAME statement with the following syntax:

FILENAME fileref PIPE 'program-name' option-list;

You can use the following arguments with this syntax of the FILENAME statement:

fileref

is any valid fileref, as described in Referencing External Files.

PIPE

is the device-type keyword that tells SAS you want to use an unnamed pipe.

program-name

specifies the external Windows application program. This argument must fully specify the pathname to the program, or the path to the directory containing the program must be contained in the Windows PATH environment variable. This argument can also contain program options. For example, you can specify the following argument to indicate you want to invoke the STOCKMKT program on all stocks:

'stockmkt.exe -all'
option-list

can be any of the options valid in the FILENAME statement, such as the LRECL= or RECFM= options. For a complete list of options available for the FILENAME statement under Windows, see FILENAME Statement: Windows.


Using Redirection Sequences

Any Windows application that accommodates standard input, output, and error commands can use the unnamed pipe feature. Because many Windows system commands use standard input, output, and error commands, you can use these commands with unnamed pipes within SAS. Unless you specify otherwise, an unnamed pipe directs STDOUT and STDERR to two different files. To combine the STDOUT and STDERR into the same file, use redirection sequences. The following is an example that redirects STDERR to STDOUT for the Windows DIR command:

filename listing pipe 'dir *.sas 2>&1';

In this example, if any errors occur in performing this command, STDERR (2) is redirected to the same file as STDOUT (1). This example demonstrates SAS ability to capitalize on operating environment capabilities. This feature of redirecting file handles is a function of the Windows operating system rather than of SAS.


Unnamed Pipe Example

In the following example, you use the unnamed pipes feature of SAS under Windows to produce some financial reports. The example assumes you have a stand-alone program that updates stock market information from a financial news bureau. You need SAS to invoke a stock market report with the most recently created data from the stock market program. The following is how you create and use the pipe within your SAS session:

filename stocks pipe 'stockmkt.exe -all' console=min;
data report;
   infile stocks;
   input stock $ open close change;
run;
proc print;
   var stock open close change;
   sum change;
   title 'Stock Market Report';
run;

In this example, the PIPE device-type keyword in the FILENAME statement indicates that the fileref STOCKS is an unnamed pipe. The STOCKMKT.EXE reference is the name of the stand-alone program that generates the stock market data. The host-option CONSOLE=MIN indicates that the command prompt window that is opened to run the STOCKMKT.EXE program is opened minimized. The INFILE statement causes SAS to invoke the STOCKMKT.EXE program and read the data in the pipe from it. The STOCKMKT.EXE program completes without you being aware that it has been implemented (except for the command prompt window button on the Windows task bar). Because the fileref STOCKS has already been defined as an unnamed pipe, the standard output from STOCKMKT.EXE is redirected to SAS and captured through the INFILE statement. The SAS program reads in the variables and uses the PRINT procedure to generate a printed report. Any error messages generated by STOCKMKT.EXE appear in the SAS log.

Previous Page | Next Page | Top of Page