Chapter Contents

Previous

Next
FILENAME

FILENAME



Assigns or deassigns a fileref for an external file, directory, or output device and returns a value

Category: External Files


Syntax
Arguments
Details
Examples
Example 1: Assigning a Fileref to an External File
Example 2: Assigning a System-Generated Fileref
Example 3: Assigning a Fileref to a Pipe File
See Also

Syntax

FILENAME(fileref,file-name<,device-type<,host-options<,dir-ref>>>)

Arguments

fileref
in a DATA step, specifies the fileref to assign to the external file. In a macro (for example, in the %SYSFUNC function), fileref is the name of a macro variable (without an ampersand) whose value contains the fileref to assign to the external file.
Tip: A blank fileref ( '') causes an error. If the fileref is a DATA step character variable with a blank value and a minimum length of 8 characters, a fileref is generated for you.
Tip: If a macro variable named in fileref has a null value, a fileref is generated for you.

file-name
specifies the external file. Specifying a blank file-name deassigns one that was assigned previously.

device-type
specifies the type of device or the access method that is used if the fileref points to an input or output device or location that is not a physical file:
DISK specifies that the device is a disk drive.
Tip: When you assign a fileref to a file on disk, you are not required to specify DISK.
Alias: BASE
DUMMY specifies that the output to the file is discarded.
Tip: Specifying DUMMY can be useful for testing.
GTERM indicates that the output device-type is a graphics device that will be receiving graphics data.
PIPE specifies an unnamed pipe.

Note:   Some operating environments do not support pipes.  [cautionend]

PLOTTER specifies an unbuffered graphics output device.
PRINTER specifies a printer or printer spool file.
TAPE specifies a tape drive.
TEMP creates a temporary file that exists only as long as the filename is assigned. The temporary file can be accessed only through the logical name and is available only while the logical name exists.
Restriction: Do not specify a physical pathname. If you do, SAS returns an error.
Tip: Files manipulated by the TEMP device can have the same attributes and behave identically to DISK files.
TERMINAL specifies the user's terminal.
The FILENAME function also supports operating environment specific devices. For details, see the SAS documentation for your operating environment.

host-options
specifies host-specific details such as file attributes and processing attributes. For details, see the SAS documentation for your operating environment.

dir-ref
specifies the fileref that was assigned to the directory or partitioned data set in which the external file resides.


Details

FILENAME returns 0 if the operation was successful, [ne]0 if it was not successful. The name associated with the file or device is called a fileref (file reference name). Other system functions that manipulate external files and directories require that the files be identified by fileref rather than by physical filename.

Operating Environment Information:   The term directory in this description refers to an aggregate grouping of files managed by the operating environment. Different operating environments identify such groupings with different names, such as directory, subdirectory, MACLIB, or partitioned data set. For details, see the SAS documentation for your operating environment.

Under some operating environments, you can also assign filerefs by using system commands. Depending on the operating environment, FILENAME may be unable to change or deassign filerefs assigned outside the SAS System.  [cautionend]

The association between a fileref and a physical file lasts only for the duration of the current SAS session or until you change or discontinue the association by using FILENAME. You can deassign filerefs by specifying a null string for the file-name argument in FILENAME.


Examples

Example 1: Assigning a Fileref to an External File

This example assigns the fileref MYFILE to an external file, then deassigns the fileref. Note that in a macro statement you do not enclose character strings in quotation marks.

%let filrf=myfile;
%let rc=%sysfunc(filename(filrf, physical-filename));
%if &rc ne 0 %then
   %put %sysfunc(sysmsg());
%let rc=%sysfunc(filename(filrf));

Example 2: Assigning a System-Generated Fileref

This example assigns a system-generated fileref to an external file. The fileref is stored in the variable FNAME. Note that in a macro statement you do not enclose character strings in quotation marks.

%let rc=%sysfunc(filename(fname, physical-filename));
%if &rc %then
   %put %sysfunc(sysmsg());
%else
   %do;
      more macro statements
   %end;

Example 3: Assigning a Fileref to a Pipe File

This example assigns the fileref MYPIPE for a pipe file with the output from the UNIX command LS, which lists the files in the directory /u/myid. Note that in a macro statement you do not enclose character strings in quotation marks.

%let filrf=mypipe;
%let rc=%sysfunc(filename(filrf, %str(ls /u/myid), pipe));

See Also

Functions:
FEXIST
FILEEXIST
FILEREF
SYSMSG


Chapter Contents

Previous

Next

Top of Page

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