Chapter Contents

Previous

Next
SAS Companion for the CMS Environment

Reading from External Files

You can read from an external file in a SAS DATA step by specifying it in the INFILE statement. Alternatively, you can use the INCLUDE command or the %INCLUDE statement.


Using 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. (See SAS Language Reference: Dictionary for a complete description of the INPUT statement.)

This section provides a brief overview of INFILE statement syntax. For complete information about the INFILE statement, see INFILE.

The syntax of the INFILE statement is

INFILE file-specification <options>;

file-specification
identifies the file. It may be in the following forms:

Form Example
fileref indata
fileref(member) maclib(mem1)
fileref(filename filetype) mydir(myfile in)
filename filetype filemode (or SFS directory) 'myfile in b'
filename filetype 'myfile in'
CMS pipeline < myfile in b
reserved fileref DATALINES

options
controls how a file is read. When specifying more than one option, use a blank space to separate each option.

You can use these options to do the following:

See INFILE for a list of valid option values under CMS. For information about other options that you can specify in the INFILE statement, see SAS Language Reference: Dictionary.

Dynamically Changing Files in a DATA Step

Use the FILEVAR= option in the INFILE statement to dynamically change input files in the middle of a DATA step. For example:

data read;
x='mydata march a';
y='mylib names b';
   infile a filevar=x;
   input Q1 $1. Q2 $1.;
   infile in filevar=y;
   input name $20.;
run;

The data set READ consists of the variables Q1, Q2, and NAME; values for Q1 and Q2 come from the file MYDATA MARCH A, whereas the value of NAME comes from the file MYLIB NAMES B.


Using the %INCLUDE Statement

The %INCLUDE statement includes SAS statements from external files, from earlier points in the same job or session, or from the terminal, and it submits those statements automatically. The form of the %INCLUDE statement is

%INCLUDE file-specification </options>;

The file-specification argument is in one of the forms given in Identifying an External File . For information about the available options, see SAS Language Reference: Dictionary. For example, suppose you issue the following FILENAME statement:

filename mypgm 'saspgm tastest c';

You can issue the following %INCLUDE statement to copy in and execute the SAS statements that are stored in the file SASPGM TASTEST C:

%include mypgm;

You can also use the %INCLUDE statement in interactive line mode to recall previously entered statements, or in noninteractive mode to allow input from the terminal. See %INCLUDE and SAS Language Reference: Dictionary for more information about the %INCLUDE statement.

Aggregate external files can be specified in a %INCLUDE statement as well:

filename mydir 'a';

...more data lines...

%include mydir(saspgm);
As shown in the previous example, the filetype can be omitted from %INCLUDE specifications of aggregate SFS directories and minidisks. For aggregates other than CMS MACLIBs, an unspecified filetype defaults to SAS. The previous example copies in and executes the file SASPGM SAS A.


Using the INCLUDE Command

The INCLUDE command enables you to display an entire external file in a window.

The form of the INCLUDE command is

INCLUDEfile-specification<options>

The file-specification argument is in one of the forms given in Identifying an External File . For information about the available options, see SAS Language Reference: Dictionary. For example, suppose you specify this FILENAME statement:

filename sasfile tape 'tap3';
The following command-line command includes the file in the Program Editor window:
include sasfile

If you do not give a value for file-specification, the file from the previous FILE or INCLUDE command is used. If you have not issued any previous FILE or INCLUDE commands, an error message tells you that no default file exists.

If the specified fileref has an A in its record format (as specified by the RECFM= option in the FILENAME statement), the INCLUDE command treats the file as a print file.

When you specify a minidisk or SFS directory as an external aggregate file with the INCLUDE command you can leave off the filetype, which will default to SAS.

See SAS Language Reference: Dictionary for more information about the INCLUDE command.


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.