Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 36 ยท Getting Started

Command Syntax and Getting Help

Learn the general Stata command syntax: prefix, command, varlist, if, in, weights and options after the comma. Use help, search and the manuals to find answers.

Stata's biggest advantage for learners is that nearly every command follows the same grammar. Once you can read that grammar, a command you have never seen becomes readable. This lesson teaches the pattern, shows how to read the syntax diagrams in help, and lists the places to look when you are stuck.

The general pattern

Most commands look like this:

STATA
[prefix:] command [varlist] [=exp] [if] [in] [weight] [using filename] [, options]

Square brackets mean "optional". A real example:

STATA
bysort region: summarize income age if age >= 18 in 1/500 [aweight = popweight], detail

Reading it from left to right:

Piece Example Meaning
prefix bysort region: Repeat the command for each group of region
command summarize The verb
varlist income age Which variables to use
if if age >= 18 Keep observations where the condition is true
in in 1/500 Keep observations by position
weight [aweight = popweight] Weighting in square brackets
options , detail Extra choices, always after one comma

Only the command name is always required, and the order of the pieces matters: the comma splits the command from its options, and everything after it is an option.

Variable lists

A varlist can use shortcuts:

STATA
summarize price mpg weight    // named variables
summarize price-weight        // all variables from price to weight in dataset order
summarize x*                  // every variable that starts with x
summarize *_id                // every variable ending in _id
summarize                     // no varlist: all variables (for commands that allow it)

Because - uses the dataset's column order, check it with describe first. Wildcards * and ? match any characters or exactly one character.

The if and in qualifiers

  • if selects observations where an expression is true: summarize price if foreign == 1.
  • in selects by row number: list in 1/10, list in -5/l (last five; l means last).

You can combine them: summarize price if foreign == 1 in 1/50. Two warnings. First, = assigns in generate, while == compares in if. Second, if here is not the programming if command; it is a qualifier on the command it follows. Programming conditionals use braces and live in do-files, covered later.

Options

Options come after a comma and change how a command works:

STATA
summarize price, detail           // percentiles, skewness, kurtosis
regress price mpg, robust         // robust standard errors
tabulate rep78, missing           // include missing values in the table
graph twoway scatter price mpg, title("Price vs mpg")

Options can be abbreviated, too: det for detail. A new user's most common error is a second comma, or an option before the comma. Stata then reports option xyz not allowed (r(198)).

Prefix commands

A prefix runs another command in a special way:

STATA
by foreign: summarize price       // data must be sorted by foreign first
bysort foreign: summarize price   // sorts automatically, then runs by group
quietly regress price mpg         // run without printing output
capture drop price_k              // run, ignore any error (stored in _rc)
xi: regress price i.rep78         // older factor-variable helper

bysort is the one you will use most often, so it has its own lesson later (sort and by).

Reading the help file

help summarize opens the documentation for a command. The top of every help file shows a syntax block. Underlined letters mean the shortest allowed abbreviation (in the help viewer, the su of summarize is underlined). Square brackets are optional parts. Curly braces such as {a | b} mean "choose one". Below the syntax are the option tables, examples and links to the manual entry.

Useful starting commands:

STATA
help summarize          // exact command or topic
search regression       // search all manuals and FAQs by keyword
findit panel data       // search net resources too (user-written commands)
help contents           // table of contents by topic
help regress postestimation  // what follows after regress

The difference between help and search: help needs the exact command name; search finds topics by word. When you do not know the command name, start with search.

Where to look when you are stuck

  1. The error code: search rc 198 explains return codes.
  2. help command: syntax and examples.
  3. The PDF manuals: every command has an entry, with worked examples that use datasets you can load with webuse.
  4. Example data: webuse nlswork, sysuse auto, sysuse census. Try an example on its data first, then adapt.
  5. Statalist and Stack Overflow: search first; when asking, post a short reproducible example (a few commands that run on sysuse auto) and the exact error message.

A small parser to see the pieces

The stand-in below splits a simplified Stata command into its parts the way your eyes do. It is a teaching model: real Stata's parser handles many more cases (weights, using, nested prefixes).

Python Stand-in: split a command into its parts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Change the commands and see which part each word lands in. Try tabulate rep78, missing and list in 1/5: the second one shows what our simplified parser cannot do (in is not handled), which is a good reminder that this is only a model.

Python Stand-in: expand a variable range and a wildcard

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where do options go in a Stata command?

    Options follow one comma, after the varlist and qualifiers.
  2. 2. What does summarize price-weight select?

    A hyphen in a varlist is a range by column order, so check order with describe.
  3. 3. You do not know the command name for a topic. Which is best?

    search finds commands by keyword; help expects an exact name.
  4. 4. What is the difference between = and == ?

    Use generate x = 5 to assign and if x == 5 to compare.

Summary

  • The syntax is [prefix:] command [varlist] [if] [in] [weight] [, options].
  • The comma separates the main command from its options; only one comma is allowed.
  • Varlists accept ranges (a-b) and wildcards (x*, *_id, ?).
  • if selects by condition, in selects by position; == compares, = assigns.
  • help needs a command name, search finds topics; the manuals have examples.

Next: A Tour of the Interface