BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Kc2
Quartz | Level 8 Kc2
Quartz | Level 8

Hello,

 

I have a  5 variables in a dataset (A1 to A5). Each of then can have a value of Y or N. I need to create a 6th variable which give "summary" of the 5 others.

When more than one variable is set to Y then A6=" More than 1".

If only one of then is Y then in A6 I need to add which variable is set to Y:

ex

        A1        A2        A3      A4     A5       A6

1      Y          N          N        Y       N       More than one

2      N          N          N        Y       N        A4

3      Y          N          N        N       N        A1

4     N          N          Y        N       N        A3

 

 

If is possible to do it in a array or do I need to use if- else with multiple combination?

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Prolly more efficient to use whichc and avoid loop:

 


data want1;
set have;
array a(*) a1-a5;
length a6 $20;
if countc(cats(of a(*)), 'Y')>1 then a6='More than one';
else if countc(cats(of a(*)), 'Y')=1 then do;
a6=vname(a(whichc('Y', of a[*])));
end;
run;

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Not tested this code, post test data in the form of a datastep as I am not here to type in test data:

data want;
  set have;
  array a{5};
  length a6 $50;
  do i=1 to 5;
    if a{i}="Y" and a6="" then a6=vname(a{i});
    else if a{i}="Y" and a6 ne "" then a6="More than one";
  end;
run;
novinosrin
Tourmaline | Level 20

DATA HAVE;
input (rownum A1 A2 A3 A4 A5) ($);
datalines;
1 Y N N Y N More than one
2 N N N Y N A4
3 Y N N N N A1
4 N N Y N N A3
;


data want;
set have;
array a(*) a1-a5;
length a6 $20;
if countc(cats(of a(*)), 'Y')>1 then a6='More than one';
else if countc(cats(of a(*)), 'Y')=1 then do;
do _n_=1 to dim(a);
if a(_n_)='Y' then a6=vname(a(_n_));
end;
end;
run;

novinosrin
Tourmaline | Level 20

Prolly more efficient to use whichc and avoid loop:

 


data want1;
set have;
array a(*) a1-a5;
length a6 $20;
if countc(cats(of a(*)), 'Y')>1 then a6='More than one';
else if countc(cats(of a(*)), 'Y')=1 then do;
a6=vname(a(whichc('Y', of a[*])));
end;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1532 views
  • 4 likes
  • 3 in conversation