运行help encode,你可以阅读:
如果 varname 包含恰好存储的数字,则不要使用 encode
作为字符串;改为使用generate newvar = real(varname) 或destring;
参见 real() 或 [D] destring。
例如:
clear all
set more off
input id str5 age
1 "32"
2 "14"
3 "65"
4 "54"
5 "98"
end
list
encode age, gen(age2)
destring age, gen(age3)
list, nolabel
注意使用encode 和destring 之间的区别。前者将数字代码 (1, 2, 3, ...) 分配给字符串值,而destring 将字符串值转换为数字。当您list 时,您会看到剥离值标签:
. list, nolabel
+------------------------+
| id age age3 age2 |
|------------------------|
1. | 1 32 32 2 |
2. | 2 14 14 1 |
3. | 3 65 65 4 |
4. | 4 54 54 3 |
5. | 5 98 98 5 |
+------------------------+
一个简单的list 或browse 可能会让您感到困惑,因为encode 分配了自然数序列,但也分配了等于原始字符串的值标签:
. list
+------------------------+
| id age age3 age2 |
|------------------------|
1. | 1 32 32 32 |
2. | 2 14 14 14 |
3. | 3 65 65 65 |
4. | 4 54 54 54 |
5. | 5 98 98 98 |
+------------------------+
nolabel 选项显示“基础”数据。
你提到它不一致,但对于未来的问题,发布准确的输入和结果对于那些试图帮助你的人更有用。