使用类别0-5创建变量

时间:2018-05-16 22:51:14

标签: stata

我使用a previous question of mine的建议创建了一个数值变量pets

generate pets = . 
mata: st_store(., "pets", rdiscrete(1000, 1, (0.24, 0.36, 0.16, 0.12, 0.1, 0.02)))

但是,类别的值范围为1到6。

是否可以将此范围更改为0到5?

1 个答案:

答案 0 :(得分:2)

创建pets变量后,您只需要recode

recode pets (1 = 0 Cat) (2 = 1 Dog) (3 = 2 Fish) (4 = 3 Hamster) (5 = 4 Rabbit) ///
            (6 = 5 Snake), prefix(new_) label(newpetslabel) 

tab new_pets, nolabel

  RECODE of |
       pets |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |        253       25.30       25.30
          1 |        348       34.80       60.10
          2 |        167       16.70       76.80
          3 |        119       11.90       88.70
          4 |         97        9.70       98.40
          5 |         16        1.60      100.00
------------+-----------------------------------
      Total |      1,000      100.00

tab new_pets

  RECODE of |
       pets |      Freq.     Percent        Cum.
------------+-----------------------------------
        Cat |        253       25.30       25.30
        Dog |        348       34.80       60.10
       Fish |        167       16.70       76.80
    Hamster |        119       11.90       88.70
     Rabbit |         97        9.70       98.40
      Snake |         16        1.60      100.00
------------+-----------------------------------
      Total |      1,000      100.00

选项prefix告诉Stata生成变量new_pets,相应的标签保存在名为value label的新newpetslabel中。

从Stata的命令提示符输入help recode以获取更多信息。