Chen
Chen

Reputation: 111

Use Stata to generate new variable, based on combination types of variables

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,

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

Answers (1)

Brendan
Brendan

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

Related Questions