0

I am trying to read in a data set from SAS using the unz() function in R. I do not want to unzip the file. I have successfully used the following to read one of them in:

dir <- "C:/Users/michael/data/"
setwd(dir)
dir_files <- as.character(unzip("example_data.zip", list = TRUE)$Name)
ds <- read_sas(unz("example_data.zip", dir_files))

That works great. I'm able to read the data set in and conduct the analysis. When I try to read in another data set, though, I encounter an error:

dir2_files <- as.character(unzip("data.zip", list = TRUE)$Name)
ds2 <- read_sas(unz("data.zip", dir2_files))
Error in read_connection_(con, tempfile()) : 
  Evaluation error: error reading from the connection.

I have read other questions on here saying that the file path may be incorrectly specified. Some answers mentioned submitting list.files() to the console to see what is listed.

list.files()
[1] "example_data.zip"              "data.zip"

As you can see, I can see the folders, and I was successfully able to read the data set in from "example_data.zip", but I cannot access the data.zip folder.

What am I missing? Thanks in advance.

MrFlick
  • 163,738
  • 12
  • 226
  • 242
  • 1
    Pretty sure that `haven` will uncompress files automatically. Have you tried supplying the path for the zip file? (assuming it is not a zip full of multiple files). It could also be the case that `data.zip` contains multiple files. In which case you would want to read them in using `lapply()` or an analog. – Andrew Aug 18 '20 at 20:04
  • Thanks, @Andrew--didn't know that about `haven`. The folders in the directory only contain one file per folder. I tried supplying the full path for the zip folder, but it still gave me the same error. I've tried other packages, like `read.sas7bdat`, but I'm running into the same issue. I just don't understand why it works for one file and not the other. I'm going to keep looking for solutions. – mossguire Aug 19 '20 at 14:01
  • Hmm, if you got the same error it may be that you are still reading in the connection. Maybe try just doing `read_sas("C:/Users/michael/data/example_data.zip")` and `read_sas("C:/Users/michael/data/data.zip")` (tidyverse reading functions also accept connections). You can also unzip the file from R if that does not work--then read in the dataset. – Andrew Aug 21 '20 at 10:33

0 Answers0