Using Boolean Values for Variables
You can set a variable to the value of a logical expression. This can make your code much easier to understand and maintain, particularly if the conditions are complex and/or are repeated several times in the code. A simple example:
data test; set sashelp.tourism; drop _: ; _exchange_rate_condition=exsp > exuk; _price_index_condition=puk > cpisp; if _exchange_rate_condition and _price_index_condition; run;
Here we are creating variables called _EXCHANGE_RATE_CONDITION and _PRICE_INDEX_CONDITION that are either true (1) or false (0) depending on the result of the condition that follows (and which we can usually drop from the output dataset).
Similarly you could make definitions like:
Use of "_" is simply a convention that makes it easy to drop all temporary variables on a DROP statement (as in this example).
A favourite trivia question: Is this SAS statement valid, and if so, what does it mean?
Answer: Yes, it's perfectly valid. It sets A to 1 ('true’) if the values of B, C and D are all the same, otherwise it sets A to 0.
Only a is a Boolean here and the statement could more clearly be written: