How to Use the INFILE Statement in SAS (With Example)

The INFILE statement in SAS is used to read external files such as text, raw data, or spreadsheets into a SAS dataset. It can be used to specify input data sources, the format of the data, and the data attributes and options associated with the data. The INFILE statement is followed by a list of options and parameters that control the input process. An example of an INFILE statement is: infile ‘filename.csv’ delimiter=’,’ MISSOVER DSD lrecl=200; This example reads a comma-separated file into SAS with missing values allowed, and a record length of 200.


You can use the INFILE statement to import data from a file into a dataset in SAS.

This statement uses the following basic syntax:

data my_data;
    infile '/home/u13181/bball_data.txt' dlm=' ' dsd missover firstobs=2;
    input team $ position $ points assists;
run;

Here’s what each line does:

  • data: Name to give dataset once imported into SAS
  • infile: Location of file to import
  • dlm: The delimiter that separates values in the file
  • dsd: Treat two consecutive delimiters as a missing value
  • missover: Assume each line in file represents one observation
  • firstobs: Which line in file to consider the first line with observations

The following example shows how to use this function in practice.

Example: How to Use INFILE Statement in SAS

Suppose we have the following text file called bball_data.txt:

We can use the following code to import this file into a SAS dataset called my_data:

/*import data from txt file into SAS dataset*/ 
data my_data;
    infile '/home/u13181/bball_data.txt' dlm=' ' dsd missover firstobs=2;
    input team $ position $ points assists;
run;

/*view dataset*/
proc print data=my_data;

By using the INFILE statement, we were able to successfully import the values from the text file into a dataset.

Notice how we used the following arguments:

  • infile: Specified where the file was located.
  • dlm: Specified that the values in the file were separated by spaces.
  • dsd: Specified that two consecutive delimiters should be treated as a missing value. This came in handy with the missing value in the points column of the first row.
  • missover: Specified that each line in the file represented one observation.
  • firstobs: Specified that the first observation was located on the second row of the file.
  • input: Specified the names to give to the columns in the dataset.

By using each of these arguments, we were able to successfully import the text file into a dataset with the correct format.

How to Import Text Files into SAS

x