Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: if then else


From   Christopher F Baum <baum@bc.edu>
To   statalist@hsphsun2.harvard.edu
Subject   st: Re: if then else
Date   Thu, 18 Dec 2003 07:51:56 -0500

On Dec 18, 2003, at 2:33 AM, Richard wrote:

And for an obtuse solution:


. g c1=3*(x1==1 & x2==3)

. g c2=4*(x3==2 & x4==17)

. g y= cond(max(c1,c2),max(c1,c2),5)

. list x1-x4 y

     +-----------------------+
     | x1   x2   x3   x4   y |
     |-----------------------|
  1. |  1    3    0    0   3 |
  2. |  0    0    2   17   4 |
  3. |  1    2    3    4   5 |
  4. |  1    3    2   17   4 |
  5. |  1    2    3    4   5 |
     +-----------------------+
No, something is wrong there; on case 4, my SPSS code would result in y =
3, not y = 4 like you have it.
Richard has indicated that SPSS "short-circuits" on an IF/THEN/ELSE, breaking out of the construct whenever one of the conditions is satisfied. Some programming languages do that, and some do not. All the more reason to have some very explicit logic (that does not depend on the rules that a specific piece of software follows). My solution was a literal translation of his evaluating several conditions in turn, but without the understanding of how his software will deal with multiple satisfied conditions. If code is to be portable and readable, that understanding becomes part of the documented scheme.

Of course many unfamiliar with Stata have argued that its language is not partiicularly readable, at least at first glance: a recent Swedish visitor said "what's with all of those backticks"? I told him the funny story of my terminal emulator which remapped backtick to tick, and hours of frustration trying to change a few lines of code in Unix Stata.

Kit

*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/




© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index