Stata in Atom

If you want to use Atom as your Stata editor, here is what you need to know. First, you need the language-stata package for proper syntax highlighting. To actually run code from Atom in Stata, you can either use the script or the stata-exec package in Atom. Alternatively, you can use hydrogen, this approach is described here.

The stata-exec package comes from the same author as language-stata, it currently only works for Mac and Linux, and it has as of today a little more than 400 downloads. I first went for the script package, since it has already more than a million downloads and it also supports more operating systems (Windows, Mac, and Linux) as well as other languages than just Stata. However, script comes with a couple of caveats: atom has to be run from command line for it to work properly, the package code might need some adjusting (depending on your Stata version), and most importantly whenever you send code to Stata it opens a new Stata window, making it useless for interactive sessions.

Adding Stata to your PATH (on Mac)

You need to make sure that Stata is added to you PATH. To add Stata to your PATH you first need to know the right path to add. Here is a guide on figuring this out. For example it could be at /Applications/Stata/StataMP.app/Contents/MacOS/. To add it to the path you can enter the following command line in your terminal.


export PATH=$PATH:/Applications/Stata/StataMP.app/Contents/MacOS/

However, this will only add it to your path for the current session. To get it done permanently, you can add the line of code at the end of your ~/.profile file. You can easily do this in Atom. To test if you were successful, type StataMP (if you have Stata/MP, otherwise Stata, StataSE, or smStata depending on your version)  in your terminal and hit enter. If it starts Stata you are good. Otherwise type echo $PATH to see if your Stata path was added to the PATH.

Option 1: stata-exec

After picking your flavor of Stata in the settings (e.g. Stata/MP) everything should work as expected. You can use Cmd+Return to send highlighted code to Stata.

Option 2: script

Fixing the script package for your Stata version

The current version of script uses stata as the command to run Stata. If that is not what you need for your Stata version (e.g. if you are using Stata/MP, you would need StataMP), you will have to fix this in the code of the package. I found this solution in a discussion on Stackoverflow. However, the script code seems to have changed since then and they had to make slightly different changes than I did. I, therefore, recommend searching for the word “stata” in all files of the script package (you can find it under ~/.atom/packages/script/) to find the relevant instances. I had to change lines 311 and 314 in index.coffee (which you can find under ~/.atom/packages/script/lib/grammars/). 

stataScript

The key-binding

The script package uses Cmd+I as the main keybinding to run code. That did conflict with another package that I had installed and I would rather use Cmd+Return for running code anyways. If you want to change the keybinding for running code, you need to add it to your keymap.cyson (Atom>Keymap…). For me that just meant adding the following code at the end of the file.


'atom-text-editor':
'cmd-enter': 'script:run'

Remaining problem

Unfortunately, using the script package to run code in Stata always opens a new window every single time, which makes it useless for interactive sessions.

2 thoughts on “Stata in Atom

  1. thank you so much! I wonder if you know how to use snippets in atom for Stata. The snippets work for Javascript, but not for do-file yet.

    Like

Leave a comment