Read ENTIRE sas table to R by using read_sas("C:/sasdata.sas7bdat",NULL) in haven package.
However, I only need few columns instead of 100 of them, and sometimes it causes “C stack usage is too close to the limit” error. Is there way to select columns when reading in R? Thanks,
You can include the col_select()
argument, as in
read_sas("C:/sasdata.sas7bdat", col_select = c(1,7, 39))
Thank you so much. I used the code above and doesn't work. The error message is "unused argument (col_select = c(1, 7, 39))" . Any suggestion?
Perhaps you have an older version of the haven
package.
library(haven)
path <- system.file("examples", "iris.sas7bdat", package = "haven")
dat_all <- read_sas(path)
dat_sel <- read_sas(path,
col_select=c(1, 2, 5))
names(dat_all)
#> [1] "Sepal_Length" "Sepal_Width" "Petal_Length" "Petal_Width" "Species"
names(dat_sel)
#> [1] "Sepal_Length" "Sepal_Width" "Species"
Created on 2021-09-27 by the reprex package (v2.0.1)
Thank you very much. It works outside of function, but not work in function. I have 10 years of data and need read all of them. The function passes input table full name and select columns and return extracted table. Something like
readit <- function(fullname){
file <- glue(fullname)
print (file) # file contains correct sas table name
indata <- read_sas(file, col_select=c(1,2,3))
outdata <- indata %>% rename('Description'=desc)
return(outdata)
}
New1 <- readit("c:/temp/iris1.sas7bdat ")
New2 <- readit("c:/temp/iris2.sas7bdat ")
Again the error message is “Error in read_sas(file, col_select = c(1, 2, 3)) : unused argument (col_select = c(1, 2, 3))”. It seems like read_sas not recognizing the passed parameter. Any solution to this?
I can't reproduce this error. Try to share something with a reproducible example
library(haven)
library(tidyverse)
library(glue)
#>
#> Attaching package: 'glue'
#> The following object is masked from 'package:dplyr':
#>
#> collapse
path <- system.file("examples", "iris.sas7bdat", package = "haven")
readit <- function(fullname){
file <- glue(fullname)
print (file) # file contains correct sas table name
indata <- read_sas(file, col_select=c(1,2,3))
outdata <- indata
return(outdata)
}
readit_mod <- function(fullname){
file <- glue(fullname) %>% as.character()
print (file) # file contains correct sas table name
indata <- read_sas(file, col_select=c(1,2,3))
outdata <- indata
return(outdata)
}
readit(path)
#> C:/../Documents/R/win-library/4.1/haven/examples/iris.sas7bdat
#> # A tibble: 150 x 3
#> Sepal_Length Sepal_Width Petal_Length
#> <dbl> <dbl> <dbl>
#> 1 5.1 3.5 1.4
#> 2 4.9 3 1.4
#> 3 4.7 3.2 1.3
#> 4 4.6 3.1 1.5
#> 5 5 3.6 1.4
#> 6 5.4 3.9 1.7
#> 7 4.6 3.4 1.4
#> 8 5 3.4 1.5
#> 9 4.4 2.9 1.4
#> 10 4.9 3.1 1.5
#> # ... with 140 more rows
readit_mod(path)
#> [1] "C:/../Documents/R/win-library/4.1/haven/examples/iris.sas7bdat"
#> # A tibble: 150 x 3
#> Sepal_Length Sepal_Width Petal_Length
#> <dbl> <dbl> <dbl>
#> 1 5.1 3.5 1.4
#> 2 4.9 3 1.4
#> 3 4.7 3.2 1.3
#> 4 4.6 3.1 1.5
#> 5 5 3.6 1.4
#> 6 5.4 3.9 1.7
#> 7 4.6 3.4 1.4
#> 8 5 3.4 1.5
#> 9 4.4 2.9 1.4
#> 10 4.9 3.1 1.5
#> # ... with 140 more rows
Created on 2021-09-27 by the reprex package (v2.0.1)
Thank you all. It works fine after I adding library(tidyverse).
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.