How to Import CSV Files into SAS (With Examples)


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: How to Import Excel Files into SAS

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.

Additional Resources

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

How to Export Data from SAS to Excel File
How to Export Data from SAS to CSV File

Leave a Reply

Your email address will not be published.