If (Conditional Commands)

All Stata commands (or nearly) can be accompanied by an if clause.

In most other statistical software I know (admittedly, this is not much more than a handful), if clauses are important for creating or changing data. For instance, you may wish to create types of household. So, a household will belong to the category of "single mothers" IF one single female parent and one or more children are present. A household will belong to the category of "unmarried heterosexual, without children" IF there are two adults of different sex (who should have declared each other as partners, because otherwise they might just share a flat for the fun of it) and there are no children. And so on. Perhaps you will find some examples at a later stage in the "data transformation" section.

With Stata, you may also do statistical analyses IF certain conditions are given. For instance, you might wish to do regress income on education for single mothers only. Let's assume that single mothers are coded as "1" in variable "household". Now you will write:

regress income education if household==1, beta

As you can see, the if clause is placed at the end of the command, before the option(s).

If clauses can be more complicated than that. Most notably, you may combine several conditions with "and" or "or". For instance, the same analysis as before, but restricted to mothers with more than one child, is produced by:

regress income education if household==1 & nchildren > 1

Here's a list of the most important operators:

& and
| or
== equal
> greater than
>= greater than or equal
< less than
<= less than or equal
!= not equal

© W. Ludwig-Mayerhofer, Stata Guide | Last update: 14 May 2012