Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Simplifying multiple cases of the if command


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Simplifying multiple cases of the if command
Date   Fri, 16 Nov 2012 18:04:57 +0000

It's important to understand that to Stata

... if news == 1 | 3 | 5

can _only_ mean

  if (news == 1) | (3) | (5)

Consider this example:

. sysuse auto
(1978 Automobile Data)

. di rep78[1]
3

. di rep78[1] == 1 | 2
1

If rep78[1] is 3, how can the last result be reported as 1 (meaning
true). It's because Stata parses the argument to -display- as

(rep78[1] == 1)       (false)
or
2                            (true)

Why is 2 true? Because any numeric result not zero is true. See

FAQ     . . . . . . . . . . . . . . . . . . . . . . .  True and false in Stata
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        8/05    What is true and false in Stata?
                http://www.stata.com/support/faqs/data-management/
                true-and-false/

for more if needed.

On Fri, Nov 16, 2012 at 5:55 PM, Nick Cox <njcoxstata@gmail.com> wrote:
> You can go
>
> ... if inlist(news, 1, 3, 5)
>
> That is another way to do it. See also
>
> SJ-6-4  dm0026  . . . . . . Stata tip 39: In a list or out? In a range or out?
>         . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
>         Q4/06   SJ 6(4):593--595                                 (no commands)
>         tip for use of inlist() and inrange()
>
> http://www.stata-journal.com/article.html?article=dm0026
>
> or
>
> http://statadaily.wordpress.com/2011/10/20/functions-inlist/
>
> Nick
>
> On Fri, Nov 16, 2012 at 5:33 PM, sanja_lutzeyer@ncsu.edu
> <slutzey@ncsu.edu> wrote:
>
>> This might be a very easy question, but if I have a number of things
>> that have to hold when generating a variable, is there a way to
>> simplify the following?
>>
>> generate variable if news== 1 | news == 3 | news ==5
>>
>> to something like
>> generate variable if news == 1 | 3 | 5? I tried this
>>
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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