Chapter Contents |
Previous |
Next |
SAS Companion for the OS/2 Environment |
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:
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 |
File | New... |
To assign a file shortcut by using the My Favorite Folders window:
You can then use these file shortcuts in your SAS programs.
Note: File shortcuts are active only during the current
SAS session.
Note: The syntax of the
FILENAME function is similar to the FILENAME statement. For information about
the FILENAME function, see SAS Language Reference: Dictionary.
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;
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.
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.
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.
-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.
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.
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.
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.
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.
progs(member1)
The SAS System uses the following set of rules to resolve this external file reference. This list represents the order of precedence:
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.
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
Note: The FSLIST procedure in FSP does not support concatenated
files.
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.
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:
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";
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;
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:
Edit | Select All |
Edit | Delete |
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.
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.
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;
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)
Note: You should not use concatenated files with some
procedures, such as the FSLIST procedure.
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;
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 |
%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";
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:
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.