如何根据多个变量创建虚拟对象

时间:2017-07-22 15:07:03

标签: stata

以下命令可以生成虚拟变量:

tabulate age, generate(I)

然而,当我想要一个基于多个变量的假人时,我该怎么办?

例如,我想简明扼要地说:

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

我已经尝试过这个:

tabulate age year, generate(I)

然而,它没有用。

1 个答案:

答案 0 :(得分:6)

您可以按照以下方式获得想要的东西:

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 |
     +---------------------------------------+