Assigning Filerefs to External Files or Devices with the FILENAME Statement

Introduction to the FILENAME Statement

The most common way to assign a fileref to an external file or device is with the FILENAME statement. There are several forms of the FILENAME statement, depending on the type of device that you want to access. For more information, see FILENAME Statement: UNIX.

Accessing DISK Files

The most common use of the FILENAME statement is to access DISK files. The FILENAME syntax for a DISK file is the following:
FILENAME fileref <DISK> 'pathname' <options>;
The following FILENAME statement associates the fileref myfile with the external file /users/mydir/myfile, which is stored on a disk device:
filename myfile disk '/users/mydir/myfile';
The following FILENAME statement assigns a fileref of prices to the file /users/pat/cars. The FILE statement then refers to the file using the fileref:
filename prices '/users/pat/cars';
data current.list;
   file prices;
   ...PUT statements...
run;
For more information about using DISK files, see Concatenating Filenames in UNIX Environments.
Note: If a filename has leading blanks, then they will be trimmed.

Debugging Code with DUMMY Devices

You can substitute the DUMMY device type for any of the other device types. This device type serves as a tool for debugging your SAS code without actually reading or writing to the device. After debugging is complete, replace the DUMMY device name with the proper device type, and your program will access the specified device type.
Here is the FILENAME syntax for a DUMMY file:
FILENAME fileref DUMMY 'pathname' <options>;
Output to DUMMY devices is discarded.

Sending Output to PRINTER Devices

The PRINTER device type enables you to send output directly to a printer. Here is the FILENAME syntax to direct a file to a PRINTER:
FILENAME fileref PRINTER '<printer> <printer-options>' <options>;
For example, this SAS program sends the output file to the BLDG3 printer:
filename myfile printer 'bldg3';

data test;
   file myfile;
   put 'This will appear in bldg3 .';
run;

Using Temporary Files (TEMP Device Type)

The TEMP device type associates a fileref with a temporary file stored in the same directory as the Work library. (See Work Library.) Using the TEMP device type enables you to create a file that lasts only as long as the SAS session.
Here is the FILENAME syntax for a TEMP file:
FILENAME fileref TEMP <options>;
For example, this FILENAME statement associates Tmp1 with a temporary file:
filename tmp1 temp;

Accessing TERMINAL Devices Directly

To access a terminal directly, use the TERMINAL device type. Here is the FILENAME syntax to associate a file with a terminal:
FILENAME fileref TERMINAL <'terminal-pathname'> <options>;
The terminal-pathname must be a pathname of the special file associated with the terminal. Check with your UNIX system administrator for information. Enclose the name in quotation marks. If you omit the terminal pathname, the fileref is assigned to your terminal.
For example, this FILENAME statement associates the fileref here with your terminal:
filename here terminal;
The following FILENAME statement associates the fileref thatfile with another terminal:
filename thatfile terminal '/dev/tty3';

Assigning Filerefs to Files on Other Systems (FTP, SFTP, and SOCKET Access Types)

You can access files on other systems in your network by using the FTP, SFTP, and SOCKET access methods. Here are the forms of the FILENAME statement:
FILENAME fileref FTP 'external-file' <ftp-options>;
FILENAME fileref SFTP 'external-file' <sftp-options>;
FILENAME fileref SOCKET 'external-file' <tcpip-options>;
FILENAME fileref SOCKET ':portno' SERVER <tcpip-options>;
These access methods are documented in SAS Statements: Reference. Under UNIX, the FTP access method supports an additional option:
MACH='machine'
identifies which entry in the .netrc file should be used to get the user name and password. The .netrc file resides on the host on which the SAS program is running. See the UNIX man page for more information about the .netrc file. You cannot specify the MACH option together with the HOST option in the FILENAME statement.
If you are transferring a file to UNIX from the z/OS operating environment and you want to use either the S370V or S370VB format to access that file, then the file must be of type RECFM=U and BLKSIZE=32760 before you transfer it.
CAUTION:
When you use the FTP access method to create a remote file, the UNIX permissions for that file are set to -rw-rw-rw-, which makes the file world-readable and world-writable.
See the UNIX man page for chmod for information about changing file permissions.