How to import text files into SAS?

To import a text file into SAS, you can use the IMPORT procedure or the DATA step. The IMPORT procedure is the simplest method, as it imports the text file directly into a SAS dataset. The DATA step requires you to specify the data types, lengths and labels for the variables in the text file. Both methods require the text file to be in a supported format, such as a comma-separated values (CSV) file or a tab-delimited text file. Once the text file is imported, the SAS dataset can be used in any SAS procedure.


You can use the PROC IMPORT statement to quickly import data from a text file into SAS.

This procedure uses the following basic syntax:

/*import data from text file called data.txt*/
proc import out=my_data
    datafile="/home/u13181/data.txt"
    dbms=dlm
    replace;
    getnames=YES;
run;

Here’s what each line does:

  • out: Name to give dataset once imported into SAS
  • datafile: Location of text file to import
  • dbms: Format of file being imported (dlm assumes spaces are used as delimiters)
  • replace: Replace the file if it already exists
  • getnames: Use first row as variable names (Set to NO if first row does not contain variable names)

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

Example: Import Text File into SAS

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

We can use the following code to import this dataset into SAS and call it new_data:

/*import data from text file called data.txt*/
proc import out=new_data
    datafile="/home/u13181/data.txt"
    dbms=dlm
    replace;
    getnames=YES;
run;

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

The data shown in the SAS output matches the data shown in the text file.

Note #1: We used getnames=YES when importing the file since the first row of the text file contained variable names.

Note #2: You can find the complete documentation for the PROC IMPORT statement .

The following tutorials explain how to perform other common tasks in SAS:

x