Last updated on December 9, 2019
In this post, we are going to learn how to read SAS (. sas7bdat) files 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:
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 an SAS file as well.
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.
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’s 3 steps to open SAS files in R:
1) Install haven
2) Load the r-package haven:
3) Open the SAS 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 Open a SAS file in R
In this section, we are going to learn how to import data in R. However, before we use, the above mentioned, r-packages we need to make sure we have it installed.
How to install r-packages:
Installing r-packages is quite easy. Below, we will learn about two methods.
- Install r packages using the install.packages() function:
Open up RGui (or RStudio) and type the following in the console:
- Install using Conda:
Open the Anaconda Prompt and type
conda install -c conda-forge r-haven r-sas7bdat r-rio
How to Read a .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.
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():
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.
df <- read_sas("airline.sas7bdat") head(df)
How to 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:
As in the previous read SAS in R example, we are now ready to use the
Here, we print the 5 last rows of the dataframe using Pandas tail method.
df <- read.sas7bdat("airline.sas7bdat")
How to Import SAS Fils 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.
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…:
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:
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:
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:
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.
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:
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.