Skip to content

Instantly share code, notes, and snippets.

@markdanese
Last active January 14, 2021 04:32
Show Gist options
  • Save markdanese/19f7394d553a7ac98da2 to your computer and use it in GitHub Desktop.
Save markdanese/19f7394d553a7ac98da2 to your computer and use it in GitHub Desktop.
An R script to download the Medicare SynPUF (synthetic public use files)
# main web page: "https://www.cms.gov/Research-Statistics-Data-and-Systems/Downloadable-Public-Use-Files/SynPUFs/DESample01.html"
# list of files to be downloaded for each 1/20 of the data
dl_list <-
c(
"https://www.cms.gov/Research-Statistics-Data-and-Systems/Downloadable-Public-Use-Files/SynPUFs/Downloads/DE1_0_2008_Beneficiary_Summary_File_Samplezzz.zip",
"http://downloads.cms.gov/files/DE1_0_2008_to_2010_Carrier_Claims_SamplezzzA.zip",
"http://downloads.cms.gov/files/DE1_0_2008_to_2010_Carrier_Claims_SamplezzzB.zip",
"https://www.cms.gov/Research-Statistics-Data-and-Systems/Downloadable-Public-Use-Files/SynPUFs/Downloads/DE1_0_2008_to_2010_Inpatient_Claims_Samplezzz.zip",
"https://www.cms.gov/Research-Statistics-Data-and-Systems/Downloadable-Public-Use-Files/SynPUFs/Downloads/DE1_0_2008_to_2010_Outpatient_Claims_Samplezzz.zip",
"http://downloads.cms.gov/files/DE1_0_2008_to_2010_Prescription_Drug_Events_Samplezzz.zip",
"https://www.cms.gov/Research-Statistics-Data-and-Systems/Downloadable-Public-Use-Files/SynPUFs/Downloads/DE1_0_2009_Beneficiary_Summary_File_Samplezzz.zip",
"https://www.cms.gov/Research-Statistics-Data-and-Systems/Downloadable-Public-Use-Files/SynPUFs/Downloads/DE1_0_2010_Beneficiary_Summary_File_Samplezzz.zip"
)
local_path <- "your/download/path/here" # specifies where the downloaded file should be saved
# loop to download files ------------------------------------------------------------
for(i in 1:20) {
r <- paste0("_", i)
dl <- gsub("zzz", r, dl_list)
for(j in seq_along(dl)){
download_path <- paste0(local_path, basename(dl[[j]]))
download.file(dl[[j]], download_path, method = "libcurl") # you might need to change method, especially on windows
}
}
@markdanese
Copy link
Author

This took 50 minutes to download on my macbook over a FIOS (fiber optic) connection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment