4.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 the 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.

4.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 4.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")
head(my_animals_spss)
#> # A tibble: 6 × 3
#>   dig_num ani_char conditions 
#>     <dbl> <chr>    <chr>      
#> 1       7 "sheep"  "Excellent"
#> 2       6 "pig"    "Good"     
#> 3       5 "monkey" ""         
#> 4       4 "pig"    "Fair"     
#> 5       3 "monkey" "Good"     
#> 6       2 ""       "Good"

4.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")
head(my_animals_sas)
#> # A tibble: 6 × 3
#>   dig_num ani_char conditions 
#>     <dbl> <chr>    <chr>      
#> 1       7 "sheep"  "Excellent"
#> 2       6 "pig"    "Good"     
#> 3       5 "monkey" ""         
#> 4       4 "pig"    "Fair"     
#> 5       3 "monkey" "Good"     
#> 6       2 ""       "Good"

4.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")
head(my_animals_stata)
#> # A tibble: 6 × 3
#>   dig_num ani_char conditions 
#>     <dbl> <chr>    <chr>      
#> 1       7 "sheep"  "Excellent"
#> 2       6 "pig"    "Good"     
#> 3       5 "monkey" ""         
#> 4       4 "pig"    "Fair"     
#> 5       3 "monkey" "Good"     
#> 6       2 ""       "Good"

4.5.4 Import using the menu

Similarly as Sections 4.3 and 4.4, you can also use the menu in Table 4.1 to import SPSS, SAS, and Stata Files.

4.5.5 Exercise

  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.