用于连接字符串的 sas 数组

Array in sas for Concatinating strings

我有一个包含名称和字符串的数据,我想将所有字符串(col1、col2 等)添加到一列中。 字符串的数量不是固定的,有时可能或多或少。 我可以用 catx 做到这一点,但不知道如何用数组来实现。 下面是我的数据集。请指导。

data a;    

    input name$  col1$ col2$ col3$ col4$;

DATALINES;
Harry abc dcd vgd bvd
peter cvc fgf ghg ghh
John fgg ftg uty gfdg
sheyala fgf jty fhf fgr
;
run;

这是我的代码:

data test;
length result ;
set a;
result=Compress(catx(';',of col1-col4),'0D0A'x);
run;

但是串数不固定。

感谢和问候, 桑杰

您可以定义一个元素数量不确定的数组。这假设您的所有列都以 col.

开头
data test;
length result ;
set a;
array c[*] col:;

result = "";
do i=1 to dim(c);
    result = Compress(catx(';',result,c[i]),'0D0A'x);
end;
drop i;
run;

col: 告诉 SAS 您希望所有变量都以 col 开头,数组中的 [*] 告诉 SAS 自己定义元素的数量。使用 dim() 函数获取该数字并遍历这些值。

编辑:

如评论中所述,以下方法也有效。

不使用数组:

data test;
length result ;
set a;

result = Compress(catx(';',of col:),'0D0A'x);

run;

或者如果您仍然想要数组:

data test;
length result ;
set a;
array c[*] col:;

result = Compress(catx(';',of c[*]),'0D0A'x);
run;