Reading Stata 14 file in R
Asked Answered
A

4

14

I have tried a thousand different times to read a Stata14 file on R, and for some reason I keep getting weird things happening (like variables dropped and such.)

The original file is saved in Stata 13 or 14, so the read.dta() command does not work. I also tried read.dta13() and it reads it sometimes but it cuts off the database at a certain spot and does not give me all the variables contained in the data set (which I can see and work with perfectly on Stata). The original file can be found here and scrolling down to Uruguay.

Anyone got any ideas on how to fix this issue? I am tired of arguing with R to read my Stata file correctly, when it works perfectly in Stata.

Arioso answered 20/2, 2016 at 4:35 Comment(6)
Why not load into STATA and outsheet as a CSV. Or use saveold in STATAContradictory
Can you tell us what precisely you've tried? Have you tried with a recent/development version of the haven package ?Humanist
@JacobH I tried saveold and to save it in Stata12 and it still is not working... R reads part of the data base, but not all of it. R is not reading part of the variables that I need.Arioso
@BenBolker I have tried read.dta(), read.dta13(), saveold in Stata to save into an older format. I have also downloaded the database in SPSS and the same issue happens -- R only reads it partially and drops some variables. Haven't tried the haven package, but will do it now and report back the results of my attempt.Arioso
Then try outsheet in STATA. This will allow you to transform the data to a .CSV. You can then read into R.Contradictory
The dataset aren't version Stata 13 or 14 files, but version 12 and below (I saw one file saved in Stata 8). I could read the files without problems with read.dta13.Enmity
J
21

I know this is an old thread but every time I google "read stata 14 with R" I come to this unresolved answer.

Community on SO have answered this: Read Stata 13 file in R

As of today, there is a CRAN package to read stata 13 and 14 using this:

install.packages("readstata13")

library(readstata13)
dat <- read.dta13("myStataFile.dta")

I hope you find this useful.

Jackjackadandy answered 23/5, 2016 at 23:27 Comment(0)
N
20

Have a look at Hadley's haven package (CRAN, github). It:

Works with Stata 13 and 14 files (foreign only works up to Stata 12).

Can also write SPSS and Stata files (This is hard to test so if you run into any problems, please let me know).

Once installed you simply:

read_dta("path/to/file")
Needy answered 20/2, 2016 at 11:15 Comment(0)
G
2

Instead of forcing other packages to read your data, you should convert your data to a common format such as CSV or Excel sheet XLS or XLSX. Stata allows you to create a decent text data set that is highly portable in almost any version of any statistics software.

To do so, go to file -> Export or just use export delimitedor export excel respectively. for more details type help export in your Stata command line.

You can also downgrade your data to Stata 12, or even Stata 11 by using the saveold command:

saveold "name.dta", version(11)

This command will create the oldest dataset that can be created in Stata 14. This probably would solve your problem, but still, I recommend using the CSV format. It's just how it works when transferring data between different software.

Genome answered 21/2, 2016 at 1:35 Comment(1)
I don't think users should be required to have the original software installed. I cannot buy SPSS if someone sends me a SPSS file. Data files should be easy to convert to the software researchers use. (R allows for that, curiously Stata does that only to a limited degree, and refers to an expensive software called Stat/Transfer.)Model
T
0

To convert too modern Stata data into the older version (say 14 into 13), the following code could help you:

write_dta(Data, "Path, version = 13, label = attr(data, "label"))
Typify answered 3/6, 2021 at 12:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.