[R] Reading SAS data into R

David Forrest drf5n at maplepark.com
Fri Aug 27 21:52:15 CEST 2004


On Fri, 27 Aug 2004, Sundar Dorai-Raj wrote:

>
> Søren Højsgaard wrote:
>
> > Dear all,
> > One of my students (whom I am trying to convince to use R) wants to get a fairly large SAS dataset into R (about 150mB). An obvious and simple thing she tried was to write the dataset as a .csv-file and then read that into R, but that takes forever (or something close to that..). The dataset is so large, that exporting it as an Excel file from SAS is not feasible (more than 65000 lines). I am reluctant to ask her to go through all the data base steps (then she'll just stick to SAS...). Can anyone help me out on that one?
> > Thanks in advance
> > Søren Højsgaard
> >
> >
>
> See ?read.ssd in package:foreign or ?sas.get in package:Hmisc. Both
> require you have SAS installed and in your PATH.

Those routines use the SAS XPORT procedure, which is a good
semi-non-proprietary way to save your data anyway.  I just fought with
this a long while and found that XPORT needs to write short variable
names, but cannot accept the longer table names.  The short stub of SAS I
needed was:


Given a sas file '/junk/data.sas7bdat' files, the short stub of SAS code should
extract the data to '/junk/rd.xport' (The paths here may be all messed
up, but maybe the options below will save you the hours I lost.)


 libname src2rd '/junk/data'
 libname rd xport '/junk/rd'
 proc copy in=src2rd out=rd;
 select data;

However it may fail if there are long variable names.  The SAS below works
around that:

 options VALIDVARNAME=V7; /* enable longnames of {V6|V7|UPCASE|ANY} */
 data shrtnm;
    set verylonggtablename;
 run;

 However, when longnames are enabled, the XPORT procedure fails on long
variable names, so the option must be set to truncate the long variable
names into shortnames:


 options VALIDVARNAME=V6; /* enable xport varname truncation */
 proc copy in=work out=rd1 memtype=data;
    select shrtnm ;
 run;

Once this is done, the data may be imported into R using read.xport
Library(foreign) :

 x<-read.xport("/junk/x.xport")


R is sooo much better than SAS.

Dave
-- 
 Dave Forrest
 drf at vims.edu                                    (804)684-7900w
 drf5n at maplepark.com                             (804)642-0662h
                                   http://maplepark.com/~drf5n/




More information about the R-help mailing list