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

# Re: st: Two dichotomous variables to One Categorical

 From Maarten Buis 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/
```