Chapter 11 Stata

Resources:

help <cmd_name>: Get help for a command in Stata console.

Overview of Documentation:

  • [U] User’s Guide: is divided into three sections: Stata basics, Elements of Stata, and Advice.

    Recommended to read.

  • Base Reference Manual: list commands alphabetically.

    Not designed to be read from cover to cover.

  • The PDF documentation may be accessed from within Stata.

    help command_name and then click on the “View complete PDF manual entry” button under the command.

    Or in the menu bar, Help > PDF Documentation to open the complete PDF documentation.

    The pdf documentation uses Acrobat Reader as the viewer. Tip: use finger pinch to zoom in and out. When using the zoom button or cmd +/cmd -, the text jumps around, you lose your original position.

Keyboard Shortcuts

Actually not of much use.

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.

Output appears in the Results window. E.g.,

. sysuse auto, clear
(1978 Automobile Data)

The dot (.) indicates that the current line is a Stata command.

> indicates that the command is not yet complete. You will see this when you have a command that spans multiple lines.

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; rest-of-line comment;
  • /* */ for multiple line comment; enclosed comment;
  • //# or **# add a bookmark
  • /// line-join indicator

Note that the // comment indicator and the /// indicator must be preceded by one or more blanks.

See [U] 16.1.2 Comments and blank lines in do-files for more details.

Continuation lines: ///

/// is called the line-join indicator or line continuation marker. It makes long lines more readable.

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.

Example

replace final_result = ///
    sqrt(first_side^2 + second_side^2) ///
    if type == "rectangle"

equivalently, you can use /* */ to break long lines:

replace final_result = /*
    */ sqrt(first_side^2 + second_side^2) /*
    */ if type == "rectangle"

N.B. There’s NO line continuation marker (///) in the command window.

In the command window, the enter key sends what has been written on the line to Stata. There is no way to continue a long command on a second line, without sending the first (incomplete) line to Stata.

You can add comments after ///.

args a /// input parameter for a
     b /// input parameter for b
     c // input parameter for c

is equivalent to

args a b c

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/