1

The following command can generate dummy variables:

tabulate age, generate(I)

Nevertheless, when I want a dummy based on multiple variables, what should I do?

For example, I would like to do the following concisely:

generate I1=1 if age==1 & year==2000
generate I2=1 if age==1 & year==2001
generate I3=1 if age==2 & year==2000
generate I4=1 if age==2 & year==2001

I have already tried this:

tabulate age year, generate(I)

However, it did not work.

0

1 Answer 1

3

You can get what you want as follows:

sysuse auto, clear
keep if !missing(rep78)

egen rf = group(rep78 foreign)
tabulate rf, generate(I)

group(rep78 |
   foreign) |      Freq.     Percent        Cum.
------------+-----------------------------------
          1 |          2        2.90        2.90
          2 |          8       11.59       14.49
          3 |         27       39.13       53.62
          4 |          3        4.35       57.97
          5 |          9       13.04       71.01
          6 |          9       13.04       84.06
          7 |          2        2.90       86.96
          8 |          9       13.04      100.00
------------+-----------------------------------
      Total |         69      100.00

list I* in 1 / 10

     +---------------------------------------+
     | I1   I2   I3   I4   I5   I6   I7   I8 |
     |---------------------------------------|
  1. |  0    0    1    0    0    0    0    0 |
  2. |  0    0    1    0    0    0    0    0 |
  3. |  0    0    1    0    0    0    0    0 |
  4. |  0    0    0    0    1    0    0    0 |
  5. |  0    0    1    0    0    0    0    0 |
  6. |  0    0    1    0    0    0    0    0 |
  7. |  0    0    1    0    0    0    0    0 |
  8. |  0    0    1    0    0    0    0    0 |
  9. |  0    0    1    0    0    0    0    0 |
 10. |  0    1    0    0    0    0    0    0 |
     +---------------------------------------+
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.