Stata 使用变量名将数字数据读取为字符串

Stata read numeric data as string using variable names

我正在使用

将 csv 文件读入 Stata
import delimited "../data_clean/winter20.csv", encoding(UTF-8) 

原始数据如下:

y             id1
-.7709586   000000000020
-.4195721   000000003969
-.8932499   300000000021
-1.256116   200000007153
-.7858037   000000000000

导入的数据变为:

y             id1
-.7709586   20
-.4195721   000000003969
-.8932499   300000000021
-1.256116   200000007153
-.7858037   0

但是,有些 ID 列被读取为数字。我想将它们导入为字符串。我想完全按照原始数据的样子读取数据。

网上查到的方法是:

import delimited "/Users/tianwang/Dropbox/Construction/data_clean/winter20.csv", encoding(UTF-8) stringcols(74 97 116) clear 

但是,原始数据可能会更新,列号可能会发生变化。以下

import delimited "/Users/tianwang/Dropbox/Construction/data_clean/winter20.csv", encoding(UTF-8) stringcols(id1 id2 id3) clear 

给出错误 id1: invalid numlist in stringcols() option。有没有办法指定变量名而不是列号?

原因是如果我将 ID 读取为数字,则前导零丢失。方法tostring 不恢复前导零。 format id1 %09.0f 仅当变量的位数相同时才有效。

我认为应该这样做。

import delimited "../data_clean/winter20.csv", stringcols(_all) encoding(UTF-8)  clear 

PS:在 Stata16/Win10

中测试