The Library class provides a high-level R interface to SAS libraries in SLC.
It allows you to list datasets, open existing datasets, create new datasets,
and convert between R data frames and SAS datasets.
Details
A Library object represents a SAS library (libref) and provides methods to:
List all datasets in the library
Open existing datasets for reading
Create new empty datasets
Import R data frames as SAS datasets
Export SAS datasets as R data frames
The library uses PROC IMPORT and PROC EXPORT internally for data frame conversions, supporting CSV and Parquet formats.
Methods
Method new()
Create a new Library object
Usage
Library$new(libref, session = NULL)Method create_dataset_from_dataframe()
Create a SAS dataset from an R data frame
Usage
Library$create_dataset_from_dataframe(
df,
name = deparse(substitute(df)),
format = "csv"
)Arguments
dfData frame to import into SAS
nameCharacter string specifying the dataset name. If not provided, uses the name of the data frame variable
formatCharacter string specifying the intermediate file format. Either "csv" (default) or "parquet"
Details
This method works by:
Writing the R data frame to a temporary file (CSV or Parquet)
Using PROC IMPORT to read the file into a SAS dataset
Cleaning up the temporary file
For Parquet format, the arrow package must be installed.
Examples
\dontrun{
work_lib <- slc$get_library("WORK")
# Import mtcars as CSV (default)
work_lib$create_dataset_from_dataframe(mtcars, "cars_csv")
# Import iris as Parquet
work_lib$create_dataset_from_dataframe(iris, "iris_data", format = "parquet")
# Name defaults to variable name if not specified
my_data <- data.frame(x = 1:5, y = letters[1:5])
work_lib$create_dataset_from_dataframe(my_data) # Creates dataset "my_data"
}
Method get_dataset_as_dataframe()
Export a SAS dataset as an R data frame
Details
This method works by:
Using PROC EXPORT to write the SAS dataset to a temporary CSV file
Reading the CSV file into R as a data frame
Cleaning up the temporary file
All SAS data types are converted to appropriate R types. Character variables become character vectors, numeric variables become numeric vectors, and dates/datetimes are converted to appropriate R date/time classes.
Examples
if (FALSE) { # \dontrun{
# Connect to SLC and get the WORK library
slc <- Slc$new()
work_lib <- slc$get_library("WORK")
# List all datasets in the library
datasets <- work_lib$get_dataset_names()
# Create a dataset from an R data frame
work_lib$create_dataset_from_dataframe(mtcars, "cars_data")
# Get a dataset back as an R data frame
cars_df <- work_lib$get_dataset_as_dataframe("cars_data")
# Open a dataset for reading
dataset <- work_lib$open_dataset("cars_data")
} # }
## ------------------------------------------------
## Method `Library$new`
## ------------------------------------------------
if (FALSE) { # \dontrun{
# Usually created via Slc$get_library() rather than directly
work_lib <- slc$get_library("WORK")
} # }
## ------------------------------------------------
## Method `Library$get_name`
## ------------------------------------------------
if (FALSE) { # \dontrun{
work_lib <- slc$get_library("WORK")
lib_name <- work_lib$get_name() # Returns "WORK"
} # }
## ------------------------------------------------
## Method `Library$get_dataset_names`
## ------------------------------------------------
if (FALSE) { # \dontrun{
work_lib <- slc$get_library("WORK")
datasets <- work_lib$get_dataset_names()
print(datasets) # e.g., c("DATASET1", "DATASET2", "MYTABLE")
} # }
## ------------------------------------------------
## Method `Library$open_dataset`
## ------------------------------------------------
if (FALSE) { # \dontrun{
work_lib <- slc$get_library("WORK")
dataset <- work_lib$open_dataset("MYTABLE")
# Get dataset dimensions
nrows <- dataset$get_nobs()
ncols <- dataset$get_nvars()
} # }
## ------------------------------------------------
## Method `Library$create_dataset`
## ------------------------------------------------
if (FALSE) { # \dontrun{
work_lib <- slc$get_library("WORK")
new_dataset <- work_lib$create_dataset("NEWTABLE")
} # }
## ------------------------------------------------
## Method `Library$create_dataset_from_dataframe`
## ------------------------------------------------
if (FALSE) { # \dontrun{
work_lib <- slc$get_library("WORK")
# Import mtcars as CSV (default)
work_lib$create_dataset_from_dataframe(mtcars, "cars_csv")
# Import iris as Parquet
work_lib$create_dataset_from_dataframe(iris, "iris_data", format = "parquet")
# Name defaults to variable name if not specified
my_data <- data.frame(x = 1:5, y = letters[1:5])
work_lib$create_dataset_from_dataframe(my_data) # Creates dataset "my_data"
} # }
## ------------------------------------------------
## Method `Library$get_dataset_as_dataframe`
## ------------------------------------------------
if (FALSE) { # \dontrun{
work_lib <- slc$get_library("WORK")
# First create a dataset in SAS
slc$submit("
data work.example;
do i = 1 to 10;
x = i * 2;
y = ranuni(123);
output;
end;
run;
")
# Export to R data frame
df <- work_lib$get_dataset_as_dataframe("example")
print(df)
} # }