The process of importing SAS datasets into R is fairly simple. As with most R and SAS tasks, there are multiple approaches to achieving the outcome, and this list is by no means exhaustive. Below we outline five methods to transfer data between SAS and R.
Option 1: The automatic approach using read.ssd()
By entering the data directory, file to import, and SAS executable into the R read.ssd function, import can be accomplished automatically through R.
library(foreign) tbl <- read.ssd("C:/data", "datafile", sascmd="C:/Program Files/SAS Institute/SAS/V9/sas.exe")
Option 2: The automatic approach using sas.get()
The Hmisc package has a similar function called sas.get. This function also uses SAS in the background to help convert data. We’ve found sas.get to be a bit more tricky to use versus the other methods since it requires that the SAS executable be accessible by entering “sas” on the command line.
library(Hmisc) tbl <- sas.get("C:/data", "datafile")
Option 3: The transport-format approach using read.xport
Similar to transferring any SAS dataset between machines, this approach requires that the SAS datasets should be converted into transport format from within SAS. Next, a simple R command is used to import the SAS files.
library(foreign) tbl <- read.xport("sasfile.xport")
Option 4: The XLS approach
Since MS Excel files are so common for small data storage, using them as an intermediate file format for data transfer may be a reasonable approach. Here is how you would import an xls file into R.
library(gdata) tbl <- read.xls("datafile.xls", sheet=1)
Option 5: The delimited file approach
If all else fails, using comma, pipe, or tab delimited files as intermediaries is always an option. Below is the code to import a comma-separated file into R.
tbl <- read.csv("C:/data/datafile.csv")