如何避免在SAS

时间:2016-07-11 15:05:48

标签: sas datastep

我正在尝试从数据步骤中编写一个json文件。

但是我的put语句总是在变量之后添加不需要的空格。

put '           {"year":' year ',';

将创建{"year":2013 ,

put '   {"name":"' %trim(name) '", ' ;

将创建{"name":"Rubella virus ",

如何在没有过度复杂化的情况下删除“风疹病毒”之后的空间?

到目前为止,我最好的解决方案是创建一个使用cats的变量,然后将new变量放得像这样:

newvar=cats('{"name":"',name,'",');
put newvar;

谢谢!

3 个答案:

答案 0 :(得分:4)

您需要将指针向后移动一步。你这样做是要求前进一步。使用此:

将'{“name”:“'name(+) - 1'”,';

很奇怪,我知道,但它确实有效。

以下是sashelp.class的示例:

代码:

data _null_;
    set sashelp.class end = eof;

    if _N_ eq 1 then
        put '[';
    put '{ "Name":"' Name+(-1) 
        '","Sex":"' Sex+(-1) 
        '","Age":"' Age+(-1) 
        '","Height":"' Height+(-1) 
        '","Weight":"' Weight+(-1) 
        '"}';

    if eof then
        put ']';
    else put ',';
run;

结果:

    [
    { "Name":"Alfred","Sex":"M","Age":"14","Height":"69","Weight":"112.5"}
    ,
    { "Name":"Alice","Sex":"F","Age":"13","Height":"56.5","Weight":"84"}
    ,
    { "Name":"Barbara","Sex":"F","Age":"13","Height":"65.3","Weight":"98"}
    ,
    { "Name":"Carol","Sex":"F","Age":"14","Height":"62.8","Weight":"102.5"}
    ,
    { "Name":"Henry","Sex":"M","Age":"14","Height":"63.5","Weight":"102.5"}
    ,
    { "Name":"James","Sex":"M","Age":"12","Height":"57.3","Weight":"83"}
    ,
    { "Name":"Jane","Sex":"F","Age":"12","Height":"59.8","Weight":"84.5"}
    ,
    { "Name":"Janet","Sex":"F","Age":"15","Height":"62.5","Weight":"112.5"}
    ,
    { "Name":"Jeffrey","Sex":"M","Age":"13","Height":"62.5","Weight":"84"}
    ,
    { "Name":"John","Sex":"M","Age":"12","Height":"59","Weight":"99.5"}
    ,
    { "Name":"Joyce","Sex":"F","Age":"11","Height":"51.3","Weight":"50.5"}
    ,
    { "Name":"Judy","Sex":"F","Age":"14","Height":"64.3","Weight":"90"}
    ,
    { "Name":"Louise","Sex":"F","Age":"12","Height":"56.3","Weight":"77"}
    ,
    { "Name":"Mary","Sex":"F","Age":"15","Height":"66.5","Weight":"112"}
    ,
    { "Name":"Philip","Sex":"M","Age":"16","Height":"72","Weight":"150"}
    ,
    { "Name":"Robert","Sex":"M","Age":"12","Height":"64.8","Weight":"128"}
    ,
    { "Name":"Ronald","Sex":"M","Age":"15","Height":"67","Weight":"133"}
    ,
    { "Name":"Thomas","Sex":"M","Age":"11","Height":"57.5","Weight":"85"}
    ,
    { "Name":"William","Sex":"M","Age":"15","Height":"66.5","Weight":"112"}
    ]

此致 瓦西里

答案 1 :(得分:2)

对于字符字段,您可以使用$QUOTE.格式添加引号。使用:删除变量值中的尾随空白。

put '{ "Name":' Name :$quote.
    ',"Sex":' Sex :$quote.
    ',"Age":"' Age +(-1) '"' 
    ',"Height":"' Height +(-1) '"'
    ',"Weight":"' Weight +(-1) '"'
    '}'
;

答案 2 :(得分:1)

如果您希望拥有“更干净”的代码,可以使用proc fcmp为自己构建一个或多个辅助函数。此函数将获取字符串描述,所需字段的名称,然后是否引用返回的字符串。请注意,如果您的值可以包含引号,则可能需要使用quote()函数而不是t

示例功能:

proc fcmp outlib=work.funcs.funcs;

  function json(iName $, iField $, iQuote) $;    
    length result $200;
    quote_char = ifc(iQuote,'"','');
    result = cats('"', iName, '":',quote_char, iField, quote_char ); 
    return (result );
  endsub;

run;

使用示例:

data _null_;
  set sashelp.class;
  x = catx(',',
           json("name",name,1), 
           json("age",age,0));
  put x;
run;

示例输出:

"name":"Alfred","age":14
"name":"Alice","age":13
"name":"Barbara","age":13
"name":"Carol","age":14
"name":"Henry","age":14
"name":"James","age":12
"name":"Jane","age":12