[R] SAS to R - if you have SAS 8.2+

Gyula Gulyas gygulyas at yahoo.ca
Fri Dec 28 00:25:09 CET 2007


Hi there,

the attached R function uses the SAS Integrated Object
Model (IOM) and it can deal with SAS dates and long
variable names. All you need to provide is the folder
where the SAS data file is and the data file name
without the extension. The function requires the rcom
package.

This is meant to be first cut...but improvements and
suggestions are more than welcome!

Gyula

 import.sas.data <-
function(inPath,inSAS,as.is=FALSE){
 # Function to read in a SAS data file
 # Packages needed: rcom
 # inPath: path to SAS file
 # inSAS: SAS data file name (no extension)
 # as.is: as per read.csv (FALSE - force all character
variables to be factors)
 # outputs a data.frame object
 # Created:  December 1, 2007
 # Modified: December 4, 2007
 # Author: Gyula Gulyas (gyula.gulyas at timberline.ca)
 # Use as is, no guarantees, liability due to data
loss is limited to the price you paid for this
function...
 
 require(rcom)

 # check if user has closing slash in path and fix it
if needed
 inPath <- sub("/$","",inPath)

 obWSM <-
comCreateObject("SASWorkspaceManager.WorkspaceManager")
 obWSM.Workspaces <- obWSM[["Workspaces"]]
 obSAS <- comCreateObject("SAS.Workspace")
 obSAS.DataService <- obSAS[["DataService"]]
 obSAS.LanguageService <- obSAS[["LanguageService"]]

 # hard-coded temporary files
 # sas temporary csv file
 csvdata <- paste(inPath,"/t__sd__t.csv", sep="")
 # sas temporary column definition file
 coldef <- paste(inPath,"/t__sc__t.csv", sep="")
 
 libRef <-
obSAS.DataService$AssignLibref("sasds","",inPath,"")

 # create the content csv file
 cont1 <- paste("proc contents data=sasds.",inSAS,"
out=_tmp1(KEEP=NAME TYPE LENGTH VARNUM FORMAT)
noprint; run;",sep="")
 cont2 <- "proc sort data=_tmp1; by varnum; run;"
 cont3 <- "data _tmp2; set _tmp1; if type=2 then
dummy='character'; "
 cont3 <- paste(cont3,"if type=1 then do; if format in
('DATE','DATETIME') then dummy='date'; ",sep="")
 cont3 <- paste(cont3,"else dummy='numeric'; end;
run;",sep="")
 cont4 <- "proc transpose data=_tmp2
out=_tmp3(DROP=_NAME_); id name; var dummy; run;"
 cont5 <- paste("proc export data=_tmp3
outfile='",coldef,"' dbms=csv; run;",sep="")

 obSAS.LanguageService$Submit(cont1)
 obSAS.LanguageService$Submit(cont2)
 obSAS.LanguageService$Submit(cont3)
 obSAS.LanguageService$Submit(cont4)
 obSAS.LanguageService$Submit(cont5)

 # column definitions, all as characters
 scf <- read.csv(coldef,as.is=T)

 sasline1 <- paste("data _tmp4; set sasds.", inSAS, ";
run;",sep="")

 obSAS.LanguageService$Submit(sasline1)

 sasline2 <- paste("proc export data=_tmp4
outfile='",csvdata,"' dbms=csv; run;",sep="")

 obSAS.LanguageService$Submit(sasline2)

 sdf <- read.csv(csvdata,as.is=as.is)

 # delete old csvdata file
 file.remove(csvdata)
 # delete old coldef file
 file.remove(coldef)
 
 # convert SAS DATETIME format into native R date
(POSIXct)
 x <- which(scf=="date")
 sdf[,x] <- as.data.frame(strptime(sdf[,x],
"%d%b%Y:%H:%M:%S"))

 # cleanup SAS objects
 obSAS.UniqueIdentifier <- obSAS[["UniqueIdentifier"]]

obWSM.Workspaces$RemoveWorkspaceByUUID(obSAS.UniqueIdentifier)
 obSAS$Close()

 return(sdf)

}

# example call
# not run
# test<- import.sas.data("path/to/data","sasdatafile")




      ____________________________________________________________________________________
Looking for last minute shopping deals?



More information about the R-help mailing list