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

Re: st: RE: re: cond(a,b,c) fn


From   James Cui <[email protected]>
To   [email protected]
Subject   Re: st: RE: re: cond(a,b,c) fn
Date   Fri, 15 Apr 2005 14:01:43 +1000

Thanks for the input. You are right "x" is an expression, not a variable. Good judgment comes from good experience and good experience comes from bad experience!

James.

Email: [email protected]



Scott Merryman wrote:

A couple other ways:

gen a = cond(rep78==1, 1, 0 ) if rep !=.


gen a = cond(rep, cond(rep ==1, 1, 0), 0, .)

Scott


-----Original Message-----
From: [email protected] [mailto:owner-
[email protected]] On Behalf Of Kit Baum
Sent: Thursday, April 14, 2005 9:43 PM
To: [email protected]
Subject: st: re: cond(a,b,c) fn

James writes

When I use the cond(x,a,b,c) function, I could not generate the value c
when x is missing. For example:

sysuse auto, clear
tab rep78, mi
gen a=cond(rep78==1, 1, 0, .)
tab a, mi

It gives

a	Freq.	Percent	Cum.

0	72	97.30	97.30
1	2	2.70	100.00

Total	74	100.00

There should be 5 missing values for variable a according to the manual.
Thanks for your input.



The online help says

 cond(x,a,b,c) returns a if x evaluates to true (not 0), b if x
evaluates to false
        (0), and c if x evaluates to missing (.).

Since "rep78==1" never evaluates to missing, a will never be set to
missing.
A way to achieve this is

. g a = (rep78==1)*rep78/rep78

. tab a,mi

          a |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |         67       90.54       90.54
          1 |          2        2.70       93.24
          . |          5        6.76      100.00
------------+-----------------------------------
      Total |         74      100.00

Kit Baum, Boston College Economics
http://ideas.repec.org/e/pba1.html

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


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




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