SAS Bitwise functions

0

In SAS, we have the ability to perform and apply bitwise logical operations. There are numerous bitwise logical functions available within SAS, and I would like to highlight a few of them for you.

To understand these functions, it is necessary to understand logical AND, OR and XOR operations.

·       Logical AND   : Returns true if both operands are true.

·       Logical OR      : Returns true if at least one of the operands is true.

·       Logical XOR   : Returns true if only one of the operands is true.

 

SAS offers build in functions that allow us to apply these logical operations. These are:

·       BAND

·       BOR

·       BXOR

 

Code to generate bitwise logical table:

data Input;

input x y;

datalines;

0 0

0 1

1 0

1 1

;

run;

 

data table;

set Input;

x_and_y   = BAND(x,y);

x_or_y    = BOR(x,y);

x_xor_y   = BXOR(x,y);

run;

 

proc print noobs; run;

Output:

 

x

y

x and y

x or y

x xor y

0

0

0

0

0

0

1

0

1

1

1

0

0

1

1

1

1

1

1

0

 

 

Now if you are wondering where we can use these functions, then do not worry I will explain to you a few cases where these functions are super useful.

 

Dataset:

Consider the below patient medication dataset contains information on the medications given to five patients. Each row in the dataset represents the medication status for medication A and medication B.

·       USUBJID: A unique identifier for the patient.

·       medicine_A, Medicine_B: A binary variable that indicates whether the patient is taking medicine A, B. 1 indicates the patient is taking medicine and 0 indicates the patient is not taking medicine.

USUBJID

medicine_A

medicine_B

1001

1

0

1002

0

1

1003

1

1

1004

0

0

1005

1

0

 

Dataset code:

 

data Medication;

  length USUBJID $10 medicine_A $1 medicine_B $1;

  informat USUBJID $10.;

  input USUBJID medicine_A medicine_B;

  datalines;

1001 1 0

1002 0 1

1003 1 1

1004 0 0

1005 1 0

;

run;

 

Questions:

1.     If I will ask you to create a ABflag variable that flags patients who are taking either medication.

 

Solution: We can accomplish this using BOR function

data ABflag;

set medication;

ABflag = BOR(medicine_A, medicine_B);

run;

proc print noobs; run;

USUBJID

medicine_A

medicine_B

ABflag

1001

1

0

1

1002

0

1

1

1003

1

1

1

1004

0

0

0

1005

1

0

1

 

2.     To create a flag variable that shows if a patient has taken both medications.

 

Solution: We can use the BAND function here.

data ABflag;

set medication;

ABflag = BAND(medicine_A, medicine_B);

run;

proc print noobs; run;

 

USUBJID

medicine_A

medicine_B

ABflag

1001

1

0

0

1002

0

1

0

1003

1

1

1

1004

0

0

0

1005

1

0

0


3.     Similarly, to create a flag variable that indicates if a patient has either taken or not taken either medication A or medication B.

 

Solution: We can use the BXOR function.

 

data ABflag;

set medication;

ABflag = BXOR(medicine_A, medicine_B);

run;

proc print noobs; run;

 

USUBJID

medicine_A

medicine_B

ABflag

1001

1

0

1

1002

0

1

1

1003

1

1

0

1004

0

0

0

1005

1

0

1

 

 

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !