今天偶然遇到了一个euc-cn格式的数据集,在对数据集进行处理的时候报错,错误信息如下:
ERROR: Some character data was lost during transcoding in the data set LIB.DS3. Either the data contains characters that are not representable in the new encoding or truncation occurred during transcoding.
在网上寻求帮助的同时看到了这篇公众号如何在SAS的三种编码间来去自如:wlatin1,euc-cn和utf-8
这里面详细介绍了各种编码之间的区别,里面提到了一种批量解决的方法,即:将sas文件导入(infile+input),再导出成新编码下的.sas文件(file+put)。
我在这里想把这种方法具体化一下~详情也可参考如下链接15597 - How to convert SAS® data set encoding
转换整个逻辑库中数据集的编码
/*转换为UTF-8的编码类型*/
libname inlib cvp 'c:\temp';
libname outlib 'c:\' outencoding='UTF-8';
proc copy noclone in=inlib out=outlib;
run;
转换单个数据集的编码
/*转换为UTF-8的编码类型*/
libname inlib cvp 'c:\temp';
libname outlib 'c:\' outencoding='UTF-8';
proc copy noclone in=inlib out=outlib;
select data_set_name;
run;
为了确定当前数据集的编码,可以在SASHELP.VTABLE中找到这个你想要确认编码的数据集,查看它对应的ENCODING变量;也可以通过下面的代码将把数据集的编码和当前SAS会话编码写入日志:
%let dsn=libref.datasetname;
%let dsid=%sysfunc(open(&dsn,i));
%let encoding=%sysfunc(attrc(&dsid,encoding)).;
%let rc=%sysfunc(close(&dsid));
%put The encoding for data set &dsn is: &encoding.;
%put The SAS session encoding is: &sysencoding.;