DATA Step, Macro, Functions and more

Using an array for concatenation adn assign a non missing var

Accepted Solution Solved
Reply
Contributor Kc2
Contributor
Posts: 46
Accepted Solution

Using an array for concatenation adn assign a non missing var

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?

 


Accepted Solutions
Solution
‎01-08-2018 12:36 PM
Super User
Posts: 2,061

Re: Using an array for concatenation adn assign a non missing var

Posted in reply to novinosrin

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


All Replies
Super User
Super User
Posts: 9,840

Re: Using an array for concatenation adn assign a non missing var

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;
Super User
Posts: 2,061

Re: Using an array for concatenation adn assign a non missing var

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;

Solution
‎01-08-2018 12:36 PM
Super User
Posts: 2,061

Re: Using an array for concatenation adn assign a non missing var

Posted in reply to novinosrin

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;

Contributor Kc2
Contributor
Posts: 46

Re: Using an array for concatenation adn assign a non missing var

Posted in reply to novinosrin

thank you.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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