Convert a dta file to csv without Stata software

155,777

Solution 1

The frankly-incredible data-analysis library for Python called Pandas has a function to read Stata files.

After installing Pandas you can just do:

>>> import pandas as pd
>>> data = pd.io.stata.read_stata('my_stata_file.dta')
>>> data.to_csv('my_stata_file.csv')

Amazing!

Solution 2

You could try doing it through R:

For Stata <= 15 you can use the haven package to read the dataset and then you simply write it to external CSV file:

library(haven)
yourData = read_dta("path/to/file")
write.csv(yourData, file = "yourStataFile.csv")

Alternatively, visit the link pointed by huntaub in a comment below.


For Stata <= 12 datasets foreign package can also be used

library(foreign)
yourData <- read.dta("yourStataFile.dta")

Solution 3

You can do it in StatTransfer, R or perl (as mentioned by others), but StatTransfer costs $$$ and R/Perl have a learning curve.
There is a free, menu-driven stats program from AM Statistical Software that can open and convert Stata .dta from all versions of Stata, see:

http://am.air.org/

Solution 4

I have not tried, but if you know Perl you can use the Parse-Stata-DtaReader module to convert the file for you.

The module has a command-line tool dta2csv, which can "convert Stata 8 and Stata 10 .dta files to csv"

Solution 5

Another way of converting between pretty much any data format using R is with the rio package.

  • Install R from CRAN and open R
  • Install the rio package using install.packages("rio")
  • Load the rio library, then use the convert() function:

    library("rio")
    convert("my_file.dta", "my_file.csv")
    

This method allows you to convert between many formats (e.g., Stata, SPSS, SAS, CSV, etc.). It uses the file extension to infer format and load using the appropriate importing package. More info can be found on the R-project rio page.

Share:
155,777

Related videos on Youtube

Brian
Author by

Brian

Updated on July 05, 2022

Comments

  • Brian
    Brian almost 2 years

    Is there a way to convert a dta file to a csv?

    I do not have a version of Stata installed on my computer, so I cannot do something like:

    File --> "Save as csv"
    
    • Eli Bendersky
      Eli Bendersky about 14 years
      I'm sure there is a way. If the format of the .DTA file is specified, it can become a simple programming exercise
    • Brian
      Brian about 14 years
      it's binary, I'm not sure how to get it out of there
  • eric.a.booth
    eric.a.booth almost 14 years
    BTW, here is Stata's breakdown of how a .dta file is structured, which could be useful for extracting data elements: stata.com/help.cgi?dta
  • vsingh
    vsingh almost 11 years
    This is paid but you can download to try out.
  • jhleath
    jhleath over 9 years
    Note that this technique does not work if you are utilizing Stata 13 .dta files. You should utilize the techniques in this question.
  • radek
    radek over 9 years
    @huntaub Thanks huntaub, updated answer to clarify that is 12 downwards.
  • Admin
    Admin over 7 years
    I'm sure this works great for those already experienced with R, but for those who are not (like me), this will probably be frustrating. It took me an hour-plus of Googling and trial-and-error to figure out all the different packages you have to install before this actually works.
  • Matthew Vita
    Matthew Vita almost 7 years
    @KennyLJ I'm new to R and found this to be very easy. Just ran install.packages("rio") and was good to go.
  • Candamir
    Candamir over 6 years
    Note for the complete beginner: start with library(haven)
  • dǝɥɔS ʇoıןןƎ
    dǝɥɔS ʇoıןןƎ over 6 years
    Wow, I can't believe Pandas supports Stata :O
  • Nick Cox
    Nick Cox about 6 years
    I took the liberty of adding more information on dta versions.
  • Admin
    Admin about 6 years
    Thanks. I was surprised to see that these details are literally buried in source code so i thought to post them on here for others.
  • Nick Cox
    Nick Cox almost 4 years
    They are not "literally buried in source code" but documented openly.
  • ancient geek
    ancient geek over 3 years
    This certainly worked for me. Very simple, can be done from command line, and totally free
  • shannontesla
    shannontesla about 3 years
    Thank you sooo much!