BookmarkSubscribeRSS Feed
nid197
Obsidian | Level 7
Hi I want to check each row in flag1-flag10 and update a final_final-

Accn flag1 flag2 flag3 flag4 flag 5 final_flag
123 y y y n n y
573 n n n n n n
962 n n y n n y

I.e from flag1 to flag5 (n) if i get any y then final_flag= y else if all n then only final_flag=n

Can anyone help?
10 REPLIES 10
PaigeMiller
Diamond | Level 26

You can use the WHICHC function. There is an example in the documentation.

https://documentation.sas.com/?cdcId=pgmmvacdc&cdcVersion=9.4&docsetId=lefunctionsref&docsetTarget=p...

 

--
Paige Miller
novinosrin
Tourmaline | Level 20
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;
Peter_C
Rhodochrosite | Level 12
There is an old function VERIFY() which is useful for checking the existence of specific characters.... like
if verify( cats( of flag:), "yY") then final_flag ="y" ;

VERIFY() operates on a single string, seeking the first occurrence of any character in the second parameter of tge function and returns 0 if none are found. It is case sensitive, so providing both upper and lower y makes the search case-insensitive.
The CATS() function returns a concatenation of the flagN vars. It requires a list of the variables to concatenate - the " of flag:" works neatly to achieve that.

One way this VERIFY() approach might fail, arises when the flags contain other strings than just a y or n - for example , if a flag var holds the 3 chars "MAY" then that Y would result in final_flag set to y
Peter_C
Rhodochrosite | Level 12
Didn't expect to see a smiley icon.
What was intended there
"flag:"
without these quotes
nid197
Obsidian | Level 7
@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' .
novinosrin
Tourmaline | Level 20

 


@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

nid197
Obsidian | Level 7
@Peter_C
the verify function has error as it does not have enough arguments.
Peter_C
Rhodochrosite | Level 12
If verify( cats(of flag: ), 'yY' ) then final_flag = 'Y' ;
ballardw
Super User

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.

 

Patrick
Opal | Level 21

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;

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
  • 10 replies
  • 2225 views
  • 4 likes
  • 6 in conversation