Previous Page | Next Page

Using External Files and Devices

Specifying Pathnames in UNIX Environments


Rules for Specifying Pathnames

You can reference an external file directly by specifying its pathname in the FILE, INFILE, or %INCLUDE statements, or you can reference the file indirectly by specifying a fileref and a pathname in the FILENAME statement and then using the fileref in the FILE, INFILE, or %INCLUDE statements.

Whether you reference a file directly or indirectly, you need to specify its pathname in the appropriate statement. In most cases, you must enclose the name in quotation marks. For example, the following INFILE statement refers to the file /users/pat/cars:

infile '/users/pat/cars';

The following FILE statement directs output to the specified computer:

file '/dev/ttyp1';

Note:   If a filename has leading blanks, then the blanks will be trimmed.  [cautionend]

The level of specification depends on your current directory. You can use the character substitutions shown in Character Substitutions in Pathnames to specify the pathname. You can also use wildcards as described in Using Wildcards in Pathnames (Input Only).


Omitting Quotation Marks in a Filename

You can omit the quotation marks in a filename if one of the following is true:

For example, if the current directory is /users/mkt/report and it includes file qtr.sas , you can reference qtr.sas in any of the following statements:

%include '/users/mkt/report/qtr.sas';
%include 'qtr.sas';
file 'qtr.sas';

If there is no qtr fileref already defined, you can omit the quotation marks and the filename extension in the %INCLUDE statement:

%include qtr;


Working with Mixed Case or Uppercase Filenames

Filenames in the UNIX operating environment are case sensitive. This means that a file named PROGRAM is not the same as a file named program. When you reference the name of a file that is written in mixed case or uppercase, and that filename is not enclosed in quotation marks, SAS converts the filename to lowercase. If the filename does not have a file extension, SAS adds the missing file extension.

For example, if you specify %include code(PROGRAM); in your program, SAS converts the filename PROGRAM to lowercase, and adds an extension of .sas to the filename. PROGRAM becomes program.sas.


Interpreting the Messages in the SAS Log

When you execute the following program, SAS converts TEMP to temp, and adds an extension of .sas to the filename:

filename inc_code 'your-directory';
%include inc_code(TEMP);

SAS writes the following messages to the SAS log:

WARNING: Physical file does not exist, A.../your-directory/TEMP.sas.
ERROR: Cannot %INCLUDE member TEMP in the aggregate INC_CODE.

The warning message shows only the original filename (TEMP.sas), and not the lowercase conversion (temp.sas). This situation might cause confusion if a file named TEMP.sas does exist.

To avoid this confusion, include the file extension with the filename if the filename contains an extension, or enclose the mixedcase or uppercase filename in quotation marks if the filename does not have an extension. For example:

%include code(TEMP.sas);
%include code("TEMP");

In both of these cases, SAS does not convert TEMP to lowercase.


Using Wildcards in Pathnames (Input Only)


Descriptions of the Valid Wildcards

You can use the *, ?, and [ ] wildcards to specify pathnames in the FILENAME (only if the fileref is to be used for input), INFILE, and %INCLUDE statements and the INCLUDE command.

*

matches one or more characters, except for the period at the beginning of filenames.

?

matches any single character.

[ ]

matches any single character from the set of characters defined within the brackets. You can specify a range of characters by specifying the starting character and ending character separated by a hyphen.

Wildcards are supported for input only. You cannot use wildcards in the FILE statement.

Example 1: Selecting Files by Including a Wildcard in a String

The following example 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;


Example 2: Reading Each File in the Current Directory

The following example reads input from every file in every subdirectory of the current working directory:

filename subfiles '*/*';
data;
   infile subfiles;
   input;
run;

If new files are added to any of the subdirectories, they can be accessed with the Subfiles fileref without changing the FILENAME statement.


Example 3: Wildcards in Filenames When Using Aggregate Syntax

You can also use wildcards in filenames, but not in directory names, when you use aggregate syntax:

filename curdir ".";
data;
  infile curdir('wild*');
  input;
run;

In the example above, the period in the FILENAME statement refers to the current directory. See Character Substitutions in Pathnames for information about other character substitutions available on UNIX.


Example 4: Associating a Fileref with Multiple Files

The following statement associates the fileref MyRef with all files that begin with alphabetic characters. Files beginning with numbers or other characters such as the period or tilde are excluded.

filename myref '[a-zA-Z]*.dat';

The following statement associates MyRef with any file beginning with Sales (in either uppercase, lowercase, or mixed case) and a year between 1990 and 1999:

filename myref '[Ss][Aa][Ll][Ee][Ss]199[0-9].dat';

Previous Page | Next Page | Top of Page