You can use the WHICHC function. There is an example in the documentation.
data have;
input (Accn flag1 flag2 flag3 flag4 flag5) (: $3.) ;
cards;
123 y y y n n y
573 n n n n n n
962 n n y n n y
;
data want;
set have;
array f(*) flag:;
final_flag='n';
if 'y' in f then final_flag='y';
run;
@nid197 wrote:
@novinosrin
This solution is working fine.
Just the problem is I need final_flag 'Y' if 'Y' is there in any of the flag(no matter how many N are there)
And final_flag will be no only if all the flags are 'N' .
@nid197 Sir./mam.
Just the problem is I need final_flag 'Y' if 'Y' is there in any of the flag(no matter how many N are there)
That's exactly what my solution does . Kindly test plz
Another example where using actual binary numeric values instead of characters for y and n work much easier.
If 1 = Y and 0 = n
then:
data have; input Accn flag1 flag2 flag3 flag4 flag5 ; final_flag= (max(of flag:)=1); datalines; 123 1 1 1 0 0 573 0 0 0 0 0 962 0 0 1 0 0 ; run;
Add 27 more "flag" variables and the code doesn't need a change.
Some additional bits: The mean of any of the flag variables would be the percent, in decimal form, of Yes values; the sum of the variable would be the number of Yes values.
And 1 / 0 are natural results of any logical comparison in SAS, as shown above.
If you must see text of Y or N then a custom format to display Y for 1 and N for 0 is pretty trivial (I have several versions depending on what is needed with missing or special missing values).
If you want to know if all of the values are the same : if range( of flag:) = 0.
If you want to know if all of the values are 0 : if sum(of flag: )= 0 ;
If you want to know if all of the values are 1 : if sum(of flag: ) = n(of flag:) which adjusts for missing until all are missing.
You can even test such as if a percentage of values are 1: if mean(of flag:) > 0.5 (more than half for example)
If want to know if a number of values were yes such as 3 to 5: if 3 le sum( of flag:) le 5.
So many things that get to be obnoxious with Y/N, T/F or other character coding.
Here one way to go:
data have;
input Accn $3. (flag1 flag2 flag3 flag4 flag5) (: $upcase1.) ;
cards;
123 y y y n n y
573 n n n n n n
962 n n y n n y
;
run;
data want;
set have;
final_flag=ifc(whichc('Y',of flag:),'Y','N');
run;
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!
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.