1.6 Loading Data from SPSS/SAS/Stata Files

1.6.1 Problem

You want to load data from a SPSS file, or from other programs like SAS or Stata.

1.6.2 Solution

The haven package has the function read_sav() for reading SPSS files. To load data from an SPSS file:

# Only need to install the first time
install.packages("haven")

library(haven)
data <- read_sav("datafile.sav")

1.6.3 Discussion

The haven package also includes functions to read from other formats:

  • read_sas(): SAS
  • read_dta(): Stata

An alternative to haven is the foreign package. It also supports SPSS and Stata files, but it is not as up-to-date as the functions from haven. For example, it only supports Stata files up to version 12, while haven supports up to version 14 (the current version as of this writing).

The foreign package does support some other formats, including:

  • read.octave(): Octave and MATLAB
  • read.systat():SYSTAT
  • read.xport(): SAS XPORT
  • read.dta(): Stata
  • read.spss(): SPSS

1.6.4 See Also

Run ls("package:foreign") for a full list of functions in the foreign package.