Previous Page | Next Page

Using External Files and Devices

Reading from and Writing to OpenVMS Commands (Pipes)


What Are Pipes?

Under OpenVMS, you can use the FILENAME statement to assign filerefs to a pipe. Pipes enable your SAS application to receive input from any OpenVMS command that writes to SYS$OUTPUT, and to write input to any OpenVMS command that reads from SYS$INPUT.

Note:   The PIPE device cannot be used from an OpenVMS captive account. For more information, see Limitations of Using a Captive Account.  [cautionend]


Syntax for Assigning Filerefs to a Pipe

Use the following FILENAME statement syntax to assign filerefs to a pipe:

FILENAME fileref PIPE 'OpenVMS-command ' <options>
fileref

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

PIPE

identifies the device type as an OpenVMS PIPE.

'OpenVMS-command '

is the name of one or more OpenVMS commands to which you want to write output or from which you want to receive input. The command must be enclosed in single or double quotation marks.

options

control how the external file is processed. See FILENAME Statement: OpenVMS for more information.

Note:   Only the LRECL= host option or the LRECL= system option for external files is supported with the PIPE device.  [cautionend]

Whether you are using the OpenVMS command as input or output depends on whether you are using the fileref for reading or writing. For example, if the fileref is used in an INFILE statement, SAS assumes that the input is coming from the output of an OpenVMS command. If the fileref is used in a FILE statement, SAS assumes that the output is going to be the input to an OpenVMS command.


Using the Fileref for Reading

When the fileref is used for reading, the specified OpenVMS command executes, and any output that is sent to SYS$OUTPUT or SYS$ERROR is read through the fileref. SYS$INPUT is connected to the null device.


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

The following SAS program uses the PIPE device-type keyword to send the output of the DIRECTORY command to a SAS DATA step. The resulting SAS data set contains the filename, file type, version, and date and time information about each file in the default directory.

filename dir_list pipe 'directory/date';

data sasjobs;
   infile dir_list pad;
   length fname $ 80;
   input fname $ char80.;
run;

proc print data=sasjobs;
run;
    

The DIRECTORY/DATE command retrieves information about the files in the current directory. The FILENAME statement connects the output of the DIRECTORY command to the fileref DIR_LIST. The DATA step creates a data set named SASJOBS from the INFILE statement; SASJOBS points to the input source. The INPUT statement reads the first 80 characters in each input line.


Example 2: Using the SYS$INPUT Fileref to Read Input through a Pipe

In the following example, the SYS$INPUT fileref is used to read input through a pipe into the SAS command. Assume that the SAS command executes the SAS program. The program in the previous example has been changed and stored in the file DIR.SAS. By placing the piping operation outside the SAS program, you can change the information that is passed to the program through the command without having to modify the program itself.

data sasjobs
   infile SYS$INPUT;
   length fname $ 80;
   input fname $ char80.;
run;

proc print data=sasjobs;
run;

To run the program, use the OpenVMS PIPE command to send the output of the DIR/DATE command to the SAS command:

$pipe dir/date | sas dir

The output is stored in DIR.LIS and the log is stored in DIR.LOG. See Overriding the Default Log and Output Destinations under OpenVMS for more details about routing SAS log and procedure output.


Using the Fileref for Writing

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


Example: Sending Data to an External File via a Pipe

In the following example, the OpenVMS CREATE command takes its input from the EXTFILE fileref and the file LIST.TXT is created in the default directory. The file LIST.TXT contains one record:

Mary 39 jacks

The code creates the data in a SAS program and sends the data to an external file via PIPE:

filename extfile pipe 'create list.txt';
data a;  
   input name $ num toy$;
   file extfile;
   put _infile_;
   cards;
Mary   39   jacks
;

Previous Page | Next Page | Top of Page