Created
May 4, 2010 22:11
-
-
Save ajdamico/390098 to your computer and use it in GitHub Desktop.
import SAS ASCII file into R with SAS INPUT text file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##select file containing SAS input procedure | |
SASinput <- readLines(file.choose()) | |
##find the first INPUT line | |
firstline<-min(grep("INPUT @1",toupper(SASinput))) | |
##find the first semicolon ending that input line | |
a<-grep(";",toupper(SASinput)) | |
lastline<-min(a[a>firstline]) | |
##isolate the Fixed-Width File input lines | |
FWFlines<-SASinput[firstline:lastline] | |
##break apart all FWF lines | |
z<-strsplit(FWFlines," ",perl=T) | |
lengths<-grep | |
##create FWF structure file (x) | |
x<-NULL | |
for (i in 1:length(z)){ | |
y<-z[[i]] | |
y<-y[! y %in% c("","INPUT",";")] | |
y<-t(as.data.frame(y)) | |
colnames(y)<-c("placement","varname","format") | |
x<-rbind(x,y) | |
} | |
x<-data.frame(x) | |
x$placement<-as.numeric(sub("\\@","",x$placement)) | |
##determine exact width of each variable | |
x$width<-as.numeric(substr(x$format , ifelse(grepl("\\$",x$format),2,1) , regexpr("\\.",x$format)-1)) | |
##determine number of decimals each variable should have | |
x$decimals<-as.numeric(substr(x$format , regexpr("\\.",x$format)+1,length(x$format))) | |
##determine whether character or string | |
x$charvar<-grepl("\\$",x$format) | |
##input actual SAS data text-delimited file to read in | |
SASfile <- read.fwf(file.choose() , x$width , col.names=x$varname) | |
##convert character strings appropriately within data frame | |
for (i in 1:length(x)){ | |
if (x[i,"charvar"]) SASfile[,i]<-as.character(SASfile[,i]) | |
else SASfile[,i]<-as.numeric(SASfile[,i]) | |
} | |
##display final R data frame | |
head(SASfile) | |
tail(SASfile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment