Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

st: RE: using -cond- to create a indicator

 From "PHAM Ngoc Minh" To Subject st: RE: using -cond- to create a indicator Date Fri, 26 Oct 2012 17:12:01 +0900

```Dear Buis,
Both commands worked perfectly. Thank you so much for your fast responses.

Sincerely,
Minh

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Maarten Buis
Sent: Friday, October 26, 2012 4:38 PM
To: statalist@hsphsun2.harvard.edu
Subject: using -cond- to create a indicator [was: Re: st: intcens: how to
estimate mean and variance after intcens]

On Fri, Oct 26, 2012 at 6:22 AM, PHAM Ngoc Minh wrote:
> I wish to create dummy variables containing missing (.), and thus used
> formula COND(IF, THEN, ELSE) but missing (.) no longer existed. An example
> as follows:
>
> I need to generate (wcgrp) from waist circumference (wc) as 1, 0 or
missing
> (.), then typed:
>
> generate wcgrp = cond(wc>90&wc!=.&sex==1|wc>80&wc!=.&sex==2, 1, 0)
>
> but (wcgrp) had only 1 and 0, and missing was regarded as 0.

What you want is a new variable that contains three possible values
(0, 1, .) but you give it values using a function that can only output
2 values (one for when the expression is true and one for when the
expression is false), so that cannot work. Notice however that
-cond()- is working correctly in the sense that it did exactly what
you told it to do: it gives a 0 when the expression is false and the
expression is false when either the waist circumference is smaller
than some sex specific threshold or when wc is missing.

Below are two possible solutions:

generate wcgrp = cond(wc>90&sex==1|wc>80&sex==2, 1, 0) if !missing(wc)

generate wcgrp = cond(wc>90&sex==1|wc>80&sex==2, cond(missing(wc), ., 1), 0)

Hope this helps,
Maarten

---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany

http://www.maartenbuis.nl
---------------------------------
*
*   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/

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