Search code examples
rspssr-haven

Export variable label for SPSS with haven


I would like to export a data set I work on in R for my colleagues to use in SPSS. When I export the data set I would like to include variable labels (i.e. the column below), I am not asking about value labels which describe the levels of the variable:

Variable label

Is there an option in haven that allows me to set this variable label?

I have searched the documentation and found only functions to set value labels. I notice haven is a wrapper for ReadStat which seems to support variable labels. In the ReadStat documentation a variable label (Citizenship of respondent) can be seen in the chunk below:

{
    "type": "SPSS",
    "variables": [
        {
            "type": "NUMERIC",
            "name": "citizenship",
            "label": "Citizenship of respondent",
            "categories": [
                {
                    "code": 1,
                    "label": "Afghanistan"
                },
...

My understanding of C++ is unfortunately not sophisticated enough to understand how haven works under the hood, so any suggestions are very welcome.

I have found a workaround, which involves manually setting the variable label by use of attributes. Consider the example below, using a teaching data set from the UK Data Service:

# install.packages("tidyverse")
library("tidyverse")

tmp    = tempfile(fileext = ".zip")
tmpdir = tempdir()

download.file(
  "http://ws.ukdataservice.ac.uk/REST/Download/Download/DSO/7912spss_e5b795672124e5b409e4a53c1a06fb9e.zip",
  destfile = tmp
)
unzip(tmp, exdir = tmpdir)

tmpdir = paste0(tmpdir, "/UKDA-7912-spss/spss/spss19/")
file   = paste0(tmpdir, list.files(tmpdir))

dat = haven::read_sav(file)

str(dat)

# Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 22428 obs. of  14 variables:
#  $ CASENEW : atomic  1 2 3 5 5 6 6 7 8 9 ...
#   ..- attr(*, "label")= chr "New random ID number"
#   ..- attr(*, "format.spss")= chr "F8.2"
#   ..- attr(*, "display_width")= int 10
# ...

I can therefore change the variable label with:

attr(dat$CASENEW, "label") = "Foo"
attr(dat$CASENEW, "label")
# "Foo"

Which, when I write to a new .sav file, does indeed open as intended in SPSS. My question is, is there a native way to do this in haven?


Solution

  • Hadley's answer:

    Just set the attributes

    — Hadley Wickham (@hadleywickham) October 27, 2017


    So there you have it: the canonical haven answer is just to set the attributes.