【问题标题】:Proc json produces extra blanks after applying a formatProc json 在应用格式后产生额外的空白
【发布时间】:2022-01-12 11:36:49
【问题描述】:

我想将 sas 数据集导出到 json。我需要应用 commax10.1 格式以使其适用于某些语言版本。问题是 fmtnumeric 选项正确应用了格式,但在引号内插入了额外的空格。我尝试过修剪空白和其他选项,但无法摆脱它们。如何删除引号内的空白?注意:我希望值保留在引号内

另外,是否可以用“”代替空值?

样本数据:

data testdata_;
input var1 var2 var3;
format _all_ commax10.1;
datalines;
 3.1582 0.3 1.8
 21 . .
 1.2 4.5 6.4
;
proc json out = 'G:\test.json' pretty fmtnumeric nosastags trimblanks keys;
export testdata_;
run;

在链接中你可以看到输出的样子。

output of json

【问题讨论】:

    标签: json sas


    【解决方案1】:

    使用strips 开头和结尾空格的自定义格式函数。

    例子:

    proc fcmp outlib=work.custom.formatfunctions;
      function stripcommax(number) $;
        return (strip(put(number,commax10.1)));
      endsub;
    run;
    
    options cmplib=(work.custom);
    
    proc format;
      value commaxstrip other=[stripcommax()];
    run;
    
    data testdata_;
    input var1 var2 var3;
    datalines;
     3.1582 0.3 1.8
     21 . .
     1.2 4.5 6.4
    ;
    proc json out = 'test.json' 
    pretty 
    fmtnumeric 
    nosastags 
    keys 
    /*
    /* trimblanks */
    ;
    format var: commaxstrip.;
    export testdata_;
    run;
    
    data _null_;
      infile 'test.json';
      input;
      put _infile_ ;
    run;
    

    【讨论】:

    • 喜欢这个想法。两个强大概念的完美结合。
    【解决方案2】:

    trimblanks 只修剪尾随空格。格式本身添加了前导空格,proc json 没有我知道的删除前导空格的选项。

    一种选择是将所有值转换为字符串,然后导出。

    data testdata_;
        input var1 var2 var3;
        format _all_ commax10.1;
    
        array var[*] var1-var3;
        array varc[3] $;
    
        do i = 1 to dim(var);
            varc[i] = strip(compress(put(var[i], commax10.1), '.') );
        end;
    
        keep varc:;
    
        datalines;
    3.1582 0.3 1.8
    21 . .
    1.2 4.5 6.4
    ;
    

    这将是一个很棒的功能请求。我建议将此发布到 SASWare Ballot Ideas 或联系 Tech Support 并让他们知道此问题。

    【讨论】:

      【解决方案3】:

      这不是 proc json 的唯一问题 - 其他挑战包括 SAS 9 _webout 目标的行长度限制、摄取无效字符时的 proc 失败以及无法 mod 到目标。

      因此,在SASjs 框架和Data Controller 中,我们倾向于恢复为数据步骤方法。

      我们的宏是开源的,可在此处获取:https://core.sasjs.io/mp__jsonout_8sas.html

      要发送格式化的值,调用如下:

      data testdata_;
      input var1 var2 var3;
      format _all_ commax10.1;
      datalines;
       3.1582 0.3 1.8
       21 . .
       1.2 4.5 6.4
      ;
      filename _dest "/tmp/test.json";
      
      /* open the JSON */
      %mp_jsonout(OPEN,jref=_dest)
      /* send the data */
      %mp_jsonout(OBJ,testdata_,jref=_dest,fmt=Y)
      /* close the JSON */
      %mp_jsonout(CLOSE,jref=_dest)
      
      /* display result */
      data _null_;
        infile _dest;
        input;
        putlog _infile_;
      run;
      

      这给出了:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-10
        • 2018-06-16
        • 2021-07-25
        • 2020-04-02
        相关资源
        最近更新 更多