Skip to contents

read_readstat_metadata() reads what a file says about its data (variable labels, display formats, value labels, user-defined missing values) without reading the data itself.

Usage

read_readstat_metadata(
  path,
  format = NULL,
  catalog_file = NULL,
  encoding = NULL
)

read_dta_metadata(path, ...)

read_sas_metadata(path, ...)

read_xpt_metadata(path, ...)

read_sav_metadata(path, ...)

read_por_metadata(path, ...)

read_stata_metadata(path, ...)

read_spss_metadata(path, ...)

Arguments

path

Path to the file.

format

One of "sas", "xpt", "stata", "spss" (.sav, .zsav) or "por" (SPSS portable). NULL (default) detects it from the extension.

catalog_file

Only for SAS files. Path to a SAS format catalog (.sas7bcat) holding the value labels of a SAS data file, or NULL (default). With it, value_labels lists the labels of the columns whose display format the catalog defines.

encoding

Only for Stata files. The text encoding of a Stata file, or NULL (default), as for read_readstat().

...

Passed on to read_readstat_metadata().

Value

A readstat_metadata object: a list with

  • format: the file format, as for the format argument.

  • n_rows: the number of rows read_readstat() returns (for SAS, rows marked as deleted aren't counted), or NA if the file doesn't record it (SAS transport and SPSS portable files). For SPSS files whose header doesn't record it, the rows are counted, which reads the whole file.

  • label: the label of the whole dataset, or NA.

  • encoding: the character encoding the file's text is read in (for Stata, encoding or the format's default), or NA if unknown (SAS transport and SPSS portable files).

  • variables: a data frame with one row per variable, named after it: name, label, display format, type ("numeric" or "character"), and for SPSS measure, display_width and alignment. Values a file doesn't have are NA.

  • value_labels: a named list with an element per variable that has value labels: its labelled values, named by their labels, like the "labels" attribute of haven. Values are numbers for numeric variables and strings for character ones.

  • missing_labels: a named list with an element per Stata or SAS variable whose missing values have labels: their codes (".", and ".a" to ".z" in Stata, ".A" to ".Z" and "._" in SAS), named by their labels. haven keeps these in "labels" too, on tagged NAs.

  • na_values, na_range: named lists with an element per SPSS variable that has user-defined missing values: its discrete missing values, and its missing range as c(low, high) (-Inf and Inf for LO and HI), like the attributes of the same names in haven.

Details

read_readstat_metadata() will detect the format from the file extension, while read_dta_metadata(), read_sas_metadata(), read_xpt_metadata(), read_sav_metadata() and read_por_metadata() read the specified format.

Examples

path <- system.file("examples", "iris.sav", package = "haven")
if (nzchar(path)) {
  meta <- read_readstat_metadata(path)
  meta
  meta$variables["Species", "label"]
  meta$value_labels$Species
}
#>     setosa versicolor  virginica 
#>          1          2          3