I came across a data set with some variables in it that recorded actual measurements, so their values could be zero or higher. Then a need arose to quickly look up how many of these variables would have recorded anything -- in effect, separating the yes/no part from the how much -- and adding the result. I essentially needed to treat continuous variables as dummies for this one purpose and then move on. Turns out there's a very easy way to do that. Consider a data set that looked something like this (sorry for the ugliness of tables in WordPress posts; there's probably a plugin for that somewhere):


_var1 _var2 _var3
1.5 0 2.7
0 1.2 0
0 1 2


Now suppose you wanted to generate a fourth variable that was equal to the sum of the non-zero instances of the first three. You would do this:

gen _var4=(_var1!=0)+(_var2!=0)+(_var3!=0)

Trivial? Yes. Obvious? I dunno. I'm sure you use logical operators all the time inside compounded logical expressions. I thought it was kind of neat and post-worthy that you could also use them inside arithmetic expressions.