【发布时间】:2016-12-18 05:56:57
【问题描述】:
我有这个创建 json 文件的宏,
但即使我指定了encoding='utf-8' bom,我也没有得到 utf-8 文件。
%macro json4datatables_useformat(ds,path,file,charvars,numvars)
/
DES="json4datatables(ds,path,file,charvars,numvars)";
/* creates a json with no headers
* a bit like a csv without the first line
* it takes thus less space
* but you have to know which column is what
*/
data _null_;
length line $300;
set &ds nobs=nobs end=end;
file "&path.&file." encoding='utf-8' bom/**/ ;
line = '[';
%if &charvars ne %then %do;
%do i=1 %to %sysfunc(countw(&charvars));
%let charvar = %scan(&charvars, &i);
%if &i ne 1 %then %do;
line = cats(line,',');
%end;
line = cats(line,'"',vvalue(&charvar),'"');
%end;
%end;
%if &numvars ne %then %do;
%do i=1 %to %sysfunc(countw(&numvars));
%let numvar = %scan(&numvars, &i);
line = catx(',',line,vvalue(&numvar));
%end;
%end;
line = cats(line,']');
if _n_=1 then put '{"data": [';
if not end then put line +(-1) ',';
else do;
put line;
put ']}';
end;
run;
%mend json4datatables_useformat;
我注意到,因为重音在我显示 json 文件的网页上显示为奇怪的字符。
只需通过在 sublime 文本中打开 json 文件并执行 File>Save with Encoding>UTF-8 即可解决此问题。
(不需要 BOM。)
还有其他强制 utf-8 编码的方法吗?
编辑:我在 Windows 上使用 SAS EG 7.1 和 SAS 9.3。
【问题讨论】:
-
我使用了您的宏并导出了带有一些波兰字符的数据。 Notepad++ 说它是 UTF8,Chrome 浏览器可以正确显示所有字符。您的 SAS 版本是多少?
-
我猜这可能是系统相关的
-
您的 SAS 会话使用什么编码?从理论上讲,这无关紧要,但我发现有时使用 UTF-8 会话而不是 ANSI 会话会有所帮助。另外,您运行的是什么版本的 SAS?
-
如何找到 SAS 会话的编码?我在 Windows 上使用 SAS EG 7.1 和 SAS 9.3。
-
我对 wlatin2 会话和写入 utf8 没有任何问题。这是另外一回事。如果我得到什么,我会告诉你的。顺便说一句,宏不错:)
标签: json encoding utf-8 sas datastep