Hi All!
Assume a variable binary which has values- 0 and 1.
Which piece of code will be more efficient and why?
1. CODE-1
if binary=1 then output;
2. CODE-2
if binary then output;
Thanks in advance! 🙂
No difference except you save a little bit of typing in the second example.
CODE 2 will be slightly more efficient. But you probably can't measure the difference without billions of observations.
CODE 1 has an extra step: determine whether BINARY is equal to 1 or not. Replace true comparisons with 1 and false comparisons with 0. Then determine whether the result is true or false (1 will be true, and 0 will be false.)
CODE 2 skips a very, very tiny step. It determines whether BINARY is true or false. (Again, 1 is true and 0 is false.) So that's one less step. One very, very tiny step.
If BINARY were able to take on values other than 0 or 1, the answer becomes a bit more complex.
@sam_sas2 wrote:
Hey! Thanks for the explanation. For arguments sake, let us assume the variable BINARY can take values from 0 to 9. Then how does it work?
Thanks in advance! 🙂
It then depends which of those values are considered "true" and which ones "false".
@sam_sas2 wrote:
Hey! Thanks for the explanation. For arguments sake, let us assume the variable BINARY can take values from 0 to 9. Then how does it work?
Thanks in advance! 🙂
How are you using those values?
If you mean to try:
if value then output;
Then everything except 0 would be output. (0 is false, the other values would be treated as "true").
But if want to do something different with different values, or groups of values, then it depends what you want to do conditionally.
Read the documentation on SELECT/WHEN or IF/THEN/ELSE as starters.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.