read multiple dataset SAS datasets and create a data frame for each with same name

I am trying the below function code, but not sure where i am going wrong, here i am trying to read multiple SAS datasets (5 in number) and create a data frame for each with same name, when i do this I see that the datasets are read and converted to tibble but not the data frame to see in the work environment, the entire data frames gets printed in the form console as lists and tibble (screenshot : where i am showing the last dataset (5th) in the form of a tibble)

calling the vector dats with datasets names

I am using the haven package to read the sas datasets

readsas <- function(x){
  evalx <- eval(quote(x))
 evalx <- read_sas(paste0('E:\\R\\',evalx,'.sas7bdat'))
 evalx <- evalx[,]
return(evalx)
}

map(dats,readsas)

I think two things are going on - you want to save the mapped object to be something and probably use map_df instead of map if you want to stack the data rather than put it in a list.

readsas <- function(x){
  evalx <- eval(quote(x))
 evalx <- read_sas(paste0('E:\\R\\',evalx,'.sas7bdat'))
 evalx <- evalx[,]
return(evalx)
}

mystacked_data <- map_df(dats,readsas)

Instead of returning the object you could assign it to your environment.

readsas <- function(x){
    name <- eval(quote(x))
    evalx <- read_sas(paste0('E:\\R\\',name,'.sas7bdat'))
    evalx <- evalx[,]
    assign(x = name, value = evalx, envir = globalenv())
}
1 Like

@scottyd22 , thank you so much. This solution worked. I was so close but could not get the thought of using assign. but appreciate your help.

Thank you @StatSteph , i wanted to have separate data frames , instead of stacked data frame. thank you for your help. this is helpful when I want to stack the data

1 Like

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.