conditional if and programming if in Stata

12,060

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:

  1. Stata reaches the if statement and decides whether the condition is satisfied.
  2. If the condition is satisfied, it enters the if block and executes the code.
  3. 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.

Share:
12,060
Metrics
Author by

Metrics

silver r badge: #124 Uwe's Maxim: Computers are cheap, and thinking hurts. library(fortunes) fortune("dog") Firstly, don't call your matrix 'matrix'. Would you call your dog 'dog'? Anyway, it might clash with the function 'matrix'. -- Barry Rowlingson R-help (October 2004)

Updated on June 27, 2022

Comments

  • Metrics
    Metrics almost 2 years

    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.