0

This is the given dataset

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

I want to create a new column that displays all the column names when the corresponding value is 1. So for example, for ins_id=1234, the string would be 'Prod1, Prod3'. There are at least 40 variables.

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

Can someone please help? Appreciate it!

Chug
  • 31
  • 6

1 Answers1

2

Some basic array functionality can help you out here. Use VNAME() to get the variable name from the array if the value is 1 and use CATX() to combine the data together.

untested:

data want;
set have;
length prod_yes $100.;

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;
Reeza
  • 20,510
  • 4
  • 21
  • 38