Search code examples
loopssascategorical-data

How to create a categorical variable in which are reported the name of dummy variable when result interger value 1 in SAS


someone can help me to generate a categorical variable in SAS?

I have a dataset like this

K234 K234 K24a34 K34j43 k...
0 1 0 1 ...
1 0 1 0 ..
0 1 1 1 ...

I don't know how obtain a one categorical variable that concat the names of dummy variables where is reported the integer value 1.

The result that i whish to obtein is like this:

type
K234-K34j43
K234-K24a34
K234-K24a34-K34j43

thank you !


Solution

    • VNAME() to get the variable name
    • ARRAY for dealing with multiple variables
    • CATX() for separators in concatenated variables
    data want;
    set have;
    
    length type $200;
    
    array _k_list(*) K: ; *list of variables to scan for 1s;
    
    do i=1 to dim(_k_list);
    
    if _k_list(i) = 1 then
          type = catx("-", type, vname(_k_list(i)));
    end;
    
    run;