Statalist


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

Re: st: question on cond( )


From   Nick Winter <[email protected]>
To   [email protected]
Subject   Re: st: question on cond( )
Date   Thu, 28 Feb 2008 12:11:17 -0500

It looks to me like the examples in the help for cond() are either incorrect or misleading.

The function cond(condition,a,b,c)

returns -a- if -condition- is true; -b- if -condition- is false, and -c- if -condition- is missing.

Note that last is "the *condition* is missing"; that is, that the statement evaluates to missing. This is *not* the same as some part of -condition- evaluating to missing.

So in the example where condition is "x>2", this condition evaluates to either true or false for all observations, including cases where x=., because the condition ".>2" is true under Stata's handling of missing values.

This seems to make the following statement from the help file wrong: "cond(a>2,"this","that","missing") = "missing" if a > ."

The only way I can think of to trigger the "missing" option would be something like this:

clear
set obs 10
gen x=_n-1 in 1/8
gen z=cond(x,"true","false","missing")
list

+-------------+
| x z |
|-------------|
1. | 0 false |
2. | 1 true |
3. | 2 true |
4. | 3 true |
5. | 4 true |
|-------------|
6. | 5 true |
7. | 6 true |
8. | 7 true |
9. | . missing |
10. | . missing |
+-------------+

But once you are doing a comparison (x>2), that will always evaluate to either "true" or "false" in Stata; never to missing.

--Nick Winter

Visintainer, Paul wrote:

I'm not sure why the "condition" function is not coding z with 2 missing
values. If I'm reading the functions command correctly, z should be
coded as missing:
cond(a>2,"this","that","missing") = "missing" if a > .
cond(a>2,"this","that","missing") = "this" if a > 2 and a < .

Any ideas?

Thanks.

. gen z=cond(x>5,1,0,.)

. list

+-------+
| x z |
|-------|
1. | 1 0 |
2. | 2 0 |
3. | 3 0 |
4. | 4 0 |
5. | 5 0 |
|-------|
6. | 6 1 |
7. | 7 1 |
8. | 8 1 |
9. | . 1 |
10. | . 1 |
+-------+



______________________________________
Paul F. Visintainer, PhD


*
* 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/
--
--------------------------------------------------------------
Nicholas Winter                                 434.924.6994 t
Assistant Professor                             434.924.3359 f
Department of Politics                  [email protected] e
University of Virginia          faculty.virginia.edu/nwinter w
PO Box 400787, 100 Cabell Hall
Charlottesville, VA 22904

*
*   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