如何避免SAS中put语句中的空格

how to avoid spaces in put satement in SAS

我正在尝试从数据步骤写入 json 文件。

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

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

将创建 {"year":2013 ,

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

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

如何删除 "Rubella virus" 之后的 space 而不会使事情过于复杂?


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

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

谢谢!

您需要将指针向后移动一步。您可以通过要求前进减一步来做到这一点。使用这个:

put ' {"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"}
    ]

此致, 瓦西里

如果您希望获得 'cleaner' 代码,您可以使用 proc fcmp 为自己构建一两个辅助函数。这个函数会接受一个字符串描述,你想要的字段的名字,然后是是否引用返回的字符串。请注意,如果您的值可以包含引号,您可能希望使用 quote() 函数而不是 t

示例函数:

proc fcmp outlib=work.funcs.funcs;

  function json(iName $, iField $, iQuote) $;    
    length result 0;
    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

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

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