Previous Page | Next Page

Using External Files and Devices

Identifying External Files to SAS


How to Identify an External File to SAS

To access an external file, you must tell SAS how to find the file. Depending on the context, you can use any of the following specifications to identify an external file to SAS:


Order of Precedence for External File Specifications

It is possible (though generally not advisable) to use the same text string as a filename, a fileref, and an OpenVMS logical name. If an external file specification is a valid SAS name and is not enclosed in quotation marks, then SAS uses the following order of precedence to interpret the text string and locate the external file:

  1. If you have defined the text string as a SAS fileref, then SAS uses the file that the fileref refers to.

  2. If the text string is not a fileref, then SAS looks to see whether you have defined it as an OpenVMS logical name. If so, it uses the file that the logical name refers to.

  3. If the text string is neither a fileref nor an OpenVMS logical name, then SAS looks for a file by that name in your default directory.


Assigning Filerefs

Use the FILENAME statement, the FILENAME function, or the SAS Explorer window to assign a fileref to an external file. For more information, see the following:


Assigning OpenVMS Logical Names to External Files

You can use an OpenVMS logical name as a file specification. Use the DCL DEFINE (or ASSIGN) command to associate a logical name with an external file.

When you assign an OpenVMS logical name to an external file, you cannot use the pound sign (#) or the at sign (@) because these characters are not valid in OpenVMS logical names. (By contrast, if you use the FILENAME statement to assign a fileref, you can use these characters because filerefs follow SAS naming conventions.) Also, using the DCL DEFINE (or ASSIGN) command to define an OpenVMS logical name does not enable you to specify the keywords or options that are available with the FILENAME statement and the FILENAME function.

Remember that you can use the SAS X statement or X command to issue a DCL DEFINE (or ASSIGN) command from within a SAS program or a SAS process. (For details, see Issuing DCL Commands during a SAS Session.)

Note:   If you use the SAS X statement or X command to issue the DCL DEFINE (or ASSIGN) command, then you need to use the method described in Issuing a Single DCL Command Using the X Statement (which executes the command in the parent OpenVMS process), not the method described in Issuing Several DCL Commands Using the X Statement (which executes multiple DCL commands in an OpenVMS subprocess). OpenVMS logical names that are defined in a subprocess are not recognized by the current SAS process. By contrast, OpenVMS logical names that are defined in the OpenVMS parent process are available for use during the current SAS process.  [cautionend]


Examples: Using Logical Names to Access External Files

Suppose you want to read a data file named [YOURDIR]EDUC.DAT, and you decide to use an OpenVMS logical name to access the file. You can issue the DCL DEFINE command before you invoke SAS. The following is an example:

$ DEFINE INED [YOURDIR]EDUC.DAT

Alternatively, you can use the SAS X statement to issue a DCL DEFINE command in your SAS program:

x 'define ined [yourdir]educ.dat';

Either of these methods properly associates the OpenVMS logical name INED with the file [YOURDIR]EDUC.DAT. You can then use INED as the file specification in the INFILE statement:

infile ined;

You can use the same methods to write to an external file. For example, use an X statement to issue a DCL DEFINE command as follows:

x 'define outfile [yourdir]scores.dat';

Then use the OpenVMS logical name OUTFILE as the file specification in the following FILE statement:

file outfile;


Using OpenVMS Pathnames to Identify External Files

If you use an OpenVMS pathname as an external file specification, you must enclose the file specification in single or double quotation marks. The file specification must be a valid OpenVMS pathname to the external file that you want to access; therefore, the level of specification depends on your location in the directory structure. The number of characters in the quoted string must not exceed the maximum filename length that OpenVMS allows (255 characters for ODS-2 disks, 4,095 characters for ODS-5 disks).

Here are some examples of valid file specifications:

infile 'node2::device:[dir1.subdir1]food.dat';
file '[mydir]prices.dat';

To access a particular version of a file, include the version number in the quoted file specification. If you omit the version number, then SAS uses the highest-numbered version when reading, and it creates a new version when writing. To append records to the end of an existing file, use the MOD option in the FILE statement, type APPEND in windowing environment filename fields, or use the FAPPEND function. For example, the following FILE statement appends data lines to the file PRICES.DAT;1:

file 'prices.dat;1' mod;


Using Wildcard Characters in External File Specifications

You can use the following wildcard characters inside a SAS job wherever a quoted-string file specification is allowed, except that you cannot use them in a FILE statement.

* (asterisk)

matches all files.

% (percent sign)

matches any single character.

. . . (ellipsis)

searches all subdirectories.

The following are some examples of valid uses of wildcard characters:

The special characters # (pound sign) and @ (at sign) can be used only in external file specifications for files on ODS-5 disks. In general, these characters must be preceded by the RMS escape character ('^'). The # and @ character cannot be used in external file specifications for files on ODS-2 disks.


Specifying Concatenated Files

Under OpenVMS, you can specify concatenations of files when reading and writing external files from within SAS. Concatenated files consist of two or more file specifications, enclosed in quotation marks and separated by commas. You can include wildcard characters in the file specifications.

The usual rules for OpenVMS version-numbering apply. You cannot use a percent symbol (%) in the version number field.

The following are some examples of valid concatenation specifications:


Using Aggregate Syntax to Identify External Files

You can also use aggregate syntax to access individual files. To do so, assign a SAS fileref or an OpenVMS logical name to a directory, and specify the individual filename in parentheses. For example, suppose you use the following FILENAME statement to associate the fileref MYFILE with the directory [MYDIR]:

filename myfile '[mydir]';

To access a file named SCORES02.DAT in that directory, you could use the following INFILE statement:

infile myfile(scores02);

By default, the INFILE statement appends a file type of .DAT to the SCORES02 filename if a filetype is not specified. (For more information about default file types, see Default File Types.)

If you want to specify a different file type, then enclose the file specification in quotation marks, as in the following example:

infile myfile('scores02.new');


Identifying OpenVMS Text Libraries

Aggregate syntax is also used to identify OpenVMS text libraries. An OpenVMS text library has a default file type of .TLB and can store frequently used text files. For example, if you have several related files of data, you might want to store them in one OpenVMS text library. OpenVMS text libraries are also commonly used as SAS autocall libraries, which store SAS macros. For more information, see Autocall Libraries under OpenVMS.

To access a file in an OpenVMS text library, complete the following steps:

For example, you can use the following FILENAME statement to assign a fileref to an OpenVMS text library:

filename mytxtlib '[mydir]mydata.tlb';

Then, assuming that you want to use a library member (OpenVMS module) named SCORES01, you can use the following INFILE statement:

infile mytxtlib(scores01);

Note:   The file-type rules for OpenVMS text library syntax differ from the rules for directory-based aggregate syntax. When referring to a member of an OpenVMS text library, do not specify a file type. If you do, the file type is ignored. For example, in the following statements the fileref TEST refers to an OpenVMS text library:

filename test 'mylib.tlb';
data _null_;
   file test(one.dat);
   put 'first';
run;

The file type .DAT is ignored, and the FILE statement writes member ONE to the text library, not to the file ONE.DAT. Wildcard characters are not allowed in filenames when you are using OpenVMS text-library syntax.  [cautionend]


Identifying an External File That Is in Your Default Directory

As explained in Order of Precedence for External File Specifications, if an external file specification is a valid SAS name and is neither quoted nor a previously defined fileref or OpenVMS logical name, then SAS opens a file by that name in your default directory. The special characters # or @ are valid only in external file specifications for ODS-5 disks, these characters are not valid in external file specifications for ODS-2 disks.

The specification must be a filename only; do not include the file type. SAS uses a default file type depending on whether you are reading or writing data lines or reading SAS statements, as indicated in Default File Types.

The following INFILE statement reads the data file FOOD.DAT from the default directory (FOOD has not been defined as a SAS fileref nor as an OpenVMS logical name):

infile food;

When SAS encounters this statement, it searches the default directory for a file named FOOD.DAT. Records are read from FOOD.DAT based on subsequent INPUT statement specifications.

The following FILE statement writes data lines to the file PRICES.DAT in the default directory (PRICES has not been defined as a SAS fileref nor as an OpenVMS logical name):

file prices;

When SAS encounters this statement, it writes to a file named PRICES.DAT in the default directory. Data lines are written to PRICES.DAT based on subsequent PUT statement specifications.


Default File Types

By default, SAS uses the OpenVMS file type .DAT with both the INFILE and FILE statements. Therefore, if you want to read from or write to an existing file in the default directory when specifying only the filename, the file type must be .DAT; otherwise, SAS cannot locate the file, and it issues an error message.

The default file types are different if you are using windowing environment commands. The following table lists the default file types for SAS statements and commands. Be sure to include the file type in a quoted file specification unless you are sure that the SAS default is correct.

Default File Types for Commands and Statements
Reference File Type Window
FILE command .SAS Program Editor
FILE command .LOG Log
FILE command .LIS Output
INCLUDE command .SAS Program Editor
FILE statement .DAT Program Editor
%INCLUDE statement .SAS Program Editor
INFILE statement .DAT Program Editor

Previous Page | Next Page | Top of Page