Chapter 11 Stata

Resources:

help <cmd_name>: Get help for a command.

Keyboard Shortcut Description
ctrl + R last cmd
ctrl + B next cmd
cmd + shift + D Run a Do file

User interface

Within the Stata interface window, there are five windows: Command, Results, History, Properties, and Variables

While Stata can be command-driven by typing code in the Command window, it can also be used in a point-and-click manner using the menu bar.

While nearly everything in Stata can be done via the menus, you’re better off typing commands into a word processing file and saving them, then copying-and-pasting them into the Stata “Command” window.

Buttons

  • Log: Track and save output from the Results window. Ensures replicability.

  • New Do-file Editor: Organize your history commands in one place, making debugging easier.

    You can use do-files to create a batchlike environment in which you place all the commands you want to perform in a file and then instruct Stata to do that file.

    Ex. You have a do file myjob.do, you can run

    do myjob

    all commands in the do file would be sourced.


Do-file

It is recommended to run do files as a whole. (This is different than R.)

You cannot re-run commands freely in Stata.

For example, if you run a command that creates a variable x, realize you made a mistake, and then fix it, you can’t simply select the command that creates x and run it again because x already exists. You could manually drop the existing version of x, but now you’re doing things in a non-reproducible way. Running the entire do file will eliminate this problem because it reloads the data from disk every time. If you find yourself getting confused by these kinds of issues, run the entire do file rather than a selection.

Do-file Rule of Thumb

  • Your .do file begins with loading a dataset and ends with saving one.

    use ..., clear // begin
    /* your code */
    save ..., replace // end
  • Never modify the raw data files. Save the results of your data cleaning in a new file.

  • Every data file is created by a script. Convert your interactive data cleaning session to a .do file.

  • No data file is modified by multiple scripts.

  • Intermediate steps are saved in different files (or kept in temporary files).

Keep do files short

Our suggestion is that you keep your do files short enough that when you’re working on one of them you can easily wrap your head around it. You also want to keep do files short so they run as quickly as possible: working on a do file usually requires running it repeatedly, so moving any code that you consider “done” to a different do file will save time.

Project Structure

  • You can have a master do file which loads your small section do files sequentially and all in one.

  • Enumerate your do files.

    Example: 0-master.do, 1-data-clean.do, 2-stylized-facts.do, …

    You can then organize them in sub-do-files: if you have different set of stylized facts, you

    could have: 2.1-stylized-facts-geography.do, 2.2-stylized-facts-count.do etc. . . .


log file

log files put everything that your do file put in the Results window.


Comments

  • // for single line comment
  • /* */ for multiple line comment
  • //# or **# add a bookmark

Continuation lines: /// Everything after /// to the end of the current line is considered a comment. The next line joins with the current line. Therefore, /// allows you to split long lines across multiple lines in the do-file.

Summary of ways to break long lines:

  • You can change the end-of-line delimiter to ; by using #delimit,

    #delimit ; // change the line delimiter to semicolon
    summarize weight price displ headroom rep78 length turn gear_ratio
        if substr(company,1,4)=="Ford" |
           substr(company,1,2)=="GM", detail ;
    gen byte ford = substr(company,1,4)=="Ford" ;
    #delimit cr // change the delimiter back to carriage return
    gen byte gm = substr(company,1,2)=="GM"

    Once you declear #delimit ;, all lines must end in ;. Stata treats carriage returns as no different from blanks.

  • you can comment out the line break by using /* */ comment delimiters, or

  • you can use the /// line-join indicator.


cd "directory_name" change working directory.

  • don’t need quotation if there is no space
  • need quotation if the directory has spaces

pwd displays the path of the current working directory.

exit, clear to quit Stata. If the dataset in memory has changed since the last time it was saved, Stata will refuse to quit unless you specify clear.


Abbreviation rules: Stata allows abbreviations. You can abbreviate commands, variable names, and options.

// full command
. summarize myvar, detail
// use abbr. to achieve the same function
. sum myv, d

As a general rule, command, option, and variable names may be abbreviated to the shortest string of characters that uniquely identifies them.

When you read the Stata manual, it uses underlines to denote the minimal abbreviation for a command or option.

E.g. When you see append, it means you can use ap to denote append. describe means the shortest allowable abbreviation for describe is desc.

If there is no underlining, no abbreviation is allowed.

rename can be abbreviated ren, rena, renam, or it can be spelled out in its entirety.

Open do files in tabs rather than in separate windows: https://www.reddit.com/r/stata/comments/1ivjegr/stata_18_mac_does_not_do_tabs_for_dofile_editor/