Reputation: 111
Say I have a dataset with three variables a
, b
, c
, and having 5 observations. Like the following:
a b c
1 0 1
1 1 1
0 1 0
0 1 1
1 0 0
Now I want to generate a new variable called type
, which is a possible combination of variable a
, b
and c
. Specifically,
type=1
if a=b=c=0
type=2
if a=c=0 & b=1
type=3
if a=b=0 & c=1
type=4
if a=0 & b=c=1
type=5
if a=1 & b=c=0
type=6
if a=b=1 & c=0
type=7
if a=c=1 & b=0
type=8
if a=b=c=1
The new dataset I want to get is:
a b c type
1 0 1 7
1 1 1 8
0 1 0 2
0 1 1 4
1 0 0 5
Are there any general ways to realize this in Stata? It's better if this can also be extended when type
is large, say 100 types. Thx a lot.
Upvotes: 1
Views: 1895
Reputation: 4011
If the specific values of type
don't matter, egen
's group
function works.
E.g.:
clear
input a b c
1 0 1
1 1 1
0 1 0
0 1 1
1 0 0
0 1 0
1 1 1
end
sort a b c // not necessary, but clearer presentation
egen type = group(a b c)
li
with the result
+------------------+
| a b c type |
|------------------|
1. | 0 1 0 1 |
2. | 0 1 0 1 |
3. | 0 1 1 2 |
4. | 1 0 0 3 |
5. | 1 0 1 4 |
|------------------|
6. | 1 1 1 5 |
7. | 1 1 1 5 |
+------------------+
Upvotes: 3