Skip to content

SAS to R - if you don't have a SAS license

5 messages · Gyula Gulyas, Wensui Liu, Frank E Harrell Jr +1 more

#
Hi all,

if you do not have a SAS license but want to convert
native SAS data files, the solution below will work.

# read SAS data without SAS

# 1. Download free SAS System Viewer from either of
the sites below:
#      
http://www.sas.com/apps/demosdownloads/setupcat.jsp?cat=SAS+System+Viewer
(requires registration)
#      
http://www.umass.edu/statdata/software/downloads/SASViewer/index.html
# 2. Open SAS data in the SAS System Viewer
# 3. View-Formatted sets the data in formatted view
# 4. Save As File...csv file - this is your SAS data
file
# 5. View-Variables (now showing the variable names
and formats)
# 6. Save As File...csv file - this is your SAS
variable definition file

# run code below

wrkdir<-getwd() # save working directory to reset
later

# Select the SAS data file...
sas.data<-read.table(file.choose(),header=T, sep=",",
na.strings=".")

# Select SAS variable definition file...
sas.def<-read.csv(file.choose())

# str(sas.def)
# sas.def$SASFORMAT[sas.def$Type=="Char"]<-"character"
# sas.def$SASFORMAT[sas.def$Type=="Num"]<-"numeric"

sas.def$SASFORMAT[substr(sas.def$Format,1,4)=="DATE"]<-"date"

sas.def<-sas.def[,length(names(sas.def))] # pick last
column

tmp<-which(sas.def=="date")

sas.data[,tmp] <-
as.data.frame(strptime(sas.data[,tmp],
"%d%b%Y:%H:%M:%S"))

str(sas.data)
print(head(sas.data))

setwd(wrkdir) # reset working directory

rm(wrkdir,tmp,sas.def)

# the end




      ____________________________________________________________________________________
Be a better friend, newshound, and
#
while I move data between SAS and R all the time, personally I don't
think your recommendation is very practical. Instead, I feel SAS
transport file is much better than csv.

Plus, the sas dataset created on unix can't be opened by sas viewer on
windows. It is even undoable if the dataset is large.

Just my $0.02.
On Dec 27, 2007 6:33 PM, Gyula Gulyas <gygulyas at yahoo.ca> wrote:

  
    
#
Wensui Liu wrote:
That's surprising.  I hoped that a "SAS Viewer" would read all formats 
of SAS binary files.

There is another definite limitation to SAS Viewer: SAS invested so 
little of their billions of $ into it that it only has 2 delimiters (tab 
and comma) and doesn't even check if character strings contain the 
delimiter so as to escape those occurrences.  So the Viewer often 
produces invalid csv files.

Frank
#
On Fri, 2007-12-28 at 11:21 -0600, Frank E Harrell Jr wrote:
<snip>

We have had that problem here, having received both 32 and 64 bit SAS
files from Linux and Solaris based SAS installations. The SAS System
Viewer was largely useless for the non-Windows SAS files that we have
had accessible to us, even though it will run quite nicely under Wine.

We have a 32 bit RHEL based SAS install here now, as I have noted in
prior posts. That has helped with certain aspects of SAS dataset
generation and transfer to and from clients.

DBMS/Copy, which also runs nicely under Wine, seems to be the only tool
that we have so far, that can provide for something of a universal SAS
"can opener". It will at least successfully _open_ SAS datasets
generated on a variety of platforms, when other approaches have failed.

However, as I have also noted in prior posts, the SAS datasets that
DBMS/Copy generates, are not guaranteed to be able to be opened by SAS
itself. Hence our need to install SAS here.

Theoretically, the .sas7bdat files are supposed to be cross-platform
compatible and I think Peter had commented on that in a prior thread on
this subject. However, hands on experience has suggested that this
expectation is not absolute.

HTH,

Marc Schwartz
#
Marc Schwartz wrote:
I have had good success with Stat/Transfer (and its Windows version runs 
perfectly under Linux using wine) but haven't tested it as extensively 
as you've tested DBMS/Copy.

Frank