How to Import Data: Reading SAS Files in R with Haven & sas7dbat

In this post, we are going to learn how to read a SAS (. sas7bdat) file in R. More specifically, we are going to use the packages haven, and sas7bdat. Furthermore, we will also learn how to load .sas7bdat files into R using RStudio.

If you are interested in other methods on how to import data in R:

Table of Contents

Can R Read SAS Files?

Now we may want to answer the question Can R Read SAS Files? In R, there are a couple of that can read SAS files into dataframes. In this post, we are going to use the r-packages haven, sas7bdat, and the GUI of RStudio to load a SAS file as well.

Haven can Read SAS Files

The first package we are going to use is the Haven package. Haven is part of the Tidyverse and supports SPSS, Stata, and SAS files in R. In this read SAS files in R tutorial, we will use the functions read_sas and write_sas to read and write . sas7bdat files, respectively.

sas7bdat can Import SAS Files

The second package we are going to use is the sas7bdat package. This package was written for the sole purpose of reading SAS files in R.

Now, before going on and describing how to use Have and sas7bdat to import SAS file in R, we are going to quickly look on how to open SAS files using Haven:

Can R open SAS files?

As you already may have understood; yes, R can open SAS files. Here are 3 steps to open SAS files in R:
1) Install haven install.packages(“haven”)
2) Load the r-package haven: require(haven)
3) Open the SAS file read_sas(PATH_TO_YOUR_SAS7BDAT_FILE)
Note, this assumes that R is already installed on your computer and read the post to get more information on how to read SAS files in R.

How to install r-packages:

Installing r-packages is quite easy. Below, we will learn about two methods.

  1. Install r packages using the install.packages() function:
    Open up RGui (or RStudio) and type the following in the console:
    How to install R packages
    • Save

    install.packages(c("haven", "sas7bdat"))
  2. Install using Conda:
    Open the Anaconda Prompt and type conda install -c conda-forge r-haven r-sas7bdat r-rio

How to Read a SAS (.sas7bdat) File in R into a DataFrame

In this section, we are going to learn how to import data into R. First, we are going to import data in R using the haven package. After this, we are going to use the sas7bdat package to read a .sas7bdat file into R. Finally, we are going to do the same using the rio package.

Method 1: Load a SAS file in R using Haven

In this section, we are going to use haven. As previously mentioned, we will use the read_sas function to read the . sas7bdat file.

First, we need to load the r-package. This is done by using either the library or the require function. In the code chunk below we use require():

require(haven)Code language: R (r)
  • Save

Now that the required package is loaded into R, we are ready to import the SAS file. In this read SAS file tutorial, we are using the data that can be downloaded here. Here’s how to read the SAS file with the R-package haven:

# importing the SAS file:
df <- read_sas("airline.sas7bdat")
head(df)Code language: R (r)
read sas in r
  • Save

In the data, we now have, there is a column named “Year”. If we, in another dataset for example, had a column with datetime information (e.g., year, day, month, and timestamps) we can separate these elements. Have a look at the recent posts:

Method 2: Read a SAS file with R Using sas7bdat

In this section, we are going to load the same .sav7bdat file into R using sas7bdat, instead of read_sas.

Before we continue, we need to load the sas7bdat package:

require(sas7bdat)Code language: JavaScript (javascript)

As in the previous read SAS in R example, we are now ready to use the sas7bdat function to import a SAS file into R’s dataframe. Here’s how to open a .sas7bdat file:

# Reading the SAS file:
df <- read.sas7bdat("airline.sas7bdat")Code language: PHP (php)
read sas in r
  • Save

Note, it is also possible to read Stata (.dta) files in R using Haven.

How to Import a SAS File in RStudio

In this section, we are going to learn how to read . sas7bdat files using RStudio. If we have some reason for not writing (all) of the R code ourselves this might be handy. Note that, RStudio will use the package haven to load SAS files.

Step 1: Choose Fram SAS

First, we open up RStudio and go to the Environment tab on the right. In this tab, we click on the Import Dataset dropdown menu, and choose From SAS…:

read sas in r using RStudio
  • Save

Step 2: Find the SAS File

Next, we click on Browse, go to the folder where we have the SAS file that we want to import to R. When this is done, we click on open:

  • Save

Step 3: Name the Dataframe and Import the SAS File

We are almost ready to import the SAS file in R. In fact, we can see our dataset, in a preview, now. Here, we can also name the dataframe and then click Import to load the SAS file into R:

how to read sas in R with RStudio
  • Save

Now, RStudio will load the haven package and use run View function so we can see the entire dataset. This means that we have successfully loaded a SAS file in R:

read sas file in R
  • Save

The next step after importing data into R (e.g., from a SAS, SPSS, or .xlsx file) may be to drop columns that we don’t need (see the recent blog post on how to remove columns by name in R using dplyr). After we have cleaned our data a bit we may want to do descriptive statistics in R and visualize our data (see this post on how to make scatter plots in R using ggplot2()). Finally, before setting up our statistical model, we may need to dummy code the categorical variables, if we have any. In a recent post, you will learn how to code dummy variables in R.

How to Write a SAS file in R

Now we know how to read a SAS file using R and are going to learn how to save a SAS file. Here, we will use the r-package haven and it’s function write_sas.

write_sas(df, "sasfile.sas7bdat")Code language: R (r)
reading sas files in R
  • Save

How to Save a SAS file to CSV

In this section of the R SAS tutorial, we are going to save the .sas7bdat file to a .csv file. This is easily done, we just have to use the write.csv function that is part of base R:

write.csv(df, "SASData.csv"))Code language: R (r)

Remember to put the right path, as the second argument, when using write to save a .sas7bdat file as CSV. That is, save it somewhere where you can find it.

Summary: Read SAS Files using R

In this post, we have learned how to read and write SAS files in R. More specifically, we used the packages haven and sas7bdat to load SAS files into R. As a bonus, we also learned how to use RStudio to import data files from SAS. Finally, we learned how to save a dataframe as a SAS file and a CSV file.

Tutorials

  • Save

2 thoughts on “How to Import Data: Reading SAS Files in R with Haven & sas7dbat”

  1. There is a mistake in the above section “How to Write a SAS file in R”.

    cwrite_sas(df, “sasfile.sas7bdat”)

    there is no function named cwrite_sas exists.

    Please change it to write_sas.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top
Share via
Copy link