How to move data from SAS to R


Different versions of SAS (i.e. 8.x, 9.2 etc), and on different hardwares (IBM unix mainframe, Windows PC, 32 bit  64bit etc. )
create different SAS data sets. In order to move data sets among different SAS version/machine, you use the format of  SAS transport file.

This SAS transport file format (or xport file) is also used when moving files from SAS to R. (and also to Stata and others)

  1. We first  create a SAS xport file, say    cars2.xpt     and put in the directory  C:/carsdata/    ( the   .xpt   is the file type)
  2. Inside R, first load the library:   library(foreign)  
  3. Then you can read the SAS xport file cars2.xpt  inside R as  read.xport("C:/carsdata/cars2.xpt")
Notice in SAS we use  \   to saparate directory, but in R we use  /  .

Example SAS program to create a SAS xport data file (make sure you have the directory carsdata on drive C:):


LIBNAME in 'C:\carsdata';
 
DATA in.cars1;
  input MAKE $ PRICE MPG REP78 FOREIGN;
DATALINES;
AMC    4099 22 3 0
AMC    4749 17 3 0
AMC    3799 22 3 0
Audi   9690 17 5 1
Audi   6295 23 3 1
BMW    9735 25 4 1
Buick  4816 20 3 0
Buick  7827 15 4 0
Buick  5788 18 3 0
Buick  4453 26 3 0
Buick  5189 20 3 0
Buick 10372 16 3 0
Buick  4082 19 3 0
Cad.  11385 14 3 0
Cad.  14500 14 2 0
Cad.  15906 21 3 0
Chev.  3299 29 3 0
Chev.  5705 16 4 0
Chev.  4504 22 3 0
Chev.  5104 22 2 0
Chev.  3667 24 2 0
Chev.  3955 19 3 0
Datsun 6229 23 4 1
Datsun 4589 35 5 1
Datsun 5079 24 4 1
Datsun 8129 21 4 1
;
RUN;

LIBNAME in 'C:\carsdata';
LIBNAME out XPORT 'C:\carsdata\cars2.xpt';
 
DATA out.cars2;
  SET in.cars1;
RUN;