BookmarkSubscribeRSS Feed
sam_sas2
Obsidian | Level 7

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! 🙂

7 REPLIES 7
SASKiwi
PROC Star

No difference except you save a little bit of typing in the second example.

Astounding
PROC Star

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
Obsidian | Level 7
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! 🙂
Kurt_Bremser
Super User

@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".

ballardw
Super User

@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.

sam_sas2
Obsidian | Level 7
Yes. I got that! Now I just want to understand in terms of efficiency when I run the program with syntax
IF <variable> then output vs IF <variable>= x then output
assuming both would give me the same output.
Hope my question is clear! 🙂
Ksharp
Super User
As Astounding , my guess is also second one .

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 832 views
  • 3 likes
  • 6 in conversation