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?
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;
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;
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;
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;
thank you.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.