在导入Stata时可能存在中文乱码问题,在stata15中可以通过unicode来解决:
help unicode
按照help文档,核心的转码步骤其实就三步
但是仅按照help文档无法对文档进行批量处理,而下载的数据中大多有多个需要转码的文件。对此,连老师已经写了ua程序来解决。不过ua程序仍然需要两步,为了进一步简化流程,想达到只需要告诉文件夹路径,就转码文件夹下面所有文件的效果,尝试参照ua命令写了如下的程序供大家参考,冒昧叫uaone吧。
使用方法如下:
uaone, d("E:\数据") // 默认转码为gb18030
* 如果需要对特定的.dta文件类型进行编码
uaone, d("E:\数据") f(dta)
* 采用其他编码类型
uaone, d("E:\数据") set(ibm-1392)
源代码如下,由于没有发布,需要大家把代码copy到Stata的"Do-file"里面运行一次,然后就可以正常使用啦。
使用过程中肯定存在很多bug,请多多赐教。。。
cap program drop uaone
program uaone
version 13.0
syntax [anything(everything)], Direction(string asis) [set(string asis) RETRanslate ///
TRansoption(string asis) Filetype(string asis)]
/*
Direction: Direction of the folder containing ".dta" files needing translation.
set: Set an encoding type. Please type "unicode encoding list" for details.
TRansoption: Options of "encoding translate", including "invalid(escape|mark|ignore)"
"transutf8" "nodata" "replace (only when "reranslate" is specified)".
RETRanslate: To do "unicode retranslate"
Filetype: Default is to translate all file types.
*/
preserve
clear
*test
capture cd `direction'
if _rc != 0{
di as error ///
"Unable to load the directions. Please double check the {bf:direction} you entered."
exit 111
}
*Options settings
local currdir `c(pwd)'
local set gb18030
local option
local transchoice "translate"
local file "*"
if "`transoption'"!= ""{
local option "`transoption'"
}
if "`set'"!=""{
local setting "`set'"
}
if `"`retranslate'"'!=`""'{
local transchoice "retranslate"
}
if `"`filetype'"'!=`""'{
local file "*.`filetype'"
}
di in w _dup(35) "="
di "{it: Translate Start}"
di in w _dup(35) "="
di _n(1)
di in w "Files in the following directories"
di in w "are to be analyzed or translated:"
cap ssc install rcd
rcd // get the list of all sub-directories
forvalues i = 1(1)`r(tdirs)'{
local dir`i' `"`r(ndir`i')'"'
}
di _n
di in w _dup(35) "-"
di "translating"
di in w _dup(35) "-"
forvalues i = 1(1)`r(tdirs)'{
qui cd `"`dir`i''"'
local filename: dir . files "`file'",respectcase
if `"`filename'"' != ""{
foreach fn of local filename{
qui unicode encoding set "`set'"
qui unicode `transchoice' `"`fn'"',`option'
di _n
di "■" _s(2) `"`fn' {it:`transchoice' completed}"'
}
}
else{
continue
}
}
di _n(1)
di in w _dup(35) "="
di "{it: Translation Completely}"
di in w _dup(35) "="
restore
qui cd `currdir'
end
网友评论