Message Boards Message Boards

0
|
16590 Views
|
4 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Importing STATA files

Posted 10 years ago
Hi there!

How can I import .dta files in Mathematica?

My current approach is first reading the .dta in R, exporting as csv, and then reading the .csv in Mathematica. I wish I could import directly.
Is this possible?

A random  .dta file may be downloaded from here: http://ge.tt/5YPA3xh1/v/0
POSTED BY: Sandu Ursu
4 Replies
So, this might not work perfectly, but why not leverage RLink?

     Needs["RLink`"];
     InstallR[];
     REvaluate["library('foreign')"];
     Shallow[rf=REvaluate["read.dta('airfare.dta')"]];

If you now look at rf, you will see that it has three parts, including an evocative second part with the header RData.  If we then try this:

     Transpose[List @@ rf[[2]]]

We get something that sure looks like the data you want.  

I don't vouch that this is (a) the best way or (b) works for all forms of .dta files (about which I know nothing), but since R knows what to do with .dta files and Mathematica now speaks to R, why not put the two together?

     
POSTED BY: Seth Chandler
I think Seth is right - it is a good idea to use R link. Same idea but more generally can be found here - togethere with a few other ones: Convert dta file to CSV
POSTED BY: Sam Carrettie

I am trying to read .dta with RLink as suggested by Seth but Mathematica doesn't seem to acknowledge the package I want to use.

I want to use the read.dta13 from the readstata13 package.

Needs["RLink`"];
InstallR["RHomeLocation" -> "C:\\Program Files\\R\\R-3.4.1"]
REvaluate["install.packages('readstata13')"]
REvaluate["library('readstata13')"]

Mathematica returns the following error: there is no package called 'readstata13'.

Am I missing a step after installing a package? Maybe I need to reference it before using it.

Thanks

With Mathematica 12 it's very easy to import Stata files with the Import[] command. See: https://reference.wolfram.com/language/ref/format/DTA.html

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract