Advanced External I/O Techniques

Overview of Advanced External I/O Techniques

This section illustrates how to use the FILENAME, FILE, and INFILE statements to perform more advanced I/O tasks, such as altering the record format and length, appending data to a file, using the DRIVEMAP device-type keyword to determine which drives are available.

Altering the Record Format

Using the RECFM= option in the FILENAME, FILE, %INCLUDE, and INFILE statements enables you to specify the record format of your external files. The following example shows you how to use this option.
Usually, SAS reads a line of data until a carriage return and line feed combination ('0D0A'x) are encountered or until just a line feed ('0A'x) is encountered. However, sometimes data do not contain these carriage–control characters but do have fixed-length records. In this case, you can specify RECFM=F to read your data.
To read such a file, you need to use the LRECL= option to specify the record length and the RECFM= option to tell SAS that the records have fixed-length record format. Here are the required statements:
data test;
   infile "test.dat" lrecl=60 recfm=f;
   input x y z;
run;
In this example, SAS expects fixed-length records that are 60 bytes long, and it reads in the three numeric variables X, Y, and Z.
You can also specify RECFM=F when your data contains carriage returns and line feeds, but you want to read these values as part of your data instead of treating them as carriage-control characters. When you specify RECFM=F, SAS ignores any carriage controls and line feeds and simply reads the record length that you specify.

Appending Data to an External File

Occasionally, you might not want to create a new output file, but rather append data to the end of an existing file. In this case, you can use the MOD option in the FILE statement as in the following example:
filename myfile "c:\sas\data";
data _null_;
   infile myfile(newdata);
   input sales expenses;
   file myfile(jandata) mod;
   put sales expenses;
run;
This example reads the variables SALES and EXPENSES from the external data file C:\SAS\DATA\NEWDATA.DAT and appends records to the existing data file C:\SAS\DATA\JANDATA.DAT.
If you are going to append data to several files in a single directory, you can use the MOD option in the FILENAME statement instead of in the FILE statement. You can also use the FAPPEND function or the PRINTTO procedure to append data to a file. For more information, see the SAS functions section in SAS Functions and CALL Routines: Reference and the PRINTTO procedure in Base SAS Procedures Guide.

Determining Your Drive Mapping

You can use the DRIVEMAP device-type keyword in the FILENAME statement to determine which drives are available for use.
You might use this technique in SAS/AF applications, where you could build selection lists to let a user choose a hard drive. You could also use the DRIVEMAP keyword to enable you to assign macro variables to the various available hard drives.
Using the DRIVEMAP device-type keyword in the FILENAME statement implies you are using the fileref for read-only purposes. If you try to use the fileref associated with the DRIVEMAP device-type keyword in a write or update situation, you receive an error message indicating you do not have sufficient authority to write to the file.
Here is an example using this keyword:
filename myfile drivemap;
data mymap;
   infile myfile;
   input drive $;
   put drive;
run;
The information written to the SAS log looks similar to the information in Drive Mapping Information .
Drive Mapping Information
50   filename myfile drivemap;
51
52   data mymap;
53      infile myfile;
54      input drive $;
55      put drive;
56   run;
NOTE: The infile MYFILE is:
      FILENAME=DRIVEMAP,
      RECFM=V,LRECL=32767
A:
C:
D:
J:
K:
L:
M:
N:
R:
S:
T:
U:
NOTE: 12 records were read from the infile MYFILE.
      The minimum record length was 2.
      The maximum record length was 2.
NOTE: The data set WORK.MYMAP has 12 observations 
      and 1 variables.
NOTE: The DATA statement used 2.04 seconds.

Reading External Files with National Characters

SAS under Windows, like most Windows applications, reads and writes character data using ANSI character codes. Currently, SAS does not provide the option to read or write files using OEM character sets.
Characters such as the  are considered national characters. Windows represents each character with a hexadecimal number. If your external file was created with a Windows editor (including applications such as Word) or in SAS, you do not need to do anything special. Simply read the file using the FILENAME or FILE statements, as you would normally do.