How to use Boolean datatype in SAS?

Atlas7 picture Atlas7 · May 19, 2016 · Viewed 9.7k times · Source

In many mainstream programming languages you can use Boolean data-type (for instance, value can be either true or false) - to represent binary "true/false".

Is there a Boolean data-type in SAS too?

For example, in this code, the variable is_fruit is meant to represent a binary true (1) or false (0) situation. As I am not aware of any Boolean data-types (or length types), I work around this using numeric:

data is_fruit;
    length fruit_name $ 40 is_fruit 8.;
    input fruit_name $ is_fruit;
    datalines;
apple 1
orange 1
car 0
tree 0
chicken 0
peach 1
mango 1
human 0
;

run;

Output:

enter image description here

enter image description here

To me this is not a very elegant data structure as the variable is_fruit is really a Boolean data-type (not numeric nor character). So my question is again...

Is there a Boolean data-type (or length type) for the is_fruit above?

Answer

Joe picture Joe · May 19, 2016

Base SAS has only two datatypes: Numeric, and Character. Numeric has length 3 bytes to 8 bytes possible, character has any length (1 byte or more).

Boolean expressions in SAS are equivalent to numeric values; a 0 or Missing (Null) value is "False", any other value (negative or positive) is "True". Assigning the result of a boolean expression in SAS to a value results in 1 for True and 0 for False.

Boolean values can be stored in a 3 length Numeric safely, or can be converted to Character if space is truly a concern. SAS is optimized for 8 byte numerics, though, and other than storage space it doesn't really help to shorten the numeric (internally during processing it will be stored in 8 bytes of RAM).