The general scheme for writing commands will be something like this
(see help language
, this is a pared down version):
[prefix:] command [varlist] [if] [, options]
For example, try:
help summarize
and you will see
summarize [varlist] [if] [in] [weight] [, options]
The name of the command is summarize. It can be abbreviated as
short as su (although sum is more common in practice). This is
indicated by the underlining.
This command takes (“accepts”) a variable list. The square brackets
indicate that the variable list is actually optional for this command,
as is everything besides the command name, summarize.
Having loaded a data set, we can try out this command
. summarize
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
make | 0
price | 74 6165.257 2949.496 3291 15906
mpg | 74 21.2973 5.785503 12 41
rep78 | 69 3.405797 .9899323 1 5
headroom | 74 2.993243 .8459948 1.5 5
-------------+---------------------------------------------------------
trunk | 74 13.75676 4.277404 5 23
weight | 74 3019.459 777.1936 1760 4840
length | 74 187.9324 22.26634 142 233
turn | 74 39.64865 4.399354 31 51
displacement | 74 197.2973 91.83722 79 425
-------------+---------------------------------------------------------
gear_ratio | 74 3.014865 .4562871 2.19 3.89
foreign | 74 .2972973 .4601885 0 1
Next we add a variable list. There are a number of ways we can
write lists of variables in Stata. (See help varlist
.)
_all
. summarize price mpg // spelled-out list
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
price | 74 6165.257 2949.496 3291 15906
mpg | 74 21.2973 5.785503 12 41
. summarize mpg - trunk // first-to-last list
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
mpg | 74 21.2973 5.785503 12 41
rep78 | 69 3.405797 .9899323 1 5
headroom | 74 2.993243 .8459948 1.5 5
trunk | 74 13.75676 4.277404 5 23
. summarize *n // wildcard list
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
turn | 74 39.64865 4.399354 31 51
foreign | 74 .2972973 .4601885 0 1
. summarize _all // or use " * "
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
make | 0
price | 74 6165.257 2949.496 3291 15906
mpg | 74 21.2973 5.785503 12 41
rep78 | 69 3.405797 .9899323 1 5
headroom | 74 2.993243 .8459948 1.5 5
-------------+---------------------------------------------------------
trunk | 74 13.75676 4.277404 5 23
weight | 74 3019.459 777.1936 1760 4840
length | 74 187.9324 22.26634 142 233
turn | 74 39.64865 4.399354 31 51
displacement | 74 197.2973 91.83722 79 425
-------------+---------------------------------------------------------
gear_ratio | 74 3.014865 .4562871 2.19 3.89
foreign | 74 .2972973 .4601885 0 1
Some languages have indexed lists, like myvar1-myvar4, meaning the
four variables myvar1, myvar2, myvar3, and myvar4, regardless of where
they are located in the data set. Not Stata. In Stata this would be
a first-to-last (positional) list. It might be the list you want, or
it might not - it depends how the data set was constructed.
How else could you specify the list with myvar1, myvar2, myvar3, and
myvar4? (State any assumptions you make.)
An if
condition allows your command to act on a subset of the
observations in your data set. (See help if
.) The syntax for
most commands can be
command if expression
where expression is something that is true or false for each observation
in the data set.
For example, to see the average price of a foreign car in this
data set we might try
. summarize price if foreign==1
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
price | 22 6384.682 2621.915 3748 12990