Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajdamico/669666 to your computer and use it in GitHub Desktop.
Save ajdamico/669666 to your computer and use it in GitHub Desktop.
download all publicly-available data files for every year of the Medical Expenditure Panel Survey
library(RCurl)
setwd("R:\\Medical Expenditure Panel Survey\\Data")
#input all available MEPS public use file numbers
year <- c(1996:2008)
consolidated <- c(12,20,28,38,50,60,70,79,89,97,105,113,NA)
conditions <- c("06r",18,27,37,52,61,69,78,87,96,104,112,NA)
jobs <- c("07",19,25,32,40,56,63,74,83,91,100,108,116)
prpf <- c(24,47,47,47,47,57,66,76,88,95,103,111,119)
longitudinal <- c(23,35,48,58,65,71,80,86,98,106,114,NA,NA)
events <- c(10,16,26,33,51,59,67,77,85,94,102,110,118)
mepsmatrix <- data.frame(year, consolidated, conditions, jobs, prpf, longitudinal, events)
#assign all events files
mepsmatrix$rx <- paste(mepsmatrix$events,"a",sep="")
mepsmatrix$dental <- paste(mepsmatrix$events,"b",sep="")
mepsmatrix$other <- paste(mepsmatrix$events,"c",sep="")
mepsmatrix$inpatient <- paste(mepsmatrix$events,"d",sep="")
mepsmatrix$er <- paste(mepsmatrix$events,"e",sep="")
mepsmatrix$outpatient <- paste(mepsmatrix$events,"f",sep="")
mepsmatrix$office <- paste(mepsmatrix$events,"g",sep="")
mepsmatrix$hh <- paste(mepsmatrix$events,"h",sep="")
mepsmatrix$events <- NULL
##download special files
download.file( "http://www.meps.ahrq.gov/mepsweb/data_files/pufs/h36u07ssp.zip" , "temp" )
zipcontents <- unzip( "temp" , list=T)
unzip( "temp" )
file.rename( as.character(zipcontents[1,1]) , "linkage.ssp" )
file.remove("temp")
download.file( "http://www.meps.ahrq.gov/mepsweb/data_stats/download_data/pufs/h36/h36u07cb.pdf" , "linkage - cb.pdf" , mode="wb" , cacheOK=F , method="internal" )
download.file( "http://www.meps.ahrq.gov/mepsweb/data_stats/download_data/pufs/h36/h36u07doc.pdf" , "linkage - doc.pdf" , mode="wb" , cacheOK=F , method="internal" )
download.file( "http://www.meps.ahrq.gov/mepsweb/data_files/pufs/h36b07ssp.zip" , "temp" )
zipcontents <- unzip( "temp" , list=T)
unzip( "temp" )
file.rename( as.character(zipcontents[1,1]) , "linkage - brr.ssp" )
file.remove("temp")
download.file( "http://www.meps.ahrq.gov/mepsweb/data_stats/download_data/pufs/h36brr/h36b07cb.pdf" , "linkage - brr cb.pdf" , mode="wb" , cacheOK=F , method="internal" )
download.file( "http://www.meps.ahrq.gov/mepsweb/data_stats/download_data/pufs/h36brr/h36b07doc.pdf" , "linkage - brr doc.pdf" , mode="wb" , cacheOK=F , method="internal" )
#begin downloading all files for all years
for (i in 1:nrow(mepsmatrix) ) {
for (j in 2:ncol(mepsmatrix) ) {
if (!is.na(mepsmatrix[i,j])){
#download the SSP file
filenumbername <- paste("h",mepsmatrix[i,j],"ssp.zip",sep="")
meps_zipped_file <- paste("http://www.meps.ahrq.gov/mepsweb/data_files/pufs/h" , mepsmatrix[i,j] , "ssp.zip" , sep="")
err <- try( getURLContent(meps_zipped_file) , silent=T )
if( class(err) == "try-error" ){
download.file( sub("ssp.zip","f1ssp.zip",meps_zipped_file) , sub("ssp.zip","f1ssp.zip",filenumbername) )
zipcontents <- unzip( sub("ssp.zip","f1ssp.zip",filenumbername) , list=T)
unzip( sub("ssp.zip","f1ssp.zip",filenumbername) )
finalname <- paste( mepsmatrix[i,1] , " - " , names(mepsmatrix)[j] , " f1.ssp" , sep="" )
file.rename( as.character(zipcontents[1,1]) , finalname )
file.remove(sub("ssp.zip","f1ssp.zip",filenumbername))
download.file( sub("ssp.zip","f1ssp.zip",meps_zipped_file) , sub("ssp.zip","f2ssp.zip",filenumbername) )
zipcontents <- unzip( sub("ssp.zip","f2ssp.zip",filenumbername) , list=T)
unzip( sub("ssp.zip","f2ssp.zip",filenumbername) )
finalname <- paste( mepsmatrix[i,1] , " - " , names(mepsmatrix)[j] , " f2.ssp" , sep="" )
file.rename( as.character(zipcontents[1,1]) , finalname )
file.remove(sub("ssp.zip","f2ssp.zip",filenumbername))
} else {
download.file( meps_zipped_file , filenumbername )
zipcontents <- unzip( filenumbername , list=T)
unzip( filenumbername )
finalname <- paste( mepsmatrix[i,1] , " - " , names(mepsmatrix)[j] , ".ssp" , sep="" )
file.rename( as.character(zipcontents[1,1]) , finalname )
file.remove(filenumbername)
}
err <- NULL
#download the codebook if possible
cbname <- paste( mepsmatrix[i,1] , " - " , names(mepsmatrix)[j] , " cb.pdf" , sep="")
cbsite <- paste( "http://www.meps.ahrq.gov/mepsweb/data_stats/download_data/pufs/h",mepsmatrix[i,j],"/h",mepsmatrix[i,j],"cb.pdf",sep="")
err <- try( getURLContent( cbsite ) )
if (! class(err) == "try-error" ) download.file( cbsite , cbname , mode="wb" , cacheOK=F , method="internal" )
err <- NULL
#download the documentation if possible
docname <- paste( mepsmatrix[i,1] , " - " , names(mepsmatrix)[j] , " doc.pdf" , sep="")
docsite <- paste( "http://www.meps.ahrq.gov/mepsweb/data_stats/download_data/pufs/h",mepsmatrix[i,j],"/h",mepsmatrix[i,j],"doc.pdf",sep="")
err <- try( getURLContent( docsite ) )
if (! class(err) == "try-error" ) download.file( docsite , docname , mode="wb" , cacheOK=F , method="internal" )
err <- NULL
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment