Chapter Contents


SAS Companion for the Microsoft Windows Environment

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 hard 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, the SAS System 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 the SAS System 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;

In this example, the SAS System 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 do contain 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, the SAS System ignores any carriage controls and line feeds and simply reads the record length you specify.

Appending Data to an External File

Occasionally, you may 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;

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 Language Reference: Dictionary and the PRINTTO procedure section in SAS Procedures Guide.

Determining Your Hard Drive Mapping

You can use the DRIVEMAP device-type keyword in the FILENAME statement to determine which hard drives are available for use. Here is an example using this keyword:

filename myfile drivemap;
data mymap;
   infile myfile;
   input drive $;
   put drive;

The information written to the SAS log looks similar to that shown in Drive Mapping Information.

Drive Mapping Information
50   filename myfile drivemap;
52   data mymap;
53      infile myfile;
54      input drive $;
55      put drive;
56   run;
NOTE: The infile MYFILE is:
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.

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.

Reading External Files with National Characters

The SAS System under Windows, like most Windows applications, reads and writes character data using ANSI character codes. In Version 8, 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 WordPerfect) or in the SAS System under Windows, you do not need to do anything special. Simply read the file using the FILENAME or FILE statements, as you would normally do.

Chapter Contents



Top of Page

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