8

I have a bunch of datasets that are in SAS format. I would like to avoid using SAS since I think R provides more than enough functionality for me. Therefore, is there a package that would allow me to interact with the SAS datasets from R? I have the SAS software installed but I would like to avoid coding things in multiple languages.

Alex
  • 19,533
  • 37
  • 126
  • 195

2 Answers2

14

Since you have SAS, you can use Frank Harrell's 'Hmisc' package which has sas.get and sasxport.get functions. It also has a bunch of utility functions: label,sas.get, contents,describe. For those without a SAS license, package 'foreign' has read.ssd, lookup.xport, and read.xport.

EDIT1: I will also mention that Anthony Joseph Damico recently announced a package to parse SAS INPUT code into read.fwf code. From its description file: " Using importation code designed for SAS users to read ASCII files into sas7bdat files, the SAScii package parses through the INPUT block of a (.sas) syntax file to design the parameters needed for a read.fwf() function call."

EDIT2: There is also a package by Matt Shotwell called 'sas7bdat' with read.sas7bdat(file) that describes its function as " Read SAS files in the sas7bdat data format."

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • `sas7bdat` is experimental. So experimental, in fact, that I've never managed to get it to work -- and I did try. – ktdrv Apr 05 '12 at 23:21
  • Thanks for the warning. I hope you communicated with its author. – IRTFM Apr 06 '12 at 03:29
  • reading sas7bdat works fine for me, even without SAS installed. But I wonder whether there is an opportunity to read SAS source code without installing it. Not run the code, only read it. – Matt Bannert Aug 08 '12 at 15:05
  • Not being a SAS user (since 1999) I have little memory of what "SAS source code" formats would be. I always used text files for my SAS source, (SAS is after just seeing the world as a bunch of punch cards, right), but maybe PC-SAS has added some proprietary format. – IRTFM Aug 08 '12 at 15:17
0

More recently, the haven package can read and write sas7bdat and SAS xpt files. This package is consistent with other import/export packages in the tidyverse.

There is also a package called libr that simulates a SAS libname() function almost exactly. This package is part of a system of packages called sassy that recreates many basic SAS concepts in R.

David J. Bosak
  • 1,386
  • 12
  • 22
  • I wouldn't oversell the "write" part of that - it reads fairly well, but writing is (still) quite limited. SAS7BDAT is a closed format, and anything outside of SAS should not be expected to interact perfectly - though Haven does a good job of reading, at least, for the most part. – Joe Jan 25 '21 at 02:34