# Reading Data from External Files - SAS Programming

All of the examples so far have had the raw data included along with the SAS code.You therefore used the DATALINES statement to signal the start of the data.Much of the time your raw data values are stored in a separate file, external to the code.

The only changes you need to make to your SAS program are to include an INFILE statement that identifies the location (file specification) where the data values are stored,and omit the DATA LINES statement (obviously the data lines themselves are also omitted).The file specification can take one of two forms when the data reside external to the file ncontaining the SAS code.

Method 1 - Identifying the Filename Directly with the INFILE Statement

With this method,you identify the external data source directly in the INFILE statement by simply enclosing its name in single quotation marks. Suppose the data for Example is stored in a file called 'C: MYDATAHTWT'.You indicate this in the INFILE statement as follows:

Example

DATA EXTERNAL;
INFILE 'C:MYDATAHTWT';
INPUT ID HEIGHT WEIGHT GENDER $AGE; RUN; The SAS System then reads the data from the C:MYDATAHTWT file, one data line at a time,and applies the INPUT statement to each record as if it had been read instream. Method 2 - Using a Separate FILENAME Statement to Identify anExternal File The other method of identifying the location of the raw data lines is to create a fileref (file reference) by means of a FILENAME statement and then to refer to this fileref in the INFILE statement. Filerefs are not enclosed in quotation marks as are external filenames. Some platforms supply alternate methods for specifying file refs,such as DD cards in MVS or filedefs in CMS.Suppose, for example,you set up a fileref called OSCAR to refer to the file called C:MYDATAHTWT. The previous code could be rewritten as follows, making use of the fileref OSCAR: Example FILENAME OSCAR 'C: MYDATA8TWT'; DATA EXTERNAL; INFILE OSCAR? INPUT ID HEIGHT WEIGHT GENDER$ AGE;
RUN;

Why Filerefs are Useful

Older versions of the SAS System required the use of a fileref; external files could not be directly referenced in the INFILE statement.Although this is not the case with Version 6 of the SAS System,there are times when the use of filerefs can greatly enhance your programming in terms of convenience, efficiency, and power.

Long filenames (fully qualified mainframe data set names can get quite lengthy) can be abbreviated with short descriptive "handles." There are also techniques that can be used to access different data sources dynami cally without the need for manual receding (see Example in this chapter for an example).

Using an INFILE Statement with Instream Data to Specify Options

As you will recall in Example (if you don't,go take a look -- it's been a while),you used the special DATALINES file specification in the INFILE statement to tell the SAS System that the data lines were included instream with the code.

This was only necessary because you wanted to use the DLM= and DSD options of the INFILE state ment on your instream data.In order to use an INFILE statement option,you must have an INFILE statement, and every INFILE state ment must have a file specification.We now show some examples of other options that give you control over reading data from external files.