Skip to content

Instantly share code, notes, and snippets.

@aaronpeikert
Last active September 3, 2019 09:30
Show Gist options
  • Save aaronpeikert/0d109d44b53cb919446c00fd829cc90d to your computer and use it in GitHub Desktop.
Save aaronpeikert/0d109d44b53cb919446c00fd829cc90d to your computer and use it in GitHub Desktop.
How to load a weird sas file that was found in the wild, but refused to be read be haven::read_sas
library(tidyverse)
sas <- tempfile()
download.file("http://psych.colorado.edu/~carey/Courses/PSYC7291/DataSets/Documentation/InterestDataDoc.txt",
sas)
sas <- read_lines(sas)
starts_with_spaces <- function(string){
out <- seq_along(string)
for (i in out) {
if(grepl("^ ", string[i]))out[i] <- out[i-1]
}
return(as.factor(out))
}
get_datalines <- function(string){
string <- string[(grep("DATALINES;", string)+1):length(string)]
string <- string[-(which(map_lgl(string, identical, "")):length(string))]
return(string)
}
get_input <- function(string) {
found <- FALSE
string <- str_squish(string)
for (i in seq_along(string)) {
if(grepl("^input ", string[i])){
found <- TRUE
start <- i
end <- start
} else if(found){
if(!grepl(";", string[i])){
end <- end + 1
} else {
end <- end + 1
break
}
}
}
input <- string[start:end]
input <- str_remove(input, "input ")
input <- str_remove(input, ";")
input <- str_split(input, " ")
input <- flatten_chr(input)
return(input)
}
sas_data <- sas %>% get_datalines() %>% split(., starts_with_spaces(.)) %>%
map(paste, collapse = " ") %>%
str_squish() %>%
str_split(" ") %>%
do.call(rbind, .)
colnames(sas_data) <- get_input(sas)
sas_data <- sas_data %>% as_tibble() %>% map_dfc(parse_number)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment