Skip to content

Import a Foreign Data File

NickCH-K edited this page Oct 17, 2019 · 1 revision

Import a Foreign Data File

Commonly, data will be distributed in a format that is not native to the software that you are using, such as Excel. How can you import it?

This page is specifically about importing data files from formats specific to particular foreign software. For importing standard shared formats, see Import a Delimited Data File (CSV, TSV) or Import a Fixed-Width Data File.

Keep in Mind

  • Check your data after it's imported to make sure it worked properly. Sometimes special characters will have trouble converting, or variable name formats are inconsistent, and so on. It never hurts to check!

Also Consider

Implementations

Because there are so many potential foreign formats, these implementations will be more about listing the appropriate commands with example syntax than providing full working examples. Make sure that you fill in the proper filename. The filename should include a filepath, or you should Set a Working Directory.

R

# Load Excel files with the readxl package
# install.packages('readxl')
library(readxl)
data <- read_excel('filename.xlsx')

# Read Stata, SAS, and SPSS files with the haven package
# install.packages('haven')
library(haven)
data <- read_stata('filename.dta')
data <- read_spss('filename.sav')
# read_sas also supports .sas7bcat, or read_xpt supports transport files
data <- read_sas('filename.sas7bdat')

# Read lots of other types with the foreign package
# install.packages('foreign')
library(foreign)
data <- read.arff('filename.arff')
data <- read.dbf('filename.dbf')
data <- read.epiinfo('filename.epiinfo')
data <- read.mtb('filename.mtb')
data <- read.octave('filename.octave')
data <- read.S('filename.S')
data <- read.systat('filename.systat')

Stata

Stata can import foreign files using the File -> Import menu. Alternately, you can use the import command:

import type using filename

where type can be excel, spss, sas, haver, or dbase (import can also be used to download data directly from sources like FRED).