júní 21, 2011

General command syntax

Most of the Stata commands can be shortened. For example, instead of typing summarize, Stata will also accept gen. The help screen demonstrates for each command
how it can be abbreviated, by showing underlined letters in the syntax section of the help.

Stata syntax follows mostly the following basic structure:

Syntax:

[by varlist1:] command [varlist2] [=exp] [if] [in] [using filename] [, options]

where square brackets shows optional qualifiers.

example:

bysort gender: tabulate age if weight < 50, nolabel

A variable list (varlist) is a list of variable names with blanks in between. There are a number of shorthand conventions to reduce the amount of typing. For instance:
myvar                                          Just one variable
myvar var1 var2            Three variables
myvar*                                       All variables starting with myvar
*var                                             All variables ending with var
my*var                                      All variables starting with my and ending with var
my~var                                      A single variable starting with my and ending with var
my?var                                     All variables starting with my and ending with var with one other character between
myvar1-myvar6                     myvar1, myvar2, ..., myvar6 (probably)
this-that                                  All variables in the order of the variables window this through that
The * character indicates to match one or more characters. All variables matching the pattern are returned. The ~ character also indicates to match one or more characters, but unlike *, only one variable is allowed to match. If more than one variable match, an error message is returned. The ? character matches a single character. All variables matching the pattern are returned. The - character indicates that all variables in the dataset, starting with the variable to the left of the - and ending with the variable to the right of the – are to be returned. Any command that takes varlist understands the keyword _all to mean all variables. Some commands are using all variables by default if none are specified (e.g., summarize shows summary statistics for all variables, and is equivalent to summarize _all).

júní 20, 2011

Using by/if/in

Now we have used the previous commands on the data set without specific or orderly sequence.

Suppose you want to describe, browse, or even summarize a group of variables in the data set? Did I really intend to ask you this or should I be answering this?

Its possible to analyze just a sample or portion of data set by use of the following conditional options

  • IF
  • IN
  • BY

IF

Executes a command when the set condition is met

Syntax: command  [variable(s)] if (condition)

example: 1) summarize age if (age>10)

  2)list make price if foreign==1

  3)list make price if price > 10000 & price <.

Explanation: 1) will only execute the command summarize on variable age on only the occurrences where age is greater than ten.

              2) Will list prices of only foreign cars .(to load this data, enter sysuse auto in the command window)

             3) Will list only prices greater than 10000 and value is not missing

Other possible operators include less than(<), less than or equal to(<=), greater than or equal(>=), not equal to(!=), if missing

Now that I have mentioned “missing”, let me tell you something about “missing”. If a variable of type numeric has no value, Stata uses dot(.) to show this. If the variable was of type string, it is shown by a blank(“ “).

IN

Used to specify  a range

Syntax: command summarize [variable] in 1/20

This will display a summary of variable 1 through to 20

BY

Primarily used to sort data

As a prefix:

When a command is prefixed with a bylist, it is performed repeatedly for each element of the variable or variables in that list, each of which must be categorical. For instance,
by foreign: summ price
will provide descr iptive statistics for both foreign and domestic cars. If the data are not already sorted by the bylist variables, the prefix bysort should be used. The option ,total will add the overall summar y.

Examples:

bysort rep78: summ price
bysort rep78 foreign: summ price

As a suffix:

summarize price, by(foreign)