I am trying to create a function in R that imports data. I would like to save each dataset with a different object name and save it to the global environment. The four arguments I am passing to the function are 1.) Month, 2.) ID, 3.) root , and 4.) file. Both root and file specify the filepath. The code is below:
# Import Data
ImportData <- function(Month, ID, root, file){
Month_Data_ID_1 <<- read_sas(paste(root,file, sep = ""))
return(Month_Data_ID_1)
}
# Function
ImportData(Jan, A7 , Base,"select_jan_on.sas7bdat")
ImportData(Feb, A8 , Base,"select_feb_on.sas7bdat")
If I run this function multiple times, the object name does not change to the new arguments that are passed through. Instead a global dataset called 'Month_Data_ID_1' is saved. How can I fix this. I also read that this is bad practice? Is there a better way to import and dynamically rename objects?
Thanks!