Importing R datasets in Stata

April 09, 2020

In this post we look at how you would import a R dataset into Stata. In particular we will export the happy dataset which is in the productplots package. Doing it all from the beginning, on Windows would look something like this.

First, using chocolately install R and its dependencies.

choco install r.project -y
choco install rtools -y
choco install r.studio -y

When installations are complete, open R Studio and from the terminal install the productplots package.

install.packages("devtools")
devtools::install_github("hadley/productplots")

That’s it basically. In the console in R Studio, load up the happy dataset in order to verify that everythings working:

Inspecting the happy dataset in R Studio

That’s all good. Now we are ready to export the dataset for use in Stata.

require(foreign)
write.dta(happy, "happy.dta")

Now the happy.dta dataset should be found on disk and ready to be used in Stata.

Inspecting the happy dataset in Stata


Profile picture

Written by Johan Osterberg who lives and works in Gothenburg, Sweden as a developer specialized in e-commerce. Connect with me on Linkedin

2024 © Johan Osterberg