|Using External Files under Windows|
|Accessing External Files|
These statements are discussed in the section SAS Statements under Windows, and in the SAS statements section in SAS Language Reference: Dictionary.
You can also specify external files in various SAS dialog box entry fields (for example, as a file destination in the Save As dialog box), the FILENAME function, and in SAS commands, such as FILE and INCLUDE.
In other words, SAS assumes that an external file reference is a standard Windows file specification. If it is not, SAS checks to determine whether the file reference is a fileref (defined by either a FILENAME statement, FILENAME function, or an environment variable). If the file reference is none of these filerefs, SAS assumes 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 indicating 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 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, the FILENAME function, or you can use a Windows environment variable to point to the file. This section discusses the different ways to assign filerefs and also shows you how to obtain a listing of the active filerefs and clear filerefs during your SAS session.
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 listing of active filerefs. To create a new fileref from SAS Explorer:
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 syntax of the FILENAME statement is as follows:
|FILENAME fileref "external-file";|
Then you can use this fileref in your SAS programs. For example, the following statements create a SAS data set named TEST, using the data stored in the external file referenced by the fileref MYDATA:
Note: The words AUX, CON, NUL, PRN, LPT1 - LPT9, and COM1 - COM9 are reserved words under Windows. Do not use these words as filerefs.
You can also use the FILENAME, FILE, and INFILE statements 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 * and ? wildcards can be used in either the external filename or file extension for matching input filenames. 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:
The following example reads all files in the current working directory:
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 illustrated in Advanced External I/O Techniques. For the complete syntax of the FILENAME statement, refer to FILENAME Statement: Windows.
Just as you can define an environment variable to serve as a logical name for a SAS 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 using the SET system option or to define a Windows environment variable using the Windows SET command. Alternatively, you can define environment variables using the System dialog box, accessed from the Control Panel.
Note: The words AUX, CON, NUL, PRN, LPT1 - LPT9, and COM1 - COM9 are reserved words under Windows. Do not use these words as environment variables.
The availability of environment variables makes it simple to assign resources to SAS before invocation. However, the environment variables you define (using the SET system option) for a particular SAS session are not available to other applications.
You can also define SET commands by using System Properties dialog box that you access from the Control Panel.
You must issue all the SET commands that define your environment variables before you invoke SAS. If you define an environment variable in an MS-DOS window, and then start SAS from the Start menu, SAS will not recognize the environment variable.
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 you are referencing, as long as the file extension is the expected one. For example, in the previous example, the INFILE statement expects a file extension of .DAT. The following table lists the expected file extensions for the various SAS statements and commands:
|SAS Command or Statement||SAS Window||File Extension|
SAS searches for a filename TESTPGM.SAS in the directory C:\PROGRAMS.
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 beneath the working directory. You can use the subdirectory name PROGRAMS when referencing files within this directory. For example, the following statement submits the program stored in working-directory \PROGRAMS\PGM1.SAS:
Note: If a directory name is the same as a previously defined fileref, the fileref takes precedence over the directory name.
This statement tells SAS that the fileref PROGS refers to all files stored in both the C:\SAS\PROGRAMS and the D:\MYPROGS directories. When you use the fileref PROGS in your SAS program, SAS looks in these directories for the member you specify. When you use this concatenation feature, you should be aware of the protocol SAS uses, which depends on whether you are accessing the files for read, write, or update. For more information, see Understanding How Concatenated Directories Are Accessed.
The member name must be a valid physical filename. If no extension is given (as in the previous example), SAS 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 the member name is quoted, SAS does not assign an extension, and it looks for the filename exactly as it is given.
You can specify concatenations of files when reading external files from within SAS. Concatenated files consist of two or more file specifications (which might contain wildcard characters) separated by blanks or commas. Here are some examples of valid concatenation specifications:
When you use this concatenation feature, you should be aware of the protocol SAS uses, which depends on whether you are accessing the files for read, write, or update. 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 illustrated in Assigning a Fileref to Concatenated Directories.
SAS supports the use of long filenames. (For more information about valid long filenames, see your Windows operating environment documentation.) You can use long filenames whenever you specify a filename as an argument to a dialog box, command, or any aspect of the SAS language.
When specifying external filenames with the SAS language, such as in a statement or function, you should enclose the filename in double quotation marks to reduce ambiguity (since a single quotation mark is a valid character in a long filename). When you need to specify multiple filenames, enclose each filename in double quotation marks and delimit the names with a blank space.
SAS supports the use of the Universal Naming Convention (UNC) paths. UNC paths let you connect your computer to network devices without having to refer to a network drive letter. SAS supports UNC paths to the extent that Windows and your network software support them. In general, you can refer to a UNC path anywhere in SAS where you would normally refer to a network drive.
If you have assigned several filerefs during a SAS session and need to refresh your memory as to which fileref points where, 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 File Shortcuts. The Explorer window lists all the filerefs active for your current SAS session. Any environment variables you have defined as filerefs are listed, provided you have used them in your SAS session. If you have defined an environment variable as a fileref but have not used it yet in a SAS program, the fileref is not listed in the Explorer window.
|FILENAME fileref|_ALL_ <CLEAR>;|
Note: You cannot clear a fileref that is defined by an environment variable. Filerefs that are defined by an environment variable are assigned for the entire SAS session.
SAS 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. SAS 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 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, SAS writes to the file in the first directory listed in the FILENAME statement, even if a file by the same name exists in a later directory. For example, suppose you input the following FILENAME statement:
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 Windows Filename|
Instead of using a fileref to refer to external files, you can use a quoted Windows filename. For example, if the file C:\MYDIR\ORANGES.SAS contains a SAS program you want to invoke, you can issue the following statement:
When you use a quoted Windows filename in a SAS statement, you can omit the drive and directory specifications if the file you want to reference is located in the working directory. For example, if in the previous example the working directory is C:\MYDIR, you can submit this statement:
You can use several reserved names as quoted physical filenames. Reserved operating system physical names enable you to do a variety of things, such as read data directly from the communications port (such as COM1). The following table lists these physical names and their corresponding device-type keywords:
|Physical Name||Device Type||Use|
|COM1-COM9||COMMPORT||Read/write from the communications port.|
|NUL||DUMMY||Discard data. This name is useful in testing situations.|
options noxwait xsync; data _null_; if symget("sysscpl") = "XP_PRO" 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, you should 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, SAS defaults to using the access protocols for files, not for devices.
For more information about available device-type keywords in the FILENAME statement, see SAS Statements under Windows. Reading Data from the Communications Port discusses the access protocols for using a communications port device.
|Using a File in Your Working Directory|
If you store the external files 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 quotation marks or file extensions, in a SAS statement. For example, if you have a filename ORANGES.SAS stored in your working directory and ORANGES is not defined as a fileref, you can submit the file with the following statement: