简体   繁体   English

条件if和编程如果在Stata中

[英]conditional if and programming if in Stata

I am trying to understand the difference between programming if and conditional if in Stata. 我试图理解编程if和条件if在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. 我想知道为什么编程if给出3291和4000但不是5000的输出。我可以理解编程if查看价格中的第一个观察,然后看它是否大于指定的数字然后执行程序。 But, this is clearly not what I am seeing here. 但是,这显然不是我在这里看到的。 Any help in this regard will be highly appreciated. 在这方面的任何帮助将受到高度赞赏。

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. 所以第一次价格观察是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. 当你运行“条件”时, sum price if price > 4499 ,那么Stata会找到price超过4,499的观察值,然后对这些观察值运行summarize命令。 There are 48 such observations. 有48个这样的观察。

When you do a "programming if", the execution is: 当你执行“编程if”时,执行是:

  1. Stata reaches the if statement and decides whether the condition is satisfied. Stata到达if语句并确定条件是否满足。
  2. If the condition is satisfied, it enters the if block and executes the code. 如果满足条件,则进入if块并执行代码。
  3. If the condition is not satisfied, Stata skips past the closing } and ignores the if code. 如果条件不满足,Stata会跳过结束}并忽略if代码。

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. 因此, if price > 4000 { ... } ,Stata会查看第一个观察结果,看到价格大于4,000并继续执行代码。 Since the summarize within the if block has no condition on it, the command is executed with all observations. 由于if块中的summarize没有条件,因此执行所有观察的命令。 When you do if price > 5000 { ... } , Stata sees that the condition is not satisfied and skips the code within { ... } . if price > 5000 { ... } ,Stata会看到条件不满足并跳过{ ... }内的代码。

The difference between the if qualifier and the if statement is explained by StataCorp in their FAQs . stataCorp在其常见问题解答中解释if限定符和if语句之间的区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM