Empower “and” and “or” in IF statement in Stata

Stata is a little bit awkward when using and and or in if statement, compared to SAS. For example:

In SAS, we can write if 2001 <= fyear <= 2010. But in Stata, we usually write: if fyear >= 2001 & fyear <= 2010.

In fact, Stata provides a handy inrange function. The above if statement can be written as: if inrange(fyear, 2001, 2010).

Similarly, Stata provides another inlist function. The syntax is inlist(z, a, b, ...), which returns 1 if z = a or z = b … In if statement, it is equivalent to if z = a | z = b | ...

This entry was posted in Stata. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *