Read Stata 13 file in R
Asked Answered
A

6

56

Is there a way to read a Stata version 13 dataset file in R?

I have tried to do the following:

> library(foreign)
> data = read.dta("TEAdataSTATA.dta") 

However, I got an error:

Error in read.dta("TEAdataSTATA.dta") :
not a Stata version 5-12 .dta file

Could someone point out if there is a way to fix this?

Avoid answered 27/5, 2014 at 21:10 Comment(4)
Not within the foreign package. ?read.dta: " Frozen: will not support Stata formats after 12".Extinctive
@BenBolker Thanks Ben, is there something that does it for a Stata 13 file (by the way, i think your father is Ben Bolker, he taught me math a long time ago :) , sorry if this is strange )Avoid
you mean Ethan Bolker, right? Don't know, sorry -- probably someone would have to sit down and reverse-engineer the format. stata.com/statalist/archive/2013-10/msg00701.html comments that transfer to SPSS is hard now, too.Extinctive
r.789695.n4.nabble.com/…Extinctive
S
13

If you have Stata 13, then you can load it there and save it as a Stata 12 format using the command saveold (see help saveold). Afterwards, take it to R.

If you have, Stata 10 - 12, you can use the user-written command use13, (by Sergiy Radyakin) to load it and save it there; then to R. You can install use13 running ssc install use13.

Details can be found at http://radyakin.org/transfer/use13/use13.htm

Other alternatives, still with Stata, involve exporting the Stata format to something else that R will read, e.g. text-based files. See help export within Stata.

Update

Starting Stata 14, saveold has a version() option, allowing one to save in Stata .dta formats as old as Stata 11.

Stomato answered 27/5, 2014 at 21:24 Comment(4)
Ok thanks a million. But sorry I still have one problem, when I try to write to convert the data to an spss file, I type: Suppose I have imported the data from the STATA file to a data frame called data1, then I write the data frame to a text file using write.table(data , 'mydata.txt' , sep="\t") and then to get the SPSS file I do: write.foreign(b , "mydata.txt" , "DerLeew.sps" , package="SPSS") but I get error: Error in writeForeignSPSS(df = list(studyid = c("P0008", "P0018", "P0031", : I cannot abbreviate the variable names to eight or fewer letters, thanks.Avoid
I can't say why. My knowledge of R is more limited. I do wonder what's going on with all those conversions: Stata to R to txt to SPSS. If you want to convert Stata to SPSS, try the Stata command savespss, again by Sergiy Radyakin. Read the following to get started: radyakin.org/transfer/savespss/savespss.htm.Stomato
wouldn't I have to use Stat/Transfer to do it directly in STATA?Avoid
Not at all. Sergiy has made a great effort on the issue of data transfer. All you need to do is install that command with net from http://radyakin.org/transfer/savespss/beta and read the web page in my previous comment to learn the (very simple) syntax. (By the way, the spelling is Stata, not STATA. It's not an acronym. )Stomato
J
97

There is a new package to import Stata 13 files into a data.frame in R.

Install the package and read a Stata 13 dataset with read.dta13():

install.packages("readstata13")

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

Update: readstata13 imports in version 0.8 also files from Stata 6 to 14

More about the package: https://github.com/sjewo/readstata13

Janaye answered 24/9, 2014 at 13:32 Comment(6)
I get Error in loadNamespace(name) : there is no package called ‘httr’ when attempting to execute the devtools::install_github("sjewo/readstata13", ref="0.2") line.Shammer
Hi Dan! Your devtools is probably outdatet and the httr package is missing. Try to update your packages install.packages("devtools", dependencies=T) and check if you can load devtools with library(devtools) . Maybe you could also check for httr with library(httr) .Janaye
What does your package do with any strL variables in the dataset?Gestapo
strL variables inherit a refer to a string in a list obtained by attr(NameOfDataset, "strl") .Janaye
In version 0.4 the option replace.strl=TRUE can be set to replace the reference to a strL string in the data.frame with the actual value.Janaye
Adding that by my benchmark, readstata13 is faster, albeit on a very small data set (420 obs x 12 var); not sure how it scales up so I'm not adding it to the answerDeel
V
29

There's a new package called Haven, by Hadley Wickham, which can load Stata 13 dta files (as well as SAS and SPSS files)

library(haven) # haven package now available on cran
df <- read_dta('c:/somefile.dta')

See: https://github.com/hadley/haven

Voltcoulomb answered 10/2, 2015 at 15:34 Comment(3)
Just a head's up that haven did not seem to be as accurate as readstata13 as far as formatting, marking many numerical variables as character variables. So that may be something to consider for those using haven.Instrumentality
+1 because haven is also the best-suited, I've found, for importing other proprietary formats like .sas7bdat from SAS, see here, whereas readstata13 is clearly a single-purpose package.Deel
FYI, haven is now available on CRAN. No need to install from github.Blackmon
S
13

If you have Stata 13, then you can load it there and save it as a Stata 12 format using the command saveold (see help saveold). Afterwards, take it to R.

If you have, Stata 10 - 12, you can use the user-written command use13, (by Sergiy Radyakin) to load it and save it there; then to R. You can install use13 running ssc install use13.

Details can be found at http://radyakin.org/transfer/use13/use13.htm

Other alternatives, still with Stata, involve exporting the Stata format to something else that R will read, e.g. text-based files. See help export within Stata.

Update

Starting Stata 14, saveold has a version() option, allowing one to save in Stata .dta formats as old as Stata 11.

Stomato answered 27/5, 2014 at 21:24 Comment(4)
Ok thanks a million. But sorry I still have one problem, when I try to write to convert the data to an spss file, I type: Suppose I have imported the data from the STATA file to a data frame called data1, then I write the data frame to a text file using write.table(data , 'mydata.txt' , sep="\t") and then to get the SPSS file I do: write.foreign(b , "mydata.txt" , "DerLeew.sps" , package="SPSS") but I get error: Error in writeForeignSPSS(df = list(studyid = c("P0008", "P0018", "P0031", : I cannot abbreviate the variable names to eight or fewer letters, thanks.Avoid
I can't say why. My knowledge of R is more limited. I do wonder what's going on with all those conversions: Stata to R to txt to SPSS. If you want to convert Stata to SPSS, try the Stata command savespss, again by Sergiy Radyakin. Read the following to get started: radyakin.org/transfer/savespss/savespss.htm.Stomato
wouldn't I have to use Stat/Transfer to do it directly in STATA?Avoid
Not at all. Sergiy has made a great effort on the issue of data transfer. All you need to do is install that command with net from http://radyakin.org/transfer/savespss/beta and read the web page in my previous comment to learn the (very simple) syntax. (By the way, the spelling is Stata, not STATA. It's not an acronym. )Stomato
D
6

In the meanwhile savespss command became a member of the SSC archive and can be installed to Stata with: findit savespss

The homepage http://www.radyakin.org/transfer/savespss/savespss.htm continues to work, but the program should be installed from the SSC now, not from the beta location.

Doherty answered 22/11, 2014 at 5:37 Comment(0)
M
3

I am not familiar with the current state of R programs regarding their ability to read other file formats, but if someone doesn't have Stata installed on their computer and R cannot read a specific version of Stata's dta files, Pandas in Python can now do the vast majority of such conversions.

Basically, the data from the dta file are first loaded using the pandas.read_stata function. As of version 0.23.0, the supported encoding and formats can be found in a related answer of mine.

Then one can either save the data as a csv file and import them using standard R functions, or instead use the pandas.DataFrame.to_feather function, which exports the data using a serialization format built on Apache Arrow. The latter has extensive support in R as it was conceived to promote interoperability with Pandas.

Milkandwater answered 23/5, 2018 at 12:58 Comment(0)
S
1

I had the same problem. Tried read.dta13, read.dta but nothing worked. Then tried the easiest and least expected: MS Excel! It opened marvelously. I saved it as a .csv and used in R!!! Hope this helps!!!!

Suppositious answered 10/8, 2017 at 6:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.