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")

2 Responses to “5 Ways to Convert SAS Data to R”

  1. David J Corliss December 14, 2011 at 9:34 am #

    Very informative but I can’t help commenting: why on earth would you want to do such a thing? Transferring R into SAS makes a lot of sense and I’ve been there. Why tranfers data into a system that does less?

    Admittedly, I have run into situations where R functionality exists but SAS does not. For example, a couple months ago, a web search indicated that the preferred technique to do a wavelet transformation in SAS is to use SAS IML to run R code. That’s just embarassing! So, I wrote a wavelet transformation in SAS from scratch (got to get this into my next SAS paper…). Like, this is hard? And I’m not even very good at this. If you already have the data in SAS, find / steal / borrow SAS code to handle it; if all else fails, write some new SAS code. Having some source code in R that does the job can provide instruction manual for how to procede in SAS. I have always found the SAS community very helpful solving new problems, while (in my experience) R code shares the same problems as many other open source code platforms: documentation – when it exisits at all – is obtuse and badly written. As with wavelet transformations, I have found it far easier to look up statistical papers in peer-reviewed journals and translate the processes into new SAS code than to move the SAS data to R and try to figure out something written in R with no documentation or support.

    • Mike September 19, 2012 at 1:00 pm #

      R is “the” choice for deep statistical and probabilistic research…so, if you are ever in need of serious research, use R

Leave a Reply


6 + five =