[R] reading version 9 SAS datasets in R

Frank E Harrell Jr f.harrell at vanderbilt.edu
Thu Dec 4 00:39:26 CET 2008


Frank E Harrell Jr wrote:
> jude.ryan at ubs.com wrote:
>> 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';
> 
> Try
> 
> FILENAME transp SASV5XPT 'D:\CES Analysis\Data\fadata.xpt';

Sorry I screwed that up.  See 
http://biostat.mc.vanderbilt.edu/SASexportHowto for the correct 
approach, under the heading "If You Have Access to SAS".

Frank

> 
> Frank
> 
>>
>>  
>>
>> /* 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
>>
>>  
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> Please do not transmit orders or instructions regarding a UBS account 
>> by e-mail. The information provided in this e-mail or any attachments 
>> is not an official transaction confirmation or account statement. For 
>> your protection, do not include account numbers, Social Security 
>> numbers, credit card numbers, passwords or other non-public 
>> information in your e-mail. Because the information contained in this 
>> message may be privileged, confidential, proprietary or otherwise 
>> protected from disclosure, please notify us immediately by replying to 
>> this message and deleting it from your computer if you have received 
>> this communication in error. Thank you.
>>
>>
>>
>> UBS Financial Services Inc.
>>
>> UBS International Inc.
>>
>> UBS Financial Services Incorporated of Puerto Rico
>>
>> UBS AG
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide 
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
> 
> 


-- 
Frank E Harrell Jr   Professor and Chair           School of Medicine
                      Department of Biostatistics   Vanderbilt University



More information about the R-help mailing list