The easiest way to import SAS files into R is to use the read_sas() function from the haven library.
This function uses the following basic syntax:
data 'C:/Users/User_Name/file_name.sas7bdat')
The following step-by-step example shows how to import a SAS file into R in practice.
Step 1: Download a SAS Data File
For this example, we’ll download the SAS file called cola.sas7bdat from this page.
Step 2: Install haven Package
Next, we’ll install the haven package in R:
install.packages('haven')
We’ll then load the package:
library(haven)
Step 3: Import the SAS File
Next, we’ll use the read_sas() function to import the SAS file:
data C:/Users/bob/Downloads/cola.sas7bdat')
Once we’ve imported the SAS file, we can get a quick summary of the data:
#view class of data
class(data)
[1] "tbl_df" "tbl" "data.frame"
#display dimensions of data frame
dim(data)
[1] 5466 5
#view first six rows of data
head(data)
ID CHOICE PRICE FEATURE DISPLAY
1 1 0 1.79 0 0
2 1 0 1.79 0 0
3 1 1 1.79 0 0
4 2 0 1.79 0 0
5 2 0 1.79 0 0
6 2 1 0.890 1 1
We can see that the file imported successfully as a data frame and that it has 5 columns and 5,466 rows.
Additional Resources
The following tutorials explain how to import other file types into R:
How to Import CSV Files into R
How to Import Excel Files into R
How to Import .dta Files into R
How to Import SPSS Files into R