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.
Activity | Package | Function |
Read SAS .sas7bdat | sas7bdat | read.sas7bdat() |
Read SAS .xpt | foreign | read.xport() |
Write SAS | foreign | write.foreign() |
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:
Then load the package with the following:
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:
Or specify the full path of where the file is located:
It should look like below in R. The alphanumeric "ID" column in other examples is removed for readability.
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:
Then load the package with the following:
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:
Or specify the full path:
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:
Then load the package with the following:
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.
The output will look like this when the code is run in SAS Studio:
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes