当值为1时在sas中获取一串列名

Get a string of column names in sas when value is 1

这是给定的数据集

Ins_id   Prod1 Prod2 Prod3 Prod4

1234     1     0     1     0
5678     0     0     1     0
91011    0     1     0     1
12131    1     1     0     1

我想创建一个新列,当对应值为 1 时显示所有列名。例如,对于 ins_id=1234,字符串将是 'Prod1, Prod3'。至少有40个变量。

Expected Output:
    Ins_id   Prod1 Prod2 Prod3 Prod4 Prod_Yes

    1234     1     0     1     0     Prod1, Prod3
    5678     0     0     1     0     Prod3
    91011    0     1     0     1     Prod2, Prod4
    12131    1     1     0     1     Prod1, Prod2, Prod4

有人可以帮忙吗?点赞!

一些基本的数组功能可以帮助您解决问题。如果值为 1,则使用 VNAME() 从数组中获取变量名称,并使用 CATX() 将数据组合在一起。

未测试:

data want;
set have;
length prod_yes 0.;

array prod(*) prod1-prod4;

do i=1 to dim(prod);
     if prod(i) = 1 then prod_yes = catx(", ", prod_yes, vname(prod(i)));
end;

run;