Bookmark and Share

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


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

Re: st: Two dichotomous variables to One Categorical


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Two dichotomous variables to One Categorical
Date   Fri, 12 Oct 2012 09:40:35 +0100

Picking up on Maarten's last line

egen group = group(var1 var2), label

has the advantages that the variable is labelled as easily as possible
and that the idea is easy to extend to more variables.

For a longer discussion, see

SJ-7-4  dm0034  . . . Stata tip 52: Generating composite categorical variables
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q4/07   SJ 7(4):582--583                                 (no commands)
        tip on how to generate categorical variables using
        tostring and egen, group()

which is directly accessible at

http://www.stata-journal.com/sjpdf.html?articlenum=dm0034

I am a big fan of -cond()-, but I think this method is easier for beginners.

On Fri, Oct 12, 2012 at 9:22 AM, Maarten Buis <[email protected]> wrote:
> On Fri, Oct 12, 2012 at 10:07 AM, malay biswas wrote:
>> I am trying to transform my two dichotomus variables (Present/Absent,
>> Present/Absent) into one categorical variable depending upon four
>> configurations(Present Present =1, Present Absent=2, Absent Present=3,
>> Absent Absent=4).
>
> There are several solutions possible, the one I currently use most is.
>
> gen newvar = cond(var1==1, ///
>     cond(var2==1, 1, 2), ///
>     cond(var2==1, 3, 4)  ) ///
>     if !missing(var1,var2)
>
> The idea is that the -cond()- function contains three parts:
> cond(expression, what happens when expression is true, what happens
> when expression is false). So the first cond() separates between var1
> == 1 and var1 != 1; the second cond() separates var2 when var1 == 1;
> the third cond() separates var2 when var1 != 1, and finally the
> !missing(var1,var2) part takes care of missing values in var1 and
> var2.
>
> You can also look at -egen group-.
>
> 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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index