Tips:Double negatives to normalize a boolean value

From sasCommunity
Jump to: navigation, search

You can normalize a numeric value into a "boolean numeric" value (i.e. 0 or 1) by using the double negatives. This works because SAS, lacking a true boolean type: (1) considers 0 or missing as false: everything else as true; and (2) evaluates false to 0 and true to 1. Paul Dorfman popularized the shorthand form of double negatives (^^ or ~~) used for this purpose. Here is an example.

/* create a flag var, found, which is 1 if the number 5 is found among the vars p1-p5; 0 otherwise */
data one;
  input p1-p5;
  found = ^^whichn(5, of p:);
cards;
1 2 3 4 5
6 7 8 9 0
;
run;
 
/* check */
proc print data=one noobs;
run;
/* on lst
p1    p2    p3    p4    p5    found
 
 1     2     3     4     5      1
 6     7     8     9     0      0
*/
Read more

Submitted by Chang Y. Chung. Contact me at my Discussion Page.