Skip to contents

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)

Arguments

libref

A WpsLibref object representing the SAS library

session

A WpsSession object (optional, but required for data frame operations)

Returns

A new Library object

Examples

\dontrun{
# Usually created via Slc$get_library() rather than directly
work_lib <- slc$get_library("WORK")
}


Method get_name()

Get the name of the library

Usage

Library$get_name()

Returns

Character string containing the library name (libref)

Examples

\dontrun{
work_lib <- slc$get_library("WORK")
lib_name <- work_lib$get_name()  # Returns "WORK"
}


Method get_dataset_names()

Get names of all datasets in the library

Usage

Library$get_dataset_names()

Returns

Character vector of dataset names

Examples

\dontrun{
work_lib <- slc$get_library("WORK")
datasets <- work_lib$get_dataset_names()
print(datasets)  # e.g., c("DATASET1", "DATASET2", "MYTABLE")
}


Method open_dataset()

Open an existing dataset for reading

Usage

Library$open_dataset(name)

Arguments

name

Character string specifying the dataset name

Returns

A Dataset object that can be used to read the dataset

Examples

\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 create_dataset()

Create a new empty dataset

Usage

Library$create_dataset(name)

Arguments

name

Character string specifying the name for the new dataset

Returns

A Dataset object representing the new empty dataset

Examples

\dontrun{
work_lib <- slc$get_library("WORK")
new_dataset <- work_lib$create_dataset("NEWTABLE")
}


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

df

Data frame to import into SAS

name

Character string specifying the dataset name. If not provided, uses the name of the data frame variable

format

Character string specifying the intermediate file format. Either "csv" (default) or "parquet"

Details

This method works by:

  1. Writing the R data frame to a temporary file (CSV or Parquet)

  2. Using PROC IMPORT to read the file into a SAS dataset

  3. Cleaning up the temporary file

For Parquet format, the arrow package must be installed.

Returns

Invisible self (for method chaining)

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

Usage

Library$get_dataset_as_dataframe(name)

Arguments

name

Character string specifying the dataset name to export

Details

This method works by:

  1. Using PROC EXPORT to write the SAS dataset to a temporary CSV file

  2. Reading the CSV file into R as a data frame

  3. 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.

Returns

Data frame containing the dataset contents

Examples

\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)
}


Method clone()

The objects of this class are cloneable with this method.

Usage

Library$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

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)
} # }