2

我正在使用我制作的这个宏导出带有 sas 的 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()。

谢谢!

4

3 回答 3

3

使用该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,']');
于 2016-08-11T12:38:37.603 回答
2

VVALUE 可能是您正在寻找的功能,但它不会取代 CATS。也用于引用使用 QUOTE 功能。

于 2016-08-11T12:36:40.673 回答
0

您可以使用put,putcputn函数以格式打印值,以获取格式使用vformat function,但当您仅使用vformat functionputn支持putc它时。

line = cats(line,'"',putc(&charvar, vformat(&charvar)),'"');
line = cats(line,'',putn(&numvar, vformat(&numvar)),'');
于 2016-08-11T12:41:22.153 回答