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.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.