How to import CSV Files into SAS (With Examples)

Importing CSV files into SAS is a simple process that can be accomplished by using the “Import Data” task within the SAS program. This task will allow the user to define the data source, specify how the data should be read, and specify the output location of the data. Examples of how to import a CSV file into SAS can be found online, and can provide a helpful guide for users who are unfamiliar with the process.


You can use proc import to quickly import data from a CSV file into SAS.

This procedure uses the following basic syntax:

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

Here’s what each line does:

  • out: Name to give dataset once imported into SAS
  • datafile: Location of CSV file to import
  • dmbs: Format of file being imported
  • 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 examples show how to use this function in practice.

Related:

Example 1: Import Data from CSV File into SAS

Suppose we have the following CSV file called my_data.csv:

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

/*import data from CSV file called my_data.csv*/
proc import out=new_data
    datafile="/home/u13181/my_data.csv"
    dbms=csv
    replace;
    getnames=YES;
run;

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

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

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

Example 2: Import Data from CSV File into SAS with No Header and Custom Delimiter

Suppose we have the following CSV file called data.csv:

Notice that this file has no header row and the values are separated by semi-colons instead of commas.

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

/*import data from CSV file called data.csv*/
proc import out=new_data
    datafile="/home/u13181/data.csv"
    dbms=csv
    replace;
    delimiter=";";
    getnames=NO;
run;

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

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

By default, SAS provides the variable names as VAR1, VAR2, and VAR3.

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

x