not a Stata version 5-12 .dta file
Asked Answered
P

2

20

I am trying to use existed data in my pc which is .dta file.

I'm trying to open it in R using the following command:

library(foreign)
mydata<-read.dta(file="C:\\Users\\me\\Desktop\\data_raw.dta")

But there is this error

>> not a Stata version 5-12 .dta file 

Any help will be appreciated. Thanks.

Pleuropneumonia answered 29/8, 2018 at 10:57 Comment(4)
What command do you use to open it?Myceto
This was my command >> mydata<-read.dta(file="C:\\Users\\me\\Desktop\\data_raw.dta")Pleuropneumonia
Well, you obviously are using read.dta from the foreign package. It can only read files from Stata version 5 to 12 and your file seems to be created by a version outside that range. Maybe try read_dta from the haven package.Piloting
It works. Thank you.Pleuropneumonia
R
18

Had the same problem and Roland's suggestion of using read_dta from the haven package worked!

To reiterate Roland's comment that helped form this solution. read.dta from the foreign package can only read files from Stata version 5 to 12 and your file seems to be created by a version outside that range.

haven's read_dta can read dta files outside of this range.

Renaldo answered 24/8, 2019 at 19:39 Comment(1)
Alternatively, in Stata use saveold with option version(12) and then import in R.Pemba
D
6

What I want to add about the read_dta is that first you have to install haven, e.g.

install.packages("haven")
library(haven)
data <- read_dta("data.dta")
Disembodied answered 9/7, 2021 at 11:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.