How to read and write SAS (.sas7bdat or xpt) files or import data from and export data to SAS in R, using read.sas7bdat(), read.xport() and write.foreign().

Below are the functions and the packages you need to install.

Packages and Functions for Reading and Writing SAS Files in R
Activity Package Function
Read SAS .sas7bdat sas7bdat read.sas7bdat()
Read SAS .xpt foreign read.xport()
Write SAS foreign write.foreign()

1 Read or Import Data from SAS SAS7BDAT Files in R

Download testdata.sas7bdat file.

The SAS .sas7bdat file format is one of the data storage file formats created by Statistical Analysis System (SAS) software.

To read or import SAS .sas7bdat files in R, install the "sas7bdat" package:

install.packages("sas7bdat")

Then load the package with the following:

library("sas7bdat")

After the installation and loading of the package, if you set or have the working directory as the folder containing the file, you can read or import the .sas7bdat file with the following line of code:

read.sas7bdat("testdata.sas7bdat")

Or specify the full path of where the file is located:

read.sas7bdat("C:/Users/Public/Statscodes/Rdata/read-files/testdata.sas7bdat")

It should look like below in R. The alphanumeric "ID" column in other examples is removed for readability.

SAS7BDAT File Read in R

SAS7BDAT File Read in R

2 Read or Import Data from SAS XPT Files in R

Download testdata.xpt file.

The SAS .xpt file format is the transport file format created by Statistical Analysis System (SAS) software.

To read or import SAS .xpt files in R, install the "foreign" package:

install.packages("foreign")

Then load the package with the following:

library("foreign")

After the installation and loading of the package, you can read or import the .xpt file with the following line of code if it is in the working directory:

read.xport("testdata.xpt")

Or specify the full path:

read.xport("C:/Users/Public/Statscodes/Rdata/read-files/testdata.xpt")
SAS XPT File Read in R

SAS XPT File Read in R

3 Write or Export Data to SAS in R

dtfrm = data.frame(Group = c("A", "B", "B", "C", "D"), 
                   ID = c("A02", "B12", "B15", "C04", "D07"), 
                   Score = c(9, 8, 8, 10, 7), 
                   Position = c(2, 3, 3, 1, 5))
dtfrm
  Group  ID Score Position
1     A A02     9        2
2     B B12     8        3
3     B B15     8        3
4     C C04    10        1
5     D D07     7        5

To write or export data to SAS in R, first install the "foreign" package:

install.packages("foreign")

Then load the package with the following:

library("foreign")

After the installation and loading of the "foreign" package, you can save the two output files to the working directory with the following line of code:

The outputs saved are "datafile.txt", containing the data in text format, and "code.sas", containing the code to be used to import the dataframe in the SAS environment.

write.foreign(dtfrm, "datafile.txt", "code.sas", package="SAS")
TXT Data and SAS Code Written in R

TXT Data and SAS Code Written in R

The output will look like this when the code is run in SAS Studio:

Dataframe in SAS Studio

Dataframe in SAS Studio

Copyright © 2020 - 2024. All Rights Reserved by Stats Codes