conditional if and programming if in Stata

conditional-statements, if-statement, stata

Solution

You are seeing the expected behavior.

sysuse auto
. list in 1

     +------------------------------------------------------------------------------------------+
  1. | make        | price | mpg | rep78 | headroom | trunk | weight | length | turn | displa~t |
     | AMC Concord | 4,099 |  22 |     3 |      2.5 |    11 |  2,930 |    186 |   40 |      121 |
     |------------------------------------------------------------------------------------------|
     |                  gear_r~o                  |                   foreign                   |
     |                      3.58                  |                  Domestic                   |
     +------------------------------------------------------------------------------------------+

So the first price observation is $4,099. When you run a "conditional" if like `sum price if price > 4499`, Stata finds the observations for which `price` exceeds 4,499 and then runs the `summarize` command on those observations. There are 48 such observations.

When you do a "programming if", the execution is:

- Stata reaches the `if` statement and decides whether the condition is satisfied.

- If the condition is satisfied, it enters the if block and executes the code.

- If the condition is not satisfied, Stata skips past the closing `}` and ignores the `if` code.

So when you do `if price > 4000 { ... }`, Stata looks at the first observation, sees that the price is greater than 4,000 and proceeds to execute the code. Since the `summarize` within the `if` block has no condition on it, the command is executed with all observations. When you do `if price > 5000 { ... }`, Stata sees that the condition is not satisfied and skips the code within `{ ... }`.

The difference between the `if` qualifier and the `if` statement is explained by StataCorp in their FAQs.

Problem

I am trying to understand the difference between programming `if` and conditional `if` in Stata. Here is what I am doing. ``` sysuse auto,clear #conditional if sum price if price>4499 Variable | Obs Mean Std. Dev. Min Max -------------+-------------------------------------------------------- price | 48 7312.813 3102.784 4504 15906 # programming if if price>3291{ sum price } Variable | Obs Mean Std. Dev. Min Max -------------+-------------------------------------------------------- price | 74 6165.257 2949.496 3291 15906 # programming if if price>5000{ sum price } This doesn't give me anything #programming if if price>4000{ sum price} Variable | Obs Mean Std. Dev. Min Max -------------+-------------------------------------------------------- price | 74 6165.257 2949.496 3291 15906 ``` I was wondering why programming `if` gives output for 3291 and 4000 but not 5000. I can understand that programming `if` looks at the first observation in price and then see if it is greater than specified number and then execute the program. But, this is clearly not what I am seeing here. Any help in this regard will be highly appreciated.

Original source