```
data logical;
length operator $ 4;
input x y;
condition = x and y;
operator = 'AND';
output;
condition = x or y;
operator = 'OR';
output;
condition = not x;
operator = 'NOT';
output;
datalines;
1 1
1 0
1 .
0 .
. .
;
```

# Logical-valued Data

SAS does not have a special “logical” data type: SAS only works with character and floating point numeric data. In SAS, logical values are handled as numeric data: 0, 1, or missing.

- where a SAS expression
**returns**a logical value, it will be either 0 or 1. - where SAS
**evaluates**a “logical” expression, it only assumes the data is numeric, treating 0 and missing data as false, and any other numeric value as true.

## Logical Operators

SAS has the usual logical operators: AND, OR, and NOT. These many be written mnemonically or symbolically, e.g. `AND`

or `&`

.

Given the values 0 and 1, these behave as you expect. Notice in the following example that a missing value is equivalent to 0, a “false” value.

```
proc print data=logical noobs;
where operator eq 'AND';
var x operator y condition;
run;
```

```
x operator y condition
1 AND 1 1
1 AND 0 0
1 AND . 0
0 AND . 0
. AND . 0
```

```
proc print data=logical noobs;
where operator eq 'OR';
var x operator y condition;
run;
```

```
x operator y condition
1 OR 1 1
1 OR 0 1
1 OR . 1
0 OR . 0
. OR . 0
```

```
proc print data=logical noobs;
where operator eq 'NOT';
var operator x condition;
run;
```

```
operator x condition
NOT 1 0
NOT 1 0
NOT 1 0
NOT 0 1
NOT . 1
```

Because these operators are acting on numeric values as inputs, you can also understand them to be checking if a value is 0 or missing (“false”), or not (“true”).

```
data logical;
length operator $ 4;
input x y;
condition = x and y;
operator = 'AND';
output;
condition = x or y;
operator = 'OR';
output;
condition = not x;
operator = 'NOT';
output;
datalines;
5 -3
2.7 0
100 .
;
```

```
proc print data=logical noobs;
where operator eq 'AND';
var x operator y condition;
run;
```

```
x operator y condition
5.0 AND -3 1
2.7 AND 0 0
100.0 AND . 0
```

```
proc print data=logical noobs;
where operator eq 'OR';
var x operator y condition;
run;
```

```
x operator y condition
5.0 OR -3 1
2.7 OR 0 1
100.0 OR . 1
```

```
proc print data=logical noobs;
where operator eq 'NOT';
var operator x condition;
run;
```

```
operator x condition
NOT 5.0 0
NOT 2.7 0
NOT 100.0 0
```

## Comparison Operators

SAS also has the usual comparison operators, , including the IN operator commonly found in data processing languages. These too may be written mnemonically or symbolically, e.g. ‘EQ’ or `=`

(note this is a single equals symbol in SAS), `LE`

or `<=`

, etc. These may be used to compare numbers verus numbers or character strings versus character strings.

### Missing Values

A special consideration is how SAS uses missing values in comparisons.

In a comparison, SAS treats missing values as if they are simply the smallest possible numeric values. So `. <= -100`

is true!

### Decimal Values

Comparisons of values that have decimal components can be problematic with any computer software (because the computation is actually done in binary).

```
data decimal;
x = 0.2;
y = 0.3 - 0.1;
condition = (x = y);
run;
proc print noobs; run;
```

```
x y condition
0.2 0.2 0
```

Although we understand that \(0.2 = 0.3 - 0.1\) is true, when this expression is converted into binary, the numbers can no longer be represented in finite precision. The necessary rounding means these numbers are **not** exactly equal.

A better comparison these, it to check that the difference between these two numbers is “small enough” to be essentially zero. Use conditions of the form

`x - y < 1e-16`

```
data decimal;
x = 0.2;
y = 0.3 - 0.1;
condition = (x - y < 1e-16);
run;
proc print noobs; run;
```

```
x y condition
0.2 0.2 1
```

See SAS’s discussion of numeric precision for more details.