1

有没有办法用更简洁的语法来表达这一点

if (`avgInc' > 0) {
    loc avgIncDec = "increased"
}
else {
    loc acgIncDec = "decreased"
}

类似 InlineIf (iif) 的东西:

loc avgInc = iif((`avgInc' > 0), "increased", "decreased")
4

2 回答 2

2

尝试cond

var = if cond(a="value","1","another value")

if a="value" var ="1" else var = "另一个值"

还有其他三元运算符:inrange() inlist()

于 2013-02-10T17:28:10.993 回答
1

以前的答案有帮助,cond()但包含一些小错误。

   loc avgIncDec = cond(`avginc' > 0, "increased", "decreased")

请注意,Stata 与许多其他语言一样,使用=赋值和==测试相等性。

于 2013-02-10T20:51:41.887 回答