Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: generate variable


From   Phil Schumm <[email protected]>
To   [email protected]
Subject   Re: st: generate variable
Date   Sat, 27 May 2006 19:51:23 -0500

On May 27, 2006, at 1:05 PM, peter harper wrote:
Thanks Nick. I tried several things given below, but I still cannot get "18825 changes made" in total, they are the people without work limiting disability. Can I please ask what precisely needs to be issued to get the "18825 or 18826 changes made" please.

ge disabledworklim=.
(22451 missing values generated)
. replace disabledworklim=1 if e6a==1&e6b==1
(997 real changes made)
. replace disabledworklim=2 if (e6a==2)&(e6a==1-e6b==1)
(17174 real changes made)
. replace disabledworklim=2 if (e6a==1&2)-(e6b==1)
(1700 real changes made)
. replace disabledworklim=2 if (e6a==1&e6a==2)&-(e6b==1)
(0 real changes made)
. replace disabledworklim=2 if (e6a==1&e6a==2)-(e6b==1)
(997 real changes made)
. replace disabledworklim=2 if (e6a<=e6a==1)&(e6a==1<=e6a==2)
(0 real changes made)
. replace disabledworklim=2 if (e6b<=e6a==1)&(e6a==1<=e6a==2)
(0 real changes made)

Take Nick's advice, and use parentheses to make your meaning clear. For example, consider your expression:


(e6a==1&e6a==2)&-(e6b==1)


You have three operators here (in order of precedence, high to low): -, ==, and &. This means your expression is equivalent to


( (e6a==1) & (e6a==2) ) & ( - (e6b==1) )
-------- -------- --------------
a b c
-----------------------
d


Now, a will equal 1 if e6a equals 1 (or 0 otherwise), b will equal 1 if e6a equals 2 (or 0 otherwise), and c will equal -1 if e6b equals 1 (or 0 otherwise). Moving outward, d will equal 1 if a and b are both non-zero; this obviously cannot happen, so d will automatically equal 0. Finally, the entire expression will be true (i.e., non-zero) only if both d and c are non-zero, and since d is automatically 0, the entire expression will always be false.

Similarly, consider your expression:


(e6b<=e6a==1)&(e6a==1<=e6a==2)


Here, the operators (high to low precedence) are: <=, ==, and &. This means that your expression is equivalent to


( (e6b<=e6a) == 1) & ( ( e6a == (1<=e6a) ) == 2 )
---------- --------
a b
------------------ -------------------
c d
----------------------------
e


Starting with the inner-most evaluations, a will equal 1 if e6b is less than or equal to e6a (or 0 otherwise), and b will equal 1 if e6a is greater than or equal to 1 (including missing). As a result, c will equal 1 if e6b is less than or equal to e6a (or 0 otherwise), and d will equal 1 if e6a equals b, which, by the way, can only happen if e6a equals either 1 or 0. Moving further outward, e will equal 1 only if d equals 2, which, since d itself is a logical expression and will therefore only ever be 0 or 1, can never happen. Thus, just like before, the entire expression will always be false.

If you have questions about the above, look carefully at the sections in the Stata manual ([U] I believe) on operators and expressions; you should find what you need there. In addition, be very careful when using the comparison operators with variables that can have missing values. The expression


x <= y


will always be true if y is missing, regardless of what x is.


-- Phil

*
* 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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index