Reading from External Files

Overview of Reading from External Files

After you allocate an external file, you can read from the file in a SAS DATA step by specifying it in the INFILE statement, the INCLUDE command, or the %INCLUDE statement.
This section describes the INFILE statement. For information about the INCLUDE command and the %INCLUDE statement, see SAS Statements: Reference. For information about the DATA step, see Base SAS Utilities: Reference.

INFILE Statement

Overview of the INFILE Statement

In a SAS DATA step, the INFILE statement specifies which external file is to be read by a subsequent INPUT statement. Every external file that you want to read must have a corresponding INFILE statement. The external file can be a sequential data set on disk or tape, a member of a partitioned data set (PDS or PDSE), or any of several nonstandard file types. For more information, see the description of the type argument in INFILE Statement Syntax. The file can also be entered from a terminal.
The INFILE statement is executable. Therefore, it can be used in conditional processing - in an IF/THEN statement, for example.
When multiple INFILE statements are present, the INPUT statement reads from the external file that was specified by the most recent INFILE statement. For a complete description of the INPUT statement, see SAS Statements: Reference.

INFILE Statement Syntax

This section provides a brief overview of INFILE statement syntax. For complete information about the INFILE statement, see INFILE Statement: z/OS.
The syntax of the INFILE statement is
INFILE file-specification <type> <options>;
file-specification
identifies the file. It can be in the following forms:
File Specification Examples for the INFILE Statement
Form
Example
fileref
report
fileref(member)
report(feb)
'physical-filename'
'library.daily.report'
'physical-filename(member)'
'library.daily.source(report1)'
reserved fileref
DATALINES
See INFILE Statement: z/OS for information about partial physical filenames and wildcard member names.
type
specifies the type of file. When you omit type, the default is a standard external file. Nonstandard (host-specific) file types that you can specify for z/OS are
DLI
for IMS databases. For information about IMS options for the INFILE statement, see SAS/ACCESS(R) 9.3 Interface to IMS: Reference.
HFS and PIPE
for files in UNIX System Services. For more information, see Accessing UNIX System Services Files. PIPE enables you to issue UNIX System Services commands from within the INFILE statement.
IDMS
specifies that the file is a CA-IDMS file. For information about CA-IDMS options for the INFILE statement, seeSAS/ACCESS DATA Step Interface to CA-IDMS: Reference .
ISAM
specifies that the file is an ISAM file. For more information, see Accessing Nonstandard Files .
VSAM
for VSAM files. Fore more information, see Accessing VSAM Data Sets.
VTOC
specifies that the Volume Table of Contents (VTOC) is to be accessed.
options
describe the input file's characteristics and specify how it is to be read with an INPUT statement. Many of these options are not host-dependent and are documented in SAS System Options: Reference. Those options that are host-specific are documented in INFILE Statement: z/OS. You can use these options to do the following:
  • define variables that contain information about the external file
  • specify special open and close processing
  • specify file characteristics

INFILE Statement Examples

Examples of the INFILE Statement
Type of Data Set
Example
sequential
infile 'library.daily.data';
member of a PDS or PDSE
infile report(feb);
or
infile 'lib.daily.src(rpt1)';
sequential or member of a PDS or PDSE1
infile data;
IMS
infile psb dli;
in-stream
infile datalines;
1The type depends on what the fileref is associated with.

Reading from a Sequential File

This example assigns the fileref RAW to the data set MYID.RAW.DATAX and uses the fileref in a simple DATA step:
filename raw 'myid.raw.datax' disp=shr;
data out;
   infile raw;
   input ... ;
run;
This example is similar to the previous one, except that it specifies a value for the SYSPREF= system option and then uses a partially qualified data set name in the FILENAME statement:
options syspref=sys2.sas7;
filename raw2 '.raw.datax' disp=shr;
data out;
   infile raw2;
   input ... ;
run;
For information about using SYSPREF= and partially qualified data set names, see Specifying Physical Files.

Reading from a Member of a PDS or PDSE

This example specifies the PDS name in the FILENAME statement and then specifies the member name in parentheses following the fileref in the INFILE statement:
filename mypds 'user.my.pds';
data out;
   infile mypds(mydata);
   input ... ;
run;
This example specifies both the PDS name and the member name in the FILENAME statement. Therefore, only the fileref is specified in the INFILE statement:
filename mymember 'user.my.pds(mydata)';
data out;
   infile mymember;
   input ... ;
run;
Multiple members of a PDS can be open for read access at the same time.

Reading from the Terminal

If you run SAS in interactive line mode or in noninteractive mode, you can read input from the terminal. These examples illustrate ways to define a terminal file.
In the first example, TERMINAL is specified as the device type in the FILENAME statement:
filename term1 terminal;
data one;
   infile term1;
   input ... ;
run;
In the next example, an asterisk is used in place of a physical filename to indicate that the file will be entered from the terminal:
filename term2  '*';
data out;
   infile term2;
   input ... ;
run;
Note: Enter "/*" to signify end-of-file after entering your input from the terminal.

Reading Concatenated Data Sets

Multiple sequential data sets can be concatenated (via a JCL DD statement, a TSO ALLOCATE command, or a FILENAME statement) and read consecutively using one pair of INFILE or INPUT statements.
Sequential data sets and individual PDS or PDSE members can also be concatenated, as in the following example:
x alloc fi(in1)
   da('my.data1' 'my.pds(mem)' 'my.data2');
data mydata;
   infile in1;
   input ... ;
   /* SAS statements  */
run;
Here is an example of using the FILENAME statement to concatenate data sets:
filename in1 ('my.data1' 'my.pds(mem)' 'my.data2');
You can also concatenate external files that are stored on different types of devices and that have different characteristics.
If PDSs or PDSEs are concatenated and a member is specified in the INFILE statement, then SAS searches each PDS or PDSE for that member. SAS searches in the order in which the PDSs appear in the DD statement, the ALLOCATE command, or the FILENAME statement or function. If the member is present in more than one of the PDSs, SAS retrieves the first one that it finds.

Reading from Multiple External Files

Overview of Reading from Multiple External Files

You can read from multiple external files either sequentially or alternately from multiple filerefs.

Sequentially Reading from Multiple External Files

To read from multiple external files sequentially, use the END= option or the EOF= option in each INFILE statement to direct program control to a new file after each file has been read. For example:
filename outrdr sysout=a pgm=intrdr
      recfm=fb lrecl=80;
data _null_;
   length dsn $ 44;
   input dsn $;
   infile dummy filevar=dsn end=end;
   file outrdr noprint notitles;
   do until(end);
      input;
      put _infile_;
      end;
datalines;
PROD.PAYROLL.JCL(BACKUP)
PROD.PAYROLL.JCL(TRANS)
PROD.PAYROLL.JCL(PRINT)
;
run;
For more information about the END= and EOF= options of the INFILE statement, see SAS Statements: Reference.

Alternately Accessing Multiple External Files

For you to be able to alternately access multiple external files, the files must have different filerefs. You can partially process one file, go to a different file, and return to the original file. An INFILE statement must be executed each time you want to access a file, even if you are returning to a file that was previously accessed. The DATA step terminates when SAS encounters the EOF of any of the files. Consider the following example:
filename exfile1 'my.file.ex1';
filename exfile2 'my.file.ex2';
data mydata;
   infile exfile1;
   input ... ;
   /* SAS statements  */

   infile exfile2;
   input ... ;

   /* SAS statements  */

   infile exfile1;
   input ... ;

   /* SAS statements  */

run;
When there is more than one INFILE statement for the same fileref, with options specified in each INFILE statement, the options apply cumulatively to successive files.
Note: Multiple files inside concatenations cannot be accessed in this manner.

Reading from Print Data Sets

When reading from a print data set, you can tell SAS to ignore the carriage-control character that is in column 1 of print data sets by specifying SAS system option FILECC. For more information, see FILECC System Option: z/OS.

Getting Information about an Input Data Set

In the following example, data set information is printed in the SAS log. Control blocks are printed in hexadecimal format. The example can be used with either a sequential data set or a PDS.
filename in 'user.data';
data out;
   infile in jfcb=jf dscb=ds volumes=vol
             ucbname=ucb devtype=dev;
   if (_n_ = 1) then
      put @1 'Data Set Name:' @17 jf  $52.     /
          @4 'Volume ='       @20 vol $30.     /
          @4 'JFCB ='         @20 jf  $hex200. /
          @4 'DSCB ='         @20 ds  $hex188. /
          @4 'Devtype ='      @20 dev $hex48.  /
          @4 'Device Addr ='  @20 ucb $3.      ;
run;