一次性解决Stata中文乱码问题

在导入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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这部分是对Stata编程的简单介绍。主要讨论宏和循环,并展示如何编写简单程序。编程是一个很大的主题,我在这里仅进行...
    谢作翰阅读 14,799评论 0 13
  • kyle0x54阅读 4,676评论 0 0
  • 微信那边的你 夜夜都在我梦里 从一个微笑认识你 从一次长谈了解你 从方寸的屏幕看到你 从圆月般的笑容记住你 从某一...
    田园听雨阅读 1,566评论 0 0
  • 当你感觉累的时候,恰恰是你状态最差的时候,所以让自己舒适吧。 舍掉自我,勇于坚持,付出永远比回报更让人欣喜,因为没...
    木青灯阅读 708评论 0 1