stata:encode

//原数据状态
des

// Contains data from D:\Stata17\ado\base/a/auto.dta
//  Observations:            74                  1978 automobile data
//     Variables:            12                  13 Apr 2020 17:45
//                                               (_dta has notes)
// --------------------------------------------------------------------------------
// Variable      Storage   Display    Value
//     name         type    format    label      Variable label
// --------------------------------------------------------------------------------
// make            str18   %-18s                 Make and model
// price           int     %8.0gc                Price
// mpg             int     %8.0g                 Mileage (mpg)
// rep78           int     %8.0g                 Repair record 1978
// headroom        float   %6.1f                 Headroom (in.)
// trunk           int     %8.0g                 Trunk space (cu. ft.)
// weight          int     %8.0gc                Weight (lbs.)
// length          int     %8.0g                 Length (in.)
// turn            int     %8.0g                 Turn circle (ft.)
// displacement    int     %8.0g                 Displacement (cu. in.)
// gear_ratio      float   %6.2f                 Gear ratio
// foreign         byte    %8.0g      origin     Car origin
// --------------------------------------------------------------------------------
// Sorted by: foreign


list make in 1/5
//
//      +---------------+
//      | make          |
//      |---------------|
//   1. | AMC Concord   |
//   2. | AMC Pacer     |
//   3. | AMC Spirit    |
//   4. | Buick Century |
//   5. | Buick Electra |
//      +---------------+

//encode 命令可以将字符型变量转换为数值型变量。具体地,将字符变量的不同取值,按照字母排列顺序分别映射到数值 1, 2,...,并为生成的数字变量添加值标签。
encode make ,gen(numvar)
//结果体现:
list numvar in 1/5,nolabel
//
//      +--------+
//      | numvar |
//      |--------|
//   1. |      1 |
//   2. |      2 |
//   3. |      3 |
//   4. |      7 |
//   5. |      8 |
//      +--------+
list numvar in 1/5
//
//      +---------------+
//      |        numvar |
//      |---------------|
//   1. |   AMC Concord |
//   2. |     AMC Pacer |
//   3. |    AMC Spirit |
//   4. | Buick Century |
//   5. | Buick Electra |
//      +---------------+

 

posted @ 2023-02-26 16:16  myrj  阅读(180)  评论(0编辑  收藏  举报