1
votes

Is there a way to express this with a terser syntax

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

Something like an InlineIf (iif):

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

2 Answers

2
votes

Try cond

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

if a="value" var ="1" else var = "another value"

There are other ternary operators, as well: inrange() inlist()

1
votes

Previous answer focuses helpfully on cond() but contains some minor errors.

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

Note that Stata, like many other languages, uses = for assignment and == to test for equality.