Referencing External Files

Accessing External Files

To access external files, you must tell SAS how to find the files. Use the following statements to access external files:
FILENAME
associates a fileref with an external file that is used for input or output.
FILE
opens an external file for writing data lines. Use the PUT statement to write lines.
INFILE
opens an external file for reading data lines. Use the INPUT statement to read lines.
%INCLUDE
opens an external file and reads SAS statements from that file. (No other statements are necessary.)
These statements are discussed in the section SAS Statements under Windows , and in the SAS statements section in SAS Statements: Reference.
You can also specify external files in various SAS dialog box entry fields (for example, as a file destination in the Save As dialog box), the FILENAME function, and in SAS commands, such as FILE and INCLUDE.
Depending on the context, SAS can reference an external file by using:
  • a fileref assigned with the FILENAME statement or function
  • an environment variable defined with either the SET system option or the Windows SET command
  • a Windows filename enclosed in quotation marks
  • member-name syntax (also called aggregate syntax)
  • a single filename within quotation marks (a file in the working directory).
The following sections discuss these methods of specifying external files.
Because there are several ways to specify external files in SAS, SAS uses a set of rules to resolve an external file reference and uses this order of precedence:
  1. Check for a standard Windows file specification enclosed in quotation marks.
  2. Check for a fileref defined by a FILENAME statement or function.
  3. Check for an environment variable fileref.
  4. Assume that the file is in the working directory.
In other words, SAS assumes that an external file reference is a standard Windows file specification. If it is not, SAS checks to determine whether the file reference is a fileref (defined by either a FILENAME statement, FILENAME function, or an environment variable). If the file reference is none of these filerefs, SAS assumes it is a filename in the working directory. If the external file reference is not valid for one of these choices, SAS issues an error message indicating that it cannot access the external file.

Using a Fileref

Overview of Using a Fileref

One way to reference external files is with a fileref. A fileref is a logical name associated with an external file. You can assign a fileref with a File Shortcut in the SAS Explorer window, the My Favorite Folders window, the FILENAME statement, the FILENAME function, or you can use a Windows environment variable to point to the file. This section discusses the different ways to assign filerefs and also shows you how to obtain a listing of the active filerefs and clear filerefs during your SAS session.

Assigning File Shortcuts

In an interactive SAS session, you can use the SAS Explorer window or the My Favorite Folders window to create filerefs. The SAS Explorer File Shortcuts folder contains a listing of active filerefs. To create a new fileref from SAS Explorer:
  1. Select the File Shortcuts folder and then select Filethen selectNew
  2. In the File Shortcut Assignment window, enter the name of the shortcut (fileref) and the path to the SAS file that the shortcut represents.
  3. You can also check Enable at Start up to reassign the shortcut for all subsequent SAS sessions.
To assign a file shortcut using the My Favorite Folders window:
  1. Open the folder that contains the file.
  2. Position the cursor over the file, right mouse click and select Create File Shortcut.
  3. In the Create File Shortcut dialog box, type the name of the file shortcut and press Enter or click OK.
You can then use these file shortcuts in your SAS programs.
Note: File Shortcuts are active only during the current SAS session.

Using the FILENAME Statement

The FILENAME statement provides a means to associate a logical name with an external file or directory.
Note: The syntax of the FILENAME function is similar to the FILENAME statement. For information about the FILENAME function, see SAS Functions and CALL Routines: Reference.
The simplest syntax of the FILENAME statement is as follows:
FILENAME filerefexternal-file”;
For example, if you want to read the file C:\MYDATA\SCORES.DAT, you can issue the following statement to associate the fileref MYDATA with the file C:\MYDATA\SCORES.DAT:
filename mydata "c:\mydata\scores.dat";
Then you can use this fileref in your SAS programs. For example, the following statements create a SAS data set named TEST, using the data stored in the external file referenced by the fileref MYDATA:
data test;
   infile mydata;
   input name $ score;
run;
Note: The words AUX, CON, NUL, PRN, LPT1 - LPT9, and COM1 - COM9 are reserved words under Windows. Do not use these words as filerefs.
You can also use the FILENAME, FILE, and INFILE statements to concatenate directories of external files and to concatenate multiple individual external files into one logical external file. These topics are discussed in Assigning a Fileref to Concatenated Directories and Assigning a Fileref to Concatenated Files .
The * and ? wildcards can be used in either the external filename or file extension for matching input filenames. Use * to match one or more characters and the ? to match a single character. Wildcards are supported for input only in the FILENAME and INFILE statements, and in member-name syntax (aggregate syntax). Wildcards are not valid in the FILE statement. The following filename statement reads input from every file in the current directory that begins with the string wild and ends with .dat:
filename wild 'wild*.dat';
data;
   infile wild;
   input;
run;
The following example reads all files in the current working directory:
filename allfiles '*.*';
data;
   infile allfiles;
   input;
run;
The FILENAME statement accepts various options that enable you to associate device names, such as printers, with external files and to control file characteristics, such as record format and length. Some of these options are illustrated in Advanced External I/O Techniques . For the complete syntax of the FILENAME statement, refer to FILENAME Statement: Windows .

Using Environment Variables

Just as you can define an environment variable to serve as a logical name for a SAS library (see Assigning SAS Libraries Using Environment Variables ), you can also use an environment variable to refer to an external file. You can choose either to define a SAS environment variable using the SET system option or to define a Windows environment variable using the Windows SET command. Alternatively, you can define environment variables using the System dialog box, accessed from the Control Panel.
Note: The words AUX, CON, NUL, PRN, LPT1 - LPT9 - and COM1 - COM9 are reserved words under Windows. Do not use these words as environment variables.
The availability of environment variables makes it simple to assign resources to SAS before invocation. However, the environment variables that you define (using the SET system option) for a particular SAS session are not available to other applications.

Using the SET System Option

For example, to define a SAS environment variable that points to the external file C:\MYDATA\TEST.DAT, you can use the following SET option in your SAS configuration file:
-set myvar c:\mydata\test.dat
Then, in your SAS programs, you can use the environment variable MYVAR to refer to the external file:
data mytest;
   infile myvar;
   input name $ score;
run;
It is recommended that you use the SET system option in your SAS configuration file if you invoke SAS using the Windows Start menu.

Using the SET Command

An alternative to using the SET system option to define an environment variable is to use the Windows SET command. For example, the Windows SET command that equates to the previous example is
SET MYVAR=C:\MYDATA\TEST.BAT
. You can also define SET commands by using System Properties dialog box that you access from the Control Panel.
You must issue all the SET commands that define your environment variables before you invoke SAS. If you define an environment variable in an MS-DOS window, and then start SAS from the Start menu, SAS does not recognize the environment variable.

Assigning a Fileref to a Directory

You can assign a fileref to a directory and then access individual files within that directory using member-name syntax (also called aggregate syntax).
For example, if all your regional sales data for January are stored in the directory C:\SAS\MYDATA, you can issue the following FILENAME statement to assign the fileref JAN to this directory:
filename jan "c:\sas\mydata";
Now you can use this fileref with a member name in your SAS programs. In the following example, you reference two files stored in the JAN directory:
data westsale;
   infile jan(west);
   input name $ 1-16 sales 18-25
         comiss 27-34;
run;
data eastsale;
   infile jan(east);
   input name $ 1-16 sales 18-25
         comiss 27-34;
run;
When you use member-name syntax, you do not have to specify the file extension for the file that you are referencing, as long as the file extension is the expected one. For example, in the previous example, the INFILE statement expects a file extension of .DAT. The following table lists the expected file extensions for the various SAS statements and commands:
Default File Extensions for Referencing External Files with Member-Name Syntax
SAS Command or Statement
SAS Window
File Extension
FILE statement
EDITOR
.DAT
%INCLUDE statement
EDITOR
.SAS
INFILE statement
EDITOR
.DAT
FILE command
EDITOR
.SAS
FILE command
LOG
.LOG
FILE command
OUTPUT
.LST
FILE command
NOTEPAD
none
INCLUDE command
EDITOR
.SAS
INCLUDE command
NOTEPAD
none
For example, the following program submits the file C:\PROGRAMS\TESTPGM.SAS to SAS:
filename test "c:\programs";
%include test(testpgm);
SAS searches for a filename TESTPGM.SAS in the directory C:\PROGRAMS.
If your file has a file extension different from the default file extension, you can use the file extension in the filename, as in the following example:
filename test "c:\programs";
%include test(testpgm.xyz);
If your file has no file extension, you must enclose the filename in quotation marks, as in the following example:
filename test "c:\programs";
%include test("testpgm");
To further illustrate the default file extensions SAS uses, here are some more examples using member-name syntax. Assume that the following FILENAME statement has been submitted:
filename test "c:\mysasdir";
The following example opens the file C:\MYSASDIR\PGM1.DAT for output:
file test(pgm1);
The following example opens the file C:\MYSASDIR\PGM1.DAT for input:
infile test(pgm1);
The following example reads and submits the file C:\MYSASDIR\PGM1:
%include test("pgm1");
These examples use SAS statements. SAS commands, such as the FILE and INCLUDE commands, also accept member-name syntax and have the same default file extensions as shown in Default File Extensions for Referencing External Files with Member-Name Syntax .
Another feature of member-name syntax is that it enables you to reference a subdirectory in the working directory without using a fileref. For example, suppose you have a subdirectory named PROGRAMS that is located beneath the working directory. You can use the subdirectory name PROGRAMS when referencing files within this directory. For example, the following statement submits the program stored in working-directory \PROGRAMS\PGM1.SAS:
%include programs(pgm1);
The next example uses the FILE command to save the contents of the active window to working-directory \PROGRAMS\TESTPGM.DAT:
file programs(testpgm);
Note: If a directory name is the same as a previously defined fileref, the fileref takes precedence over the directory name.

Assigning a Fileref to Concatenated Directories

Member-name syntax is also handy when you use the FILENAME statement to concatenate directories of external files. For example, suppose you issue the following FILENAME statement:
filename progs ("c:\sas\programs",
                "d:\myprogs");
This statement tells SAS that the fileref PROGS refers to all files stored in both the C:\SAS\PROGRAMS directory and the D:\MYPROGS directory. When you use the fileref PROGS in your SAS program, SAS looks in these directories for the member that you specify. When you use this concatenation feature, you should be aware of the protocol SAS uses, which depends on whether you are accessing the files for read, write, or update. For more information, see Understanding How Concatenated Directories Are Accessed .

Summary of Rules for Resolving Member-Name Syntax

SAS resolves an external file reference that uses member-name syntax by using a set of rules. For example, suppose your external file reference in a SAS statement or command is the following:
progs(member1)
SAS uses the following set of rules to resolve this external file reference. This list represents the order of precedence:
  1. Check for a fileref named PROGS defined by a FILENAME statement.
  2. Check for a SAS or Windows environment variable named PROGS.
  3. Check for a directory named PROGS beneath the working directory.
The member name must be a valid physical filename. If no extension is given (as in the previous example), SAS uses the appropriate default extension, as given in Default File Extensions for Referencing External Files with Member-Name Syntax . If the extension is given or the member name is quoted, SAS does not assign an extension, and it looks for the filename exactly as it is given.

Assigning a Fileref to Concatenated Files

You can specify concatenations of files when reading external files from within SAS. Concatenated files consist of two or more file specifications (which might contain wildcard characters) separated by blanks or commas. Here are some examples of valid concatenation specifications:
  • filename allsas ("one.sas", "two.sas", "three.sas");
  • filename alldata ("test1.dat" "test2.dat" "test3.dat");
  • filename allinc "test*.sas";
  • %include allsas;
  • infile alldata;
  • include allinc;
When you use this concatenation feature, you should be aware of the protocol SAS uses, which depends on whether you are accessing the files for read, write, or update. For more information, see Understanding How Concatenated Files Are Accessed .
Note: Do not confuse concatenated file specifications with concatenated directory specifications, which are also valid and are illustrated in Assigning a Fileref to Concatenated Directories .

Referencing External Files with Long Filenames

SAS supports the use of long filenames. (For more information about valid long filenames, see your Windows operating environment documentation.) You can use long filenames whenever you specify a filename as an argument to a dialog box, command, or any aspect of the SAS language.
When specifying external filenames with the SAS language, such as in a statement or function, you should enclose the filename in double quotation marks to reduce ambiguity (since a single quotation mark is a valid character in a long filename). When you need to specify multiple filenames, enclose each filename in double quotation marks and delimit the names with a blank space.
Here are some examples of valid uses of long filenames within SAS:
  • libname abc "My data file";
  • filename myfile "Bernie's file";
  • filename summer ("June sales" "July sales" "August sales");
  • include "A really, really big SAS program";

Referencing Files Using UNC Paths

SAS supports the use of the Universal Naming Convention (UNC) paths. UNC paths let you connect your computer to network devices without having to refer to a network drive letter. SAS supports UNC paths to the extent that Windows and your network software support them. In general, you can refer to a UNC path anywhere in SAS where you would normally refer to a network drive.
UNC paths have the following syntax:
\\SERVER\SHARE\FOLDER\FILEPATH
where
SERVER
is the network file server name.
SHARE
is the shared volume on the server.
FOLDER
is one of the directories on the shared volume.
FILEPATH
is a continuation of the file path, which might reference one or more subdirectories.
For example, the following command includes a file from the network file server ZAPHOD:
include "\\zaphod\universe\galaxy\stars.sas";

Listing Fileref Assignments

If you have assigned several filerefs during a SAS session and need to refresh your memory as to which fileref points where, you can use either the SAS Explorer window or the FILENAME statement to list all the assigned filerefs.
To use the SAS Explorer window to list the active filerefs, double-click File Shortcuts. The Explorer window lists all the filerefs active for your current SAS session. Any environment variables that you have defined as filerefs are listed, provided you have used them in your SAS session. If you have defined an environment variable as a fileref but have not used it yet in a SAS program, the fileref is not listed in the Explorer window.
You can use the following FILENAME statement to write the active filerefs to the SAS log:
filename _all_ list;

Clearing Filerefs

You can clear a fileref by using the following syntax of the FILENAME statement:
FILENAME fileref|_ALL_ <CLEAR>;
If you specify a fileref, only that fileref is cleared. If you specify the keyword _ALL_, all the filerefs that you have assigned during your current SAS session are cleared.
To clear filerefs using the SAS Explorer File Shortcuts:
  1. select the File Shortcuts that you want to delete. To select all File Shortcuts, select Editthen selectSelect All
  2. press the Delete key or select Editthen selectDelete
  3. Click OK in the message box to confirm deletion of the File shortcuts.
Note: You cannot clear a fileref that is defined by an environment variable. Filerefs that are defined by an environment variable are assigned for the entire SAS session.
SAS automatically clears the association between filerefs and their respective files at the end of your job or session. If you want to associate the fileref with a different file during the current session, you do not have to end the session or clear the fileref. SAS automatically reassigns the fileref when you issue a FILENAME statement for the new file.

Understanding How Concatenated Directories Are Accessed

When you associate a fileref with more than one physical directory, which file is accessed depends on whether it is being accessed for input or output.

Input

If the file is opened for input or update, the first file found that matches the member name is accessed. For example, if you submit the following statements, and the file PHONE.DAT exists in both the C:\SAMPLES and C:\TESTPGMS directories, the one in C:\SAMPLES is read:
filename test ("c:\samples","c:\testpgms");
data sampdat;
   infile test(phone.dat);
   input name $ phonenum $ city $ state $;
run;

Output

When you open a file for output, SAS writes to the file in the first directory listed in the FILENAME statement, even if a file by the same name exists in a later directory. For example, suppose you enter the following FILENAME statement:
filename test ("c:\sas","d:\mysasdir");
Then, when you issue the following FILE command, the file SOURCE.PGM is written to the C:\SAS directory, even if a file by the same name exists in the D:\MYSASDIR directory:
file test(source.pgm);

Understanding How Concatenated Files Are Accessed

When you associate a fileref with more than one physical file, the behavior of SAS statements and commands depends on whether you are accessing the files for input or output.

Input

If the file is opened for input, data from all files are entered. For example, if you issue the following statements, the %INCLUDE statement submits four programs for execution:
filename mydata ("qtr1.sas","qtr2.sas",
                 "qtr3.sas","qtr4.sas");
%include mydata;

Output

If the file is opened for output, data are written to the first file in the concatenation. For example, if you issue the following statements, the PUT statement writes to MYDAT1.DAT:
filename indata "dogdat.dat";
filename outdata ("mydat1.dat","mydat2.dat",
                  "mydat3.dat","mydat4.dat");
data _null_;
   infile indata;
   input name breed color;
   file outdata;
   put name= breed= color=;
run;

Using a Quoted Windows Filename

Overview of Using a Quoted Windows Filename

Instead of using a fileref to refer to external files, you can use a quoted Windows filename. For example, if the file C:\MYDIR\ORANGES.SAS contains a SAS program that you want to invoke, you can issue the following statement:
%include "c:\mydir\oranges.sas";
When you use a quoted Windows filename in a SAS statement, you can omit the drive and directory specifications if the file that you want to reference is located in the working directory. For example, if in the previous example the working directory is C:\MYDIR, you can submit this statement:
%include "oranges.sas";

Using a File in Your Working Directory

If you store the external files that you need to access in your working directory and they have the expected file extensions (see Default File Extensions for Referencing External Files with Member-Name Syntax ), you can simply refer to the filename, without quotation marks or file extensions, in a SAS statement. For example, if a filename ORANGES.SAS is stored in your working directory and ORANGES is not defined as a fileref, you can submit the file with the following statement:
%include oranges;
Remember, though, that using this type of file reference requires that
  • the file is stored in the working directory
  • the file has the correct file extension
  • the filename is not also defined as a fileref.
For more information about how to determine and change the SAS working directory, see Determining the Current Folder When SAS Starts and Changing the SAS Current Folder .

Running External LUA Files

You can run external scripts written in the LUA programming language from the SAS command line. For more information, see Running External Lua Files in SAS Companion for UNIX Environments.