Search code examples
sasmultiple-columns

Put multiple values of different variables in one column SAS


I have a dataset similar to this (Se is variable Serious):

Se D L C Di H O

N

N

N

Y N Y N N N N

N

N

N

Y N Y N Y N N

variables Death (D), Life_Threat (L), Congenital_anom (C), Disability (Di), Hospitalizaion (H) and Other (O) have to match a value from 1 (for variable Death) to 6 (for variableOther) and I have to record these values in a new variable that in the case for example of the last line where I have two 'Y' takes into account both values in this way 2,4. How can I do it? (I have lines where all 6 variables are Y).

Thanks in advance

I don’t know how to set the new variable so that it considers all the 6 variables for which I have Y


Solution

  • You can use the function catx to combine values to one string:

    Generating your sample data:

    data have;
    input Se $1. D $1. L $1. C $1. Di $1. H $1. O $1.;
    datalines;
    N
    N
    N
    YNYNNNN
    N
    N
    N
    YNYNYNN
    YYYYYYY
    run;
    

    Here, an array is defined that consists of the six input variables and the function catx is used. In "synthesis" you will find the lists of Y-values in the form "2" or "2,4", respectively, or "1,2,3,4,5,6" in the last example.

    data want (drop=i);
    set have;
    length synthesis $12;
    array yn {6} D L C Di H O;
    do i=1 to 6;
      if yn{i}="Y" then synthesis=catx(',',synthesis,put(i,$1.));
    end;
    run;