Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: Two dichotomous variables to One Categorical


From   Maarten Buis <maartenlbuis@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Two dichotomous variables to One Categorical
Date   Fri, 12 Oct 2012 10:22:54 +0200

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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index