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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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