json - 如何在 SAS 中连接格式化值

标签 json datatables sas format datastep

我正在使用我制作的宏以 JSON 格式导出数据:

%macro json4datatables(ds,path,file,charvars,numvars)
    / store source
    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,'"',&charvar,'"');
            %end;
        %end;
        %if &numvars ne %then %do;
            %do i=1 %to %sysfunc(countw(&numvars));
                %let numvar = %scan(&numvars, &i);
                %if &i ne 1 OR &charvars ne %then %do;
                    line = cats(line,',');
                %end;
                line = cats(line,'',&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;

但我的问题是原始值被导出。
我想导出格式化的值。

我怎样才能实现这个目标?
我想也许有一个函数允许连接格式化值而不是值,我可以用它替换 cats() 。

谢谢!

最佳答案

使用VVALUE()函数。

line = cats(line,'',vvalue(&numvar),'');

为什么不直接使用 CATX() 函数呢?替换

%if &i ne 1 OR &charvars ne %then %do;
    line = cats(line,',');
%end;
line = cats(line,'',vvalue(&numvar),'');

line = catx(',',line,vvalue(&numvar));

对于字符值,请使用QUOTE()函数。

line = catx(',',line,quote(cats(vvalue(&charvar))));

将添加的方括号移到末尾。

line = cats('[',line,']');

关于json - 如何在 SAS 中连接格式化值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38896340/

相关文章:

jquery - 在对 JQuery 数据表进行初始排序时最后显示空值

javascript - Jquery 数据表在 IE9 上呈现缓慢

unit-testing - SAS : FUTS vs. SASUnit 中的单元测试框架

sas - 数据清理: processing extra if statements vs performing extra assignments

sas - 添加新的 SAS 变量,同时保留完整性约束

arrays - 从 GoLang 中单独的 []byte 结果创建 JSON 数组

ruby-on-rails - 使用 RABL 为 JSON 模板进行缓存预热

java - Jackson 自定义序列化和反序列化

json - 使用 PowerShell 循环确定对象和/或 JSON 值的深度

jquery - 仅导出 jquery-datatables 中过滤的记录