We’re pleased to announce the release of haven 2.6.0. haven lets you read and write SAS, SPSS, and Stata files from R. You can install it from CRAN with:

install.packages("haven")

The big news in this release is that write_sas() is back: haven now writes .sas7bdat files that SAS can actually open. This release also introduces a new way to work with file metadata, makes reading a bit more pleasant, and includes a large number of smaller fixes. You can see a full list of changes in the release notes.

library(haven)

write_sas() is back#

write_sas() was deprecated in haven 2.5.2 because the files it produced couldn’t reliably be read by SAS. The cause turned out to be an incorrectly computed header checksum in ReadStat, the C library that powers haven. That’s now fixed, so SAS can read the files haven writes, and write_sas() is no longer deprecated.

Along the way we fixed a number of other problems with the SAS writer:

  • Tagged missing values (e.g. tagged_na("a")) are now written in the format SAS expects, so they come back as .A in SAS.
  • Variable names starting with an underscore are written correctly.
  • Dates and date-times on or after 4000-03-01 are no longer off by one day. (SAS doesn’t treat years divisible by 4000 as leap years, and now neither does haven when reading SAS files.)

So if you need to hand a dataset to a SAS user, you can now simply:

df <- data.frame(
  id = 1:3,
  visit = as.Date(c("2026-01-15", "2026-02-12", "2026-03-19")),
  score = c(12.5, NA, 9.25)
)
path <- tempfile(fileext = ".sas7bdat")
write_sas(df, path)

Richer metadata with meta()#

SAS, SPSS, and Stata files carry a lot of metadata beyond the data itself: display formats, column widths, measurement levels, file timestamps, and more. Previously haven stored a handful of these as ad hoc attributes like format.sas, width, and display_width, ignoring the rest. haven 2.6.0 replaces these slapdash attributes with a single meta() accessor backed by S7 classes, one per format, for both data frames and variables.

iris_sas_path <- system.file("examples", "iris.sas7bdat", package = "haven")
iris_sas <- read_sas(iris_sas_path)
meta(iris_sas)
<haven::meta_df_sas>
@created   : 2016-06-08 22:38:50 UTC
@modified  : 2016-06-08 22:38:50 UTC
@table_name: "IRIS"
meta(iris_sas$Species)
<haven::meta_var_sas>
@format  : "$6"
@informat: "$6"
@width   : 6

Metadata that haven reads is used again when writing, so it survives a round trip. This release also captures metadata that haven previously ignored:

  • SAS: variable informats, table names, and the transport file version.
  • SPSS: measurement levels, alignment, and (read-only) multiple response sets.
  • Stata: storage types and the file version.

You can create metadata from scratch with the format-specific constructors like meta_var_sas(). For example, here we give our data a SAS date format and a numeric format with two decimal places, then write it out:

meta(df$visit) <- meta_var_sas(format = "DATE9.")
meta(df$score) <- meta_var_sas(format = "8.2")
write_sas(df, path)

SAS users opening this file will now see 15JAN2026 for visit and two decimal places of precision for score.

The same approach works for SPSS, where you might want to change a variable’s measure type:

iris_sav_path <- system.file("examples", "iris.sav", package = "haven")
iris_sav <- read_sav(iris_sav_path)
meta(iris_sav$Species)@measure <- "ordinal"
meta(iris_sav$Species)
<haven::meta_var_spss>
@format       : "F8.0"
@display_width: 8
@measure      : "ordinal"
@alignment    : "right"

If you want to strip metadata, use the new zap_meta(), which supersedes zap_formats() and zap_widths().

If you only need the metadata from a file (or just the column names and types), use n_max = 0. haven now stops parsing right after the file header, so this is fast even for very large files:

read_sav(iris_sav_path, n_max = 0)
# A tibble: 0 × 5
# ℹ 5 variables: Sepal.Length <dbl>, Sepal.Width <dbl>, Petal.Length <dbl>,
#   Petal.Width <dbl>, Species <dbl+lbl>

Reading improvements#

All read_*() functions gain a progress argument. As in readr, a progress bar is shown by default in interactive sessions when reading large files; turn it off with options(haven.show_progress = FALSE).

col_select now fully implements the tidyselect interface, so columns are returned in the order you request them and you can rename them as you go:

read_sas(
  iris_sas_path,
  col_select = c(species = Species, starts_with("Petal"))
)
# A tibble: 150 × 3
   species Petal_Length Petal_Width
   <chr>          <dbl>       <dbl>
 1 setosa           1.4         0.2
 2 setosa           1.4         0.2
 3 setosa           1.3         0.2
 4 setosa           1.5         0.2
 5 setosa           1.4         0.2
 6 setosa           1.7         0.4
 7 setosa           1.4         0.3
 8 setosa           1.5         0.2
 9 setosa           1.4         0.2
10 setosa           1.5         0.1
# ℹ 140 more rows

Other improvements#

A few other changes worth highlighting:

  • read_sas() can now read files produced by JMP and files with zero rows, and supports all SAS date, time, and datetime formats. It also uses the data file’s encoding for catalog files that don’t specify their own, fixing garbled value labels in UTF-8 catalogs.
  • write_dta() now stores each numeric column using the smallest Stata storage type that exactly represents its values, like Stata’s compress, which can substantially reduce file sizes. It also gives an informative error for value labels outside the range Stata can store, rather than silently converting them to missing values.
  • write_xpt() supports long variable labels in version 8 files, and errors on variable names that are too long instead of silently truncating them.
  • write_sav() supports value labels for strings longer than 8 bytes.
  • labelled_spss() vectors containing NA can now be combined with rbind() and dplyr::bind_rows().

Breaking changes#

This release includes a few breaking changes:

  • read_*() no longer read compressed (.gz, .bz2, .xz, .zip) files. Decompress them first.
  • read_*() no longer attach the format.*, width, display_width, or notes attributes. Use meta() instead.
  • read_sas() no longer accepts cols_only, which has been deprecated since haven 2.2.0. Use col_select instead.

haven also no longer depends on crayon or readr, giving it a lighter dependency footprint.

Acknowledgements#

A big thanks to everyone who contributed to this release by filing issues, submitting pull requests, and discussing ideas: @ankonyeni, @bms63, @cpiraux, @d2yh, @gorcha, @gowerc, @hadley, @jeroen, @jeroenjanssens, @jplavulca, @LeonidasZhak, @MarcusPenthin, @nathanj3, @nickharrigan, @rolsen-dft, @sasutils, @sims1253, @szimmer, @taekop, and @vthanik.