Stata如何将记录的第一行设为变量名?

下载并读取演示数据:


copy https://www.stata.com/examples/auto.csv auto.csv,replace
import delimited auto, varnames(nonames) clear

正常情况下,Stata会自动判断首行是否为变量,为了演示,我们加了选项varnames(nonames),不让其检测,菜单对应的是First row as variable names→never,这样变量名就被读取为到第一行记录。
查看演示数据:

. list

     +------------------------------------------------+
     |            v1      v2    v3      v4         v5 |
     |------------------------------------------------|
  1. |          make   price   mpg   rep78    foreign |
  2. |   AMC Concord    4099    22       3   Domestic |
  3. |     AMC Pacer    4749    17       3   Domestic |
  4. |    AMC Spirit    3799    22           Domestic |
  5. | Buick Century    4816    20       3   Domestic |
     |------------------------------------------------|
  6. | Buick Electra    7827    15       4   Domestic |
  7. | Buick LeSabre    5788    18       3   Domestic |
  8. |    Buick Opel    4453    26           Domestic |
  9. |   Buick Regal    5189    20       3   Domestic |
 10. | Buick Riviera   10372    16       3   Domestic |
     |------------------------------------------------|
 11. | Buick Skylark    4082    19       3   Domestic |
     +------------------------------------------------+

美国伊利诺大学吉斯商学院(Gies College of Business, University of Illinois)的副教授Julian Reif 博士开发了Stata社区命令autorename,autorename renames variables using a row of data. This can be useful when reading oddly formatted datasets.

安装Stata社区命令autorename:


ssc install autorename, replace
help autorename

然后,运行autorename命令,并查看结果:


. autorename
(1 observation deleted)

. list
     +------------------------------------------------+
     |          make   price   mpg   rep78    foreign |
     |------------------------------------------------|
  1. |   AMC Concord    4099    22       3   Domestic |
  2. |     AMC Pacer    4749    17       3   Domestic |
  3. |    AMC Spirit    3799    22       .   Domestic |
  4. | Buick Century    4816    20       3   Domestic |
  5. | Buick Electra    7827    15       4   Domestic |
     |------------------------------------------------|
  6. | Buick LeSabre    5788    18       3   Domestic |
  7. |    Buick Opel    4453    26       .   Domestic |
  8. |   Buick Regal    5189    20       3   Domestic |
  9. | Buick Riviera   10372    16       3   Domestic |
 10. | Buick Skylark    4082    19       3   Domestic |
     +------------------------------------------------+

完,本文内容交流请移步:http://www.epiman.cn/thread-153241-1-1.html