Wednesday, November 28, 2007

Step 3: Choosing an External Text Editor

Stata provides many ways for interacting with the program. You can use the drop down menus, of course. You can write your commands in the Command window. Or you can use Stata's built-in .do file editor, like so:

Picture 2.jpg

Picture 1.jpg The notepad button opens a new do file

Picture 3.jpg

However, I'll suggest that you use an external text editor instead of the built-in text editor.

I'm not a snob about this. I still use the menus for many commands, especially new ones I'm not familiar with. However, eventually you'll have to write long files to:

  • Label (label var command)
  • Import (infix or infile command)
  • Define variable values (label var def command)
  • Summarize (summ/desc/codebook commands)

    Using a text editor is easier for the following reasons:

    1. Text editors make search-and-replace easier if you make a systematic mistake
    2. Text editors allow you to have Stata closed while writing
    3. Text editors (at least the ones outlined here) highlight syntax, which is invaluable for checking your code
    4. Text editors just have more options than the built-in editor (keyboard shortcuts, multiple views, ready-made templates)
    5. Text editors, as the name might suggest, make editing existing .do and .dct files much easier

    As far as available editors for Mac, there are many. Look around VersionTracker and you'll find all sorts of free or cheap options, like SubEthaEdit, TextWrangler, Smultron, Vim, and lots of others.

    If you're willing to pay a bit more, there are very full-featured programs like BBEdit ($49 US educational, $125 otherwise) and TextMate (39 Euros). Although these are impressive, they are geared much more towards professional web developers and are, in my opinion, a bit of overkill for Stata.

    I can't say I've tried all of these, of course. But I've experimented with a number of editors, including the built-in TextEdit, Taco Edit, Aquamacs, Smultron and TextWrangler.

    Unlike some built-in OS X software, TextEdit isn't too impressive. There are far better choices for free.

    Taco Edit is really designed for HTML coding, not other languages.

    Aquamacs is pretty good, some people really swear by it, including many of the skilled and serious programmers I know. The main objection I had was that it was much more difficult to integrate with Stata. You need to install a series of .ado files into Stata to make Aquamacs work as an external editor. Read this if you're interested.

    I haven't tried Vim.

    So we're down to two contenders:

    Smultron (open-source, freeware)

    Smultron icon

    Smultron is nice. It has a pleasant Cocoa interface. It handles multiple open files easily. It doesn't take much memory. Here's a screenshot:

    Picture 2.jpg

    TextWrangler (non-open-source, freeware)

    TextWrangler icon

    However, I'm going to recommend TextWrangler, the free version of BBEdit developed by Bare Bones Software. Here's what it looks like:


    Most importantly, TextWrangler integrates extremely easily with Stata:

    1. Save a file with a .do extension, and TextWrangler will immediately recognize it as a file that should be formatted according to Stata syntax and run in
    2. The defaults can be set so new files are always formatted as .do files
    3. Once you've saved a .do file, you can quickly run it in Stata like this:

    Picture 5.jpg

    Click the button on the right and the file will pop up in the Finder window:

    Picture 7.jpg

    Double-click the file in Finder and it will immediately run in Stata; if Stata is closed, it will immediately open it and run it.

    And here's a really nice feature: at the bottom of TextWrangler, you can select your language and it will immediately recognize the command words. So if you need to do some website editing in CSS or HTML (as I sometimes do), TextWrangler is very helpful for that as well.

    Picture 4.jpg

    While you can't go wrong with either Smultron or TextWrangler, I'd recommend the latter as a free, full-featured, do-it-all program for Stata coding.

    Next installment will deal with Step 4: Importing Unformatted Data

    Technorati Tags:
    , , , , ,


    Dave said...

    I have BBEdit and it does not manage to handle .do files created in BBEdit the same way you say TextWrangler does. I'll try again...

    csgzs said...

    Hi, I am glad I have found your blog as it seems to address also the problem I am trying to solve: finding an external editor that is fully integrated with Stata, including the ability to submit code. However, I do not seem to be able to find a reference. Am I mistaken or is this bit really missing? I have checked the excellent page by Nicholas J. Cox, but also there I am unable to find the answer. (Yes, I am using Text Wrangler too) Is there any? Thanks, Zsombor

    Joel said...

    Thanks for the tips. I'm considering turning to TextWrangler. Does it allow you to do or run only part of a .do file like STATA's built on editor does?

    Adam said...

    Hi everyone. I'm afraid "integration" is too strong a word here, my apologies. Several people have emailed asking about scripting or sending snippets of code directly to Stata. I don't think this is possible with Textwrangler (please tell me if it is).

    Textwrangler is still my preferred editor but code submission is limited to clicking the stata icon at the top of text wrangler to reveal the file in finder, then double-clicking in finder to run it.

    Sorry for the misunderstanding. - Adam

    Eric L said...

    A very helpful source for more TextWrangler to Stata material:

    Hilary said...

    viagra pharmacy
    viagra online pharmacy

    Effect Matrix said...

    Thanks for sharing in detail. Your blog is an inspiration! Apart of really useful tips, it's just really ! This post will be effectively Just about everything looks good displayed.
    Mac Photo Editor

    dolly chaudhary said...

    There is a reason why I bought a Mac, it was not to be one of the cool kids that wanted the best of the best. No it was because of the music production I do on it. Hours and hours sitting in front of my Mac copying, pasting, moving, deleting, hour after hour just beating on my Mac in a endless assault to get my work done. That is the key part, my work. I work from home, it is great, but even if it is from home it is still work and it still needs to get done. So my Mac, I have it because it is fast, gets the job done and comes back for more.
    But what happens when it doesn't want to do those things anymore?
    I move around massive amounts of information and yes even on the almighty Mac this can cause a problem after a while. Things fragment, programs get corrupted issues come up. My light speed Mac slows down to a crawl and all of the sudden I simply can not get any work done. Because I work from home there is no IT guy to call and ask to come fix it. No instead I have to figure out what is wrong. I am lucky, I did, but not after trying everything under the sun first and wasting countless hours looking for one program that can do what I needed instead of ten programs. One program to lead them all….okay that was a lame Lord of the rings reference, but that program was/is Detox My Mac. A simple to use program that did not just fix my issues, it put my Mac on overdrive again. A few clicks and my Mac was clean and ready to rock and roll again.
    Read more here:-