Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read in large sas7bdat dataset in R?

Tags:

r

I have a 60gb sas7bdat file I am trying to subset in R. Does anyone know a method to do this?

I have SAS and it takes about 14 minutes to complete, but I want a way to do this in R.

thank you

like image 395
user295944 Avatar asked Oct 01 '22 10:10

user295944


2 Answers

I downloaded the wonderfull dsread http://www.oview.co.uk/dsread/ that converts Sas to csv.

You should use it from the CMD but I choose to integrate this in R. I created a function that migrate Sas to csv:

esp_Sas_Csv<-function(dir,file_input,dir_output,device="c:") {
p1<-paste("cd",dir)
if(tolower(device)!="c:") {p1<-paste(device,"\n",p1)}

#"open" the program

p2<-"SET dsread=C:\\dsread\\dsread.exe"
cmd2<-paste('"%dsread%" /v /t ',file_input,' >  "',dir_output,file_input,'.csv"',sep="")

cmd<-paste(p1,p2,cmd2, sep="\n")
setwd("c:\\temp")
write(cmd,"sas_to_csv.bat")
system("sas_to_csv.bat")

}

This code:

device<-"c:"
dir<-"C:\\temp\\"
file_input<-"my_sas_tab"
dir_output<-"C:\\temp\\"

esp_Sas_Csv(dir,file_input,dir_output,device)

generates and Runs a .bat like

cd C:\temp\
SET dsread=C:\dsread\dsread.exe
"%dsread%" /v /t my_sas_tab >  "C:\temp\my_sas_tab.csv"
like image 74
dax90 Avatar answered Oct 13 '22 12:10

dax90


See my answer here--

I found the haven package to work reasonably quickly in reading sas7bdat files.

Anyway, at 60GB, your best bet is probably to convert it to .csv or something fread can handle, then data.table::fread it into R.

like image 26
MichaelChirico Avatar answered Oct 13 '22 10:10

MichaelChirico