reading version 9 SAS datasets in R
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of jude.ryan at ubs.com Sent: Wednesday, December 03, 2008 1:40 PM To: r-help at r-project.org Cc: juderyan61 at yahoo.com Subject: [R] reading version 9 SAS datasets in R Hi, I am trying to read a SAS version 9.1.3 SAS dataset into R (to preserve the SAS labels), but am unable to do so (I have read in a CSV version). I first created a transport file using the SAS code: libname ces2 'D:\CES Analysis\Data'; filename transp 'D:\CES Analysis\Data\fadata.xpt'; /* create a transport file - R cannot read file created by proc cport */ proc cport data=ces2.fadata file=transp; run; I then tried to read it in R using:
library(foreign)
library(Hmisc)
fadata2 <- sasxport.get("D:\\CES Analysis\\Data\\fadata.xpt")
Error in lookup.xport(file) : file not in SAS transfer format
Next I tried using the libname statement and the xport engine
to create
a transport file. The problem with this method is that variable names
cannot be more than 8 characters as this method creates a SAS
version 6
transport file.
libname to_r xport 'D:\CES Analysis\Data\fadata2.xpt';
data to_r.fadata2;
set ces2.fadata;
run;
But I get an error message in the SAS log:
493 libname to_r xport 'D:\CES Analysis\Data\fadata2.xpt';
NOTE: Libref TO_R was successfully assigned as follows:
Engine: XPORT
Physical Name: D:\CES Analysis\Data\fadata2.xpt
494
495 data to_r.fadata2;
496 set ces2.fadata;
497 run;
ERROR: The variable name BUS_TEL_N is illegal for the version 6 file
TO_R.FADATA2.DATA.
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set TO_R.FADATA2 was only partially opened and will
not be saved.
Next I tried other ways of reading a SAS dataset in R, as shown below:
fadata2 <- sas.get("D:\\CES Analysis\\Data", mem=fadata)
Error in sas.get("D:\\CES Analysis\\Data", mem = fadata) :
Unix file, "D:\CES Analysis\Data/c(1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, .sd2 D:\CES
Analysis\Data/c(NA, 64716,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 64716, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
In addition: Warning message:
In sas.get("D:\\CES Analysis\\Data", mem = fadata) :
D:\CES Analysis\Data/formats.sc? or formats.sas7bcat not found.
Formatting ignored.
ls()
[1] "fadata"
?read.xport
fadata2 <- read.xport("D:\\CES Analysis\\Data\\fadata.xpt")
Error in lookup.xport(file) : file not in SAS transfer format
?read.ssd
fadata2 <- read.ssd("D:\\CES Analysis\\Data", "fadata")
SAS failed. SAS program at
D:\DOCUME~1\re06572\LOCALS~1\Temp\RtmpLqCVUx\file72ae2cd6.sas
The log file will be file72ae2cd6.log in the current directory
Warning messages:
1: In system(paste(sascmd, tmpProg)) : "sas" not found
2: In read.ssd("D:\\CES Analysis\\Data", "fadata") :
SAS return code was -1
sashome <- "C:\\Program Files\\SAS\\SAS 9.1"
fadata2 <- read.ssd(file.path(sashome, "core", "sashelp"), "fadata",
sascmd=file.path(sashome, "sas.exe")) SAS failed. SAS program at D:\DOCUME~1\re06572\LOCALS~1\Temp\RtmpLqCVUx\file6df11649.sas The log file will be file6df11649.log in the current directory Warning message: In read.ssd(file.path(sashome, "core", "sashelp"), "fadata", sascmd = file.path(sashome, : SAS return code was 2
Is there any way I can read in a SAS version 9 dataset in R, so that I can preserve the SAS labels? If I have to change the SAS variable names to be 8 characters or less, to create a SAS version 6 transport file, I could probably do without the SAS labels as I have already read in the data into R from a CSV file. Thanks in advance for any help. Jude
___________________________________________ Jude Ryan Director, Client Analytic Services Strategy & Business Development UBS Financial Services Inc. 1200 Harbor Boulevard, 4th Floor Weehawken, NJ 07086-6791 Tel. 201-352-1935 Fax 201-272-2914 Email: jude.ryan at ubs.com
Jude, Since it appears that you have access to SAS, I would look at using the sas.get() function in the Hmisc package. While the function appears to still be broken in the Windows version, there is an easy fix that has been posted to R-help in the past. If you want to try this, you can search the R-help archives for posts on sas.get or you can email me off-list and I will send you the fix with instructions. This will avoid any v6 limitations. Hope this is helpful, Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204