Chapter Contents

Previous

Next
SAS Companion for the OS/2 Environment

Referencing External Files

To access external files, you must tell the SAS System 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 later in this section and in SAS Statements under OS/2, as well as in the SAS language statements section in SAS Language Reference: Dictionary.

You can also specify external files in various SAS dialog entry fields (for example, as a file destination in the Save As dialog box), in the FILENAME function, and in SAS commands, such as FILE and INCLUDE.

Depending on the context, SAS can reference an external file by using

The following sections discuss these methods of specifying external files.

Because there are several ways to specify external files in the SAS System, SAS uses a set of rules to resolve an external file reference. The following list represents the order of precedence:

  1. Check for a standard OS/2 file specification that is enclosed in quotes.

  2. Check for a fileref that is 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, the SAS System assumes that an external file reference is a standard OS/2 file specification. If it is not, SAS checks to see if the file reference is a fileref (defined by either a FILENAME statement, FILNAME function, or an environment variable). If the file reference is none of these, SAS assumes that 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 that indicates that it cannot access the external file.


Using a Fileref

One way to reference external files is with a fileref. A fileref is a logical name that is 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, and the FILENAME function, or you can use an OS/2 environment variable to point to the file. This section discusses the different ways to assign filerefs. It also shows you how to obtain a listing of the active filerefs and clear filerefs during your SAS session.

Assigning a File Shortcut

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 list of active filerefs. To create a new fileref from SAS Explorer:

  1. Select the File Shortcuts folder and then select

    File
    [arrow]
    New...

  2. In the New... dialog box, select File Shortcuts and click on [OK].

  3. In the New File Shortcut window, type the name of the shortcut (fileref) and the path to the SAS file that the shortcut represents.

To assign a file shortcut by using the My Favorite Folders window:

  1. Open the folder that contains the file.

  2. Position the cursor over the file, click with the right mouse button and select Create File Shortcut.

  3. In the Create File Shortcut dialog box, type the name of the file shortcut and click on [OK].

You can then use these file shortcuts in your SAS programs.

Note:   File shortcuts are active only during the current SAS session.  [cautionend]

Using the FILENAME Statement

The FILENAME statement enables you to associate a logical name with an external file, directory, or device.

Note:   The syntax of the FILENAME function is similar to the FILENAME statement. For information about the FILENAME function, see SAS Language Reference: Dictionary.  [cautionend]
The simplest form of the FILENAME statement is as follows:

FILENAME fileref "external-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 that is named TEST, by using the data that are stored in the external file that is referenced by the fileref MYDATA:

data test;
   infile mydata;
   input name $ score;
run;
CAUTION:
Under OS/2, the words CON, NUL, KBDS, PRN, COM1 - COM3, and LPT1 - LPT3 are reserved words. Do not use these reserved words as a fileref, libref, variable name, or macro variable.  [cautionend]

You can also use the FILENAME statement 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 asterisk (*) and question mark (?) wildcards can be used in either the external filename or in the file extension to match input file names. 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 shown in Advanced External I/O Techniques. For the complete syntax of the FILENAME statement, see to FILENAME.

Using Environment Variables

Just as you can define an environment variable to serve as a logical name for a SAS data 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 by using the SET system option or to define an OS/2 environment variable by using the OS/2 SET command.

CAUTION:
Under OS/2, the words CON, NUL, KBDS, PRN, COM1 - COM3, and LPT1 - LPT3 are reserved words. Do not use these reserved words as an environment variable.  [cautionend]

The availability of environment variables makes it simple to assign resources to the SAS System prior to 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 the SAS System through a program group window.

Using the SET command

An alternative to using the SET system option to define an environment variable is to use the OS/2 SET command. For example, the OS/2 SET command that is equivalent to the previous example is

SET MYVAR=C:\MYDATA\TEST.BAT

You must issue all the SET commands that define your environment variables before you invoke the SAS System.

When you reference an external environment variable (one that is assigned with the OS/2 SET command) in your SAS programs (such as in a FILE statement), a note that informs you that the environment variable has been used and that the fileref has been assigned is written to the SAS log.

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 that are 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 instance, in the previous example, the INFILE statement expects a file extension of .DAT. Default File Extensions for Referencing External Files with Member Name Syntax lists the expected file extensions for the various SAS statements and commands, and lists the window in which the statement or command is used.

Default File Extensions for Referencing External Files with Member Name Syntax
SAS Command or Statement SAS Window File Extension
FILE statement Program

Editor

.DAT
%INCLUDE statement Program

Editor

.SAS
INFILE statement Program

Editor

.DAT
FILE command Program

Editor

.SAS
FILE command Log .LOG
FILE command Output .LST
FILE command NOTEPAD none
INCLUDE command Program

Editor

.SAS
INCLUDE command NOTEPAD none

For example, the following program submits the file C:\PROGRAMS\TESTPGM.SAS to the SAS System:

filename test "c:\programs";
%include test(testpgm);
The SAS System searches for a file that is named TESTPGM.SAS in the directory C:\PROGRAMS.

If your file has a file extension that is 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 quotes, as in the following example:

filename test "c:\programs";
%include test("testpgm");

For more illustrations of the default file extensions that the SAS System uses, see the following examples. 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. As an example, suppose you have a subdirectory named PROGRAMS that is located in a subdirectory of the working directory. You can use the subdirectory name PROGRAMS when referencing files within this directory. For example, the following statement submits the program that is 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.  [cautionend]

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 the SAS System that the fileref PROGS refers to all files that are stored in both the C:\SAS\PROGRAMS and the D:\MYPROGS directories. When you use the fileref PROGS in your SAS program, the SAS System looks in these directories for the member that you specify. When you use this concatenation feature, you should be aware of the protocol that the SAS System uses, which depends on whether you are accessing the files to read, write, or update them. For more information, see Understanding How Concatenated Directories Are Accessed.

Summary of Rules for Resolving Member Name Syntax

The SAS System 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)

The SAS System 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 that is defined by a FILENAME statement.

  2. Check for a SAS or OS/2 environment variable that is named PROGS.

  3. Check for a directory that is 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), the SAS System 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 if the member name is quoted, the SAS System 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 you are reading external files from within the SAS System. Concatenated files consist of two or more file specifications (which may contain wildcard characters) that are separated by blanks or commas. The following are some examples of valid concatenation specifications:

Note:   The FSLIST procedure in FSP does not support concatenated files.  [cautionend]

When you use this concatenation feature, note the protocol that the SAS System uses; the protocol depends on whether you are accessing the files in order to read, write, or update them. 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 shown in Assigning a Fileref to Concatenated Directories.  [cautionend]

Referencing External Files with Long Filenames

The SAS System under OS/2 supports the use of long filenames on disk volumes that are formatted with the high-performance file system (HPFS). You can use long filenames whenever you specify a command, or an element of the SAS language, or a filename as an argument to a dialog box. For more information about valid long filenames, see OS/2 Environment: Changes and Enhancements to the SAS System and your OS/2 operating environment documentation.

When you are specifying external filenames by using the SAS language, such as specifying the filename in a statement or function, enclose the filename in double quotes to reduce ambiguity (a single quote is a valid character in a long filename). When you need to specify multiple filenames, enclose each filename in double quotes and delimit the names with a blank space.

Here are some examples of valid uses of long filenames within SAS:


Listing Fileref Assignments

If you have assigned several filerefs during a SAS session and need to remember which fileref points to which file, 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 on the File Shortcuts icon. The Explorer window lists all the filerefs that are active for your current SAS session. Any environment variables that you have defined as filerefs are listed, provided that you have used them in your SAS session. If you have defined an environment variable as a fileref but have not yet used it in a SAS program, the fileref is not listed in the Explorer window.

If you are running the SAS System in batch mode, 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 submitting the FILENAME statement and by using the following syntax:

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 by using SAS Explorer:

  1. double-click on File Shortcuts

  2. select the file shortcuts that you want to delete. To select all file shortcuts, select

    Edit
    [arrow]
    Select All

  3. press the DELETE key or select

    Edit
    [arrow]
    Delete

  4. Click on [OK] in the message box to confirm deletion of the file shortcuts.

Note:   When you clear a fileref that is defined by an environment variable, the variable remains defined, but it is no longer considered a fileref. That is, it is no longer listed in the SAS Explorer window. You can use the variable in another FILENAME statement to create a new fileref.  [cautionend]

The SAS System 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. The SAS System 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 that has more than one physical directory, the file that is accessed depends upon whether you want access to it for input, output, or update.

Input and update

If the file is opened for input or update, the first file that is 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, the SAS System writes to the file in the first directory that is listed in the FILENAME statement, even if a file by the same name exists in a later directory. For example, suppose that you execute the following FILENAME statement:

filename test ("c:\sas","d:\mysasdir");

Then, if 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 that has more than one physical file, the behavior of SAS statements and commands depends on whether you are accessing the files for input, output, or update.

Note:   You should not use concatenated files with some procedures, such as the FSLIST procedure.  [cautionend]

Input

If the file is opened for input, data from all files are input. 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 and update

If the file is opened for output or update, 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 OS/2 Filename

Instead of using a fileref to refer to external files, you can use a quoted OS/2 filename. For example, if the file C:\MYDIR\ORANGES.SAS contains a SAS program that you want to run, you can issue the following statement:

%include "c:\mydir\oranges.sas";

When you use a quoted OS/2 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 instance, if in the previous example the working directory is C:\MYDIR, you can submit the following statement:

%include "oranges.sas";

Using Reserved Operating Environment Physical Names

You can use several reserved names as quoted physical filenames. Reserved operating environment physical names enable you to do a variety of things, such as read data directly from the communications port (such as COM1).Reserved OS/2 Physical Names lists these physical names and their corresponding device-type keywords:

Reserved OS/2 Physical Names
Physical Name Device Type Use
COM1-COM4 COMMPORT Read or write from the communications port.
NUL DUMMY Discard data. This name is useful in testing situations.

You can specify operating environment physical names with or without a colon. For example, you can specify either COM1: or COM1. For additional information about physical names, see your OS/2 documentation.

The following example demonstrates how to capture data from an external device or application that is transmitting data via a serial (RS-232C port).

options noxwait sxync;
data _null_;
   if symget("sysscpl") = "OS2" then
          rc = system("mode COM1:9600,n,8,1,xon=on");
      stop;
run;

filename commdata commport "COM1:";

data fruit;
   keep num type;
   infile commdata unbuffered;
   file commdata;
   put "ready";
   input totrecs records $;
   if totrecs = . or records ne "RECORDS" then
     do;
       file log;
       put "ERROR: Unable to determine
             number of records to read.";
       stop;
     end;
   do i = 1 to totrecs;
     input num type $;
     output;
     put "NEXT";
   end;
   stop
run;

Note the use of the device-type keyword COMMPORT in the FILENAME statement in this example. Because the access protocols for devices are slightly different from the access protocols for files, always use the appropriate device-type keyword in combination with the reserved physical name in the FILENAME statement. If you do not use a device-type keyword, the SAS System defaults to using the access protocols for files, not for devices.

As an alternative to using a device-type keyword in the FILENAME statement, you can use the DEVICE= option in the %INCLUDE statement. Although this technique serves the same purpose as the device-type keyword, it is not as flexible an approach as using a device-type keyword in the FILENAME statement. For example, if you use a fileref in a PROC PRINTTO statement, no DEVICE= option is available.

For more information about available device-type keywords in the FILENAME statement and for valid values for the DEVICE= option in the %INCLUDE statement, see the descriptions of these statements in SAS Statements under OS/2. For a discussion of the access protocols for using a communications port device, see Reading Data from the Communications Port.


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 quotes or file extensions, in a SAS statement. For example, if you have stored a file that is named ORANGES.SAS 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

For more information about how to determine and change the SAS working directory, see Setting the Current Folder and Changing the SAS Current Folder.


Chapter Contents

Previous

Next

Top of Page

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