BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Bogdan
Calcite | Level 5

Which way is faster?

Suppose I want to write the expression (k=1)*e+(k=2)*f and suppose that e,f need to be calculated. Does SAS calculate e,f , then (k=1)*e+(k=2)*f  or does it evaluate first the boolean expressions (k=1) and (k=2) and then the corresponding e if k=1 is true or f if k=2 is true.

If SAS calculates both e and f  then it is more efficient to do a conditional

if k=1 then do e;

else do f;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Yes, all the math will get performed.  You might get some savings by switching as you suggest:

if k=1 then ...;

else if k=2 then ...;

In addition, this code works faster if k=1 most of the time.  If k=2 most of the time, you could switch and test for k=2 first.  But truthfully, the savings will be tiny, perhaps not even measurable.  If you are interested in efficiency, you might post more of your code and get suggestions about how to speed it up.

Good luck.

View solution in original post

3 REPLIES 3
data_null__
Jade | Level 19

Why don't you write some code to test your assumption?  Do each a million times and observe the time difference.  You will need options FULLSTIMER.

Ron_MacroMaven
Lapis Lazuli | Level 10

Here are references to help you understand the order of evaluation of an expression:

Logic: and, not, or

SAS(R) 9.3 Language Reference: Concepts, Second Edition

arithmetic:

SAS Operators in Expressions

SAS(R) 9.3 Language Reference: Concepts, Second Edition

Ron Fehd  knot logically lazy maven

Astounding
PROC Star

Yes, all the math will get performed.  You might get some savings by switching as you suggest:

if k=1 then ...;

else if k=2 then ...;

In addition, this code works faster if k=1 most of the time.  If k=2 most of the time, you could switch and test for k=2 first.  But truthfully, the savings will be tiny, perhaps not even measurable.  If you are interested in efficiency, you might post more of your code and get suggestions about how to speed it up.

Good luck.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 820 views
  • 6 likes
  • 4 in conversation