在SAS中跨观察值累积文本变量

在SAS中跨观察值累积文本变量,sas,Sas,这看起来很简单,但并不像预期的那样有效: data names; input name $12.; cards; John Jacob Jingleheimer Schmidt ; run; data names; length namelist $100.; set names end=eof; retain namelist; if _n_=1 then namelist=name; else namelist = namelist

这看起来很简单,但并不像预期的那样有效:

data names;
    input name $12.;
    cards;
John
Jacob
Jingleheimer
Schmidt
;
run;

data names;
    length namelist $100.;
    set names end=eof;
    retain namelist;
    if _n_=1 then namelist=name;
    else namelist = namelist || "|" || name;
    if eof then output;
run;
我希望结果有一个包含

约翰|雅各布|伯利恒|施密特


但是
namelist
只是
John
。我做错了什么?

在连接到列表之前,需要修剪空白

data names;
    length namelist $100.;
    set names end=eof;
    retain namelist;
    if _n_=1 then namelist=trim(name);
    else namelist = trim(namelist) || "|" || trim(name);
    if eof then output;
run;
您还可以使用cats()函数(它为您进行修剪和连接):


如果您在作业中添加了条带

strip(namelist) || "|" || name
它也会起作用


(但是CATS是一个非常好的解决方案)

使用catx函数可以指定分隔符

data names;
    length namelist $100.;
    set names end=eof;
    retain namelist;
    namelist = catx("|",namelist,name);
    if eof then output;
run;

啊哈,谢谢!你能解释一下原因吗?文本是否存在填充导致其填充可用空间或其他内容?当然-根据此链接()“当字符值小于其所属变量的长度时,SAS会用尾随空格填充该值。”。如果你看一下关于连接和删除内部空白的标题,它们会更加详细。
data names;
    length namelist $100.;
    set names end=eof;
    retain namelist;
    namelist = catx("|",namelist,name);
    if eof then output;
run;