Wednesday, October 01, 2014

sas datasets IO with R

R code lines below are run without SAS installed.
library(sas7bdat) # read sas
library(SASxport) # write sas, however, this package was removed from CRAN: Archived on 2015-06-09 as errors were not corrected despite reminders.
 
infile <- './sdtm/dm.sas7bdat'
dm1 <- read.sas7bdat(infile)
# all columns are characters now
 
label1 <- t(sapply(attributes(dm1)[['column.info']], function(x){c(x$name , x$label)}))
 
dm2 <- dm1
dm2$AGE <- as.numeric(dm2$AGE)
 
re1 <- lapply(1: nrow(label1), function(i){label(dm2[[ label1[i,1] ]] ) <<- label1[i,2] })
re2 <- lapply(1: nrow(label1), function(i){SASformat(dm2[[ label1[i,1] ]] ) <<- '$50.' })
SASformat(dm2[['DOMAIN']]) <- '$2.'
 
write.xport(dm2, file='dm1.xpt',autogen.formats=FALSE)
Created by Pretty R at inside-R.org

No comments: