Writing code is fun but maintaining your own or others can be challenging when it starts to grow. This tip shows how you can, using Boolean Expressions, code big IF statements with less code.
An IF is really a lot of “questions”. Does the customer like music or hiking or both? Have a car? House and kids? Therefore, the same technique with boolean expressions works just as well when we want to summarize these questions.
In this example we want to select prospects for a campaign that only has YES in one of the many profile attributes (Yes or No are possible values), so we line up a series of comparisons or “questions”. Consider we have +100 Yes/No flags and the code needed to say “If attrA=YES and AttrN=NO and attrC=NO and attrD=NO…” and so on. This code will be very painful to read and challenging to change.
The trick here is to use Boolean expressions and sum up the result. Each “test” will return 1 if TRUE and 0 if FALSE.
Example: response = (“1jan2021”d < Date() );
This will return 1 as it’s true. (1 Jan 2021 is less than today’s date.)
So we prefer the second way (of finding subjects with only one interest) in the code below. Full test code further down.
/* Works fine */
if (Music = 'Yes' and Art = 'No' and Walking = 'No') OR
(Music = 'No' and Art = 'Yes' and Walking = 'No') OR
(Music = 'No' and Art = 'No' and Walking = 'Yes') then ...
/* But this is better */
if (Music = 'Yes') + (Art = 'Yes') + (Walking = 'Yes') = 1 then ...
Copy the code below and play with it.
/* Some test data */
data WORK.interests;
Name = 'Adam'; Music = 'Yes'; Art = 'Yes'; Walking = 'No';
OUTPUT;
Name = 'Eva'; Music = 'No'; Art = 'Yes'; Walking = 'No';
OUTPUT;
run;
/* - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* This looks fine as we only have two tests/scenarios */
data WORK.selected_for_campain;
length Select $ 3;
set WORK.interests;
Select = 'No';
/* Each combo of Yes/No requires a comparison block */
if (Music = 'Yes' and Art = 'No') OR
(Music = 'No' and Art = 'Yes') then
Select = 'Yes';
/* Same test can be done using the BXOR function (Binary eXclusive OR) */
if bxor( (Music = 'Yes'), (Art = 'Yes') ) then
Select = 'Yes';
/* More traditional, but just as clean as BXOR */
if (Music = 'Yes') + (Art = 'Yes') = 1 then
Select = 'Yes';
run;
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* With more combinations of Yes/No it gets tricky */
/* BXOR cannot be used now, as it only takes 2 arguments */
data WORK.selected_for_campain;
length Select $ 3;
set WORK.interests;
Select = 'No';
/* Like this? With all the possible combinations. */
if (Music = 'Yes' and Art = 'No' and Walking = 'No') OR
(Music = 'No' and Art = 'Yes' and Walking = 'No') OR
(Music = 'No' and Art = 'No' and Walking = 'Yes') then
Select = 'Yes';
/* Or like this? Adding new tests requires very little code */
if (Music = 'Yes') + (Art = 'Yes') + (Walking = 'Yes') = 1 then
Select = 'Yes';
run;
/* Use the same technique to count the positive tests */
data WORK.count_yes_flags;
set WORK.interests;
/* Count the Yes flags */
Result = (Music = 'Yes') + (Art = 'Yes') + (Walking = 'Yes') ;
run;