Reads a SAS sas7bdat file using haven::read_sas and then applies the labels given in the formats file (containing PROC FORMAT statements). The labels use labelled::labelled and may be coerced to factors if desired.

read_sas_with_format(
  file_bdat,
  file_format,
  as_factor = FALSE,
  missing_values = paste0(".", c("", 1:9, LETTERS)),
  debug_level = 0,
  ...
)

Arguments

file_bdat

Path to the sas7bdat file to be read by haven::read_sas

file_format

Path to the SAS formats file to be read by read_proc_format

as_factor

Convert labelled variables to factor with haven::as_factor?

missing_values

Possible SAS values that should be treated as missing.

debug_level

Default is 0. Level 1 prints confirmation of reading proc_format file. Level 2 prints confirmation that the labels were applied to each variable in df.

...

Arguments passed on to haven::read_sas

data_file

Path to data and catalog files. The files are processed with readr::datasource().

catalog_file

Path to data and catalog files. The files are processed with readr::datasource().

encoding

The character encoding used for the data_file and catalog_encoding respectively. A value of NULL uses the encoding specified in the file; use this argument to override it if it is incorrect.

catalog_encoding

The character encoding used for the data_file and catalog_encoding respectively. A value of NULL uses the encoding specified in the file; use this argument to override it if it is incorrect.

col_select

One or more selection expressions, like in dplyr::select(). Use c() or list() to use more than one expression. See ?dplyr::select for details on available selection options. Only the specified columns will be read from data_file.

skip

Number of lines to skip before reading data.

n_max

Maximum number of lines to read.

cols_only

Deprecated: Use col_select instead.

.name_repair

Treatment of problematic column names:

  • "minimal": No name repair or checks, beyond basic existence,

  • "unique": Make sure names are unique and not empty,

  • "check_unique": (default value), no name repair, but check they are unique,

  • "universal": Make the names unique and syntactic

  • a function: apply custom name repair (e.g., .name_repair = make.names for names in the style of base R).

  • A purrr-style anonymous function, see rlang::as_function()

See name-repair for more details on these terms and the strategies used to enforce them.

See also

Other SAS helper functions: add_proc_format_labels(), read_proc_format()

Examples

if (FALSE) { read_sas_with_format("freepsa_data_feb16_d080516.sas7bdat", "freepsa.sas_formats.feb16.d080516.sas") }