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.
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.
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.
To import a SAS file, you can use the function read_sas()
.