5.5 Exporting to and Importing from SPSS, SAS, and Stata Files

Now, you know how to export and import data from delimited files and Excel files. In this section, you will learn how to export and import data from other statistical software including SPSS, SAS, and Stata. We will use the package haven, another member of the tidyverse family.

The following table summarizes the key functions in the haven package.

Table 5.4: Summary of haven Functions for Statistical Software Files
Software Action Function Extension
SPSS Export write_sav() .sav
SPSS Import read_spss() .sav / .por
SAS Export write_sas() .sas7bdat
SAS Import read_sas() .sas7bdat
Stata Export write_dta() .dta
Stata Import read_dta() .dta

5.5.1 Export and Import SPSS Files

Let’s first load the package haven, and prepare a data frame for exporting.

library(tibble)
library(haven)
dig_num <- 7:1
ani_char <- c("sheep", "pig", "monkey", "pig", "monkey", NA, "pig")
conditions <- c("Excellent", "Good", NA, "Fair", "Good", "Good", "Excellent")
my_animals <- tibble(dig_num, ani_char, conditions)
my_animals
#> # A tibble: 7 × 3
#>   dig_num ani_char conditions
#>     <int> <chr>    <chr>     
#> 1       7 sheep    Excellent 
#> 2       6 pig      Good      
#> 3       5 monkey   <NA>      
#> 4       4 pig      Fair      
#> 5       3 monkey   Good      
#> 6       2 <NA>     Good      
#> 7       1 pig      Excellent

The data frame my_animals will be used as in Section 5.2. You can use the function write_sav() to export a data frame into a SPSS .sav file.

write_sav(my_animals, "data/my_animals.sav")

To read a SPSS file ending in .sav or .por, you can use the function read_spss() which will automatically call read_sav() for .sav files and read_por() for .por files.

my_animals_spss <- read_spss("data/my_animals.sav")
#> Error:
#> ! 'data/my_animals.sav' does not exist in current working directory ('/home/runner/work/r02pro_source/r02pro_source').
head(my_animals_spss)
#> Error:
#> ! object 'my_animals_spss' not found

You may notice that the imported data has additional type information (e.g., <chr> appears as <chr+lbl>). This is because SPSS files can store value labels — metadata that maps values to descriptive labels. The haven package preserves this metadata using a special labelled class. You can convert labelled columns to regular R types using as_factor() (for labelled factors) or zap_labels() (to remove labels entirely).

5.5.2 Export and Import SAS Files

You can use the function write_sas() to export a data frame into a SAS .sas7bdat file.

write_sas(my_animals, "data/my_animals.sas7bdat")

To import a SAS file, you can use the function read_sas().

my_animals_sas <- read_sas("data/my_animals.sas7bdat")
#> Error:
#> ! 'data/my_animals.sas7bdat' does not exist in current working directory ('/home/runner/work/r02pro_source/r02pro_source').
head(my_animals_sas)
#> Error:
#> ! object 'my_animals_sas' not found

SAS has a maximum variable name length of 32 characters. If your R data frame has column names longer than 32 characters, write_sas() will truncate them. Make sure to check your column names before exporting to SAS.

5.5.3 Export and Import Stata Files

Lastly, let’s talk about Stata files. You can use the function write_dta() to export a data frame into a Stata .dta file.

write_dta(my_animals, "data/my_animals.dta")

To read a Stata file ending in .dta, you can use the function read_dta().

my_animals_stata <- read_dta("data/my_animals.dta")
#> Error:
#> ! 'data/my_animals.dta' does not exist in current working directory ('/home/runner/work/r02pro_source/r02pro_source').
head(my_animals_stata)
#> Error:
#> ! object 'my_animals_stata' not found

Stata variable names cannot contain special characters (such as . or spaces) and must be 32 characters or fewer. The write_dta() function will return an error if column names do not meet these requirements. You can rename columns using names() or dplyr::rename() before exporting.

5.5.4 Import using the menu

Similarly as Sections 5.3 and 5.4, you can also use the menu in Table 5.1 to import SPSS, SAS, and Stata Files.

5.5.5 Exercises

  1. Export the first 8 observations in the sahp dataset (in r02pro package) to SPSS, SAS, and Stata file formats, respectively.
  2. Import the files from Q1 to R and verify their contents.
  3. Import the SPSS file from Q1 and use str() to inspect the structure. What differences do you notice compared to the original sahp data?
  4. Try exporting a data frame with a column name longer than 32 characters to a SAS file. What happens?

Buy Me A Coffee