New Contributor
Posts: 4

The efficient difference of the IF-THEN/ELSE Statements and SELECT/WHEN

* WEB PAGE :
http://web.utk.edu/sas/OnlineTutor/1.2/en/60477/m83/m83_11.htm

"Using DO Groups Efficiently"

"Remember to use the following criteria when choosing which technique is
more efficient"

---------------------------------------------------------------------------
| IF-THEN/ELSE Statements | SELECT/WHEN Statements |
---------------------------------------------------------------------------
The number of conditions| few | many |

The distribution of a | not uniform | uniform |
variable's values

The type of variable | character | numeric |
----------------------------------------------------------------------------

I want to know the efficient difference of the IF-THEN/ELSE Statements and
SELECT/WHEN Statements

I tried to understand the efficient,but can't understand through SAS help.

I want to know the description and example for the difference.
Posts: 4,736

Re: The efficient difference of the IF-THEN/ELSE Statements and SELECT/WHEN

Hi

I believe from a performance point of view the difference between "if then else" and "select when" is rather neglectable.

The most important thing for both ways will be to order the cases descending according to probability (meaning: have the case which will be most often true first so all the other comparisons will be skipped).

Also from a performance perspective: Things like reducing data volumes as early as possible, minimizing passes through data / full table scans, avoiding unnecessary sorting and minimizing disk I/O operations are much more relevant.

HTH
Patrick
New Contributor
Posts: 4

Re: The efficient difference of the IF-THEN/ELSE Statements and SELECT/WHEN

Particularly, I want to know the efficient difference for the "The type of variable"
(Character and Numeric)
SAS Super FREQ
Posts: 9,368

Re: The efficient difference of the IF-THEN/ELSE Statements and SELECT/WHEN

Hi,
IF/THEN/ELSE and/or SELECT/WHEN are frequently used for TABLE lookups. So instead of focussing on only those techniques (which are really about the same in terms of processing), perhaps you should broaden your investigation into a discussion of table lookup efficiencies and overall efficiencies:
http://www2.sas.com/proceedings/forum2008/095-2008.pdf
http://www2.sas.com/proceedings/forum2007/042-2007.pdf
http://support.sas.com/resources/papers/sgf09/336-2009.pdf

cynthia
PROC Star
Posts: 8,164

Re: The efficient difference of the IF-THEN/ELSE Statements and SELECT/WHEN

Cynthia,

The same thread has been active on SAS-L the last couple of days. Nat Wooding, as part of that thread, did an interesting comparison regarding the numeric vs character difference ( see: http://www.listserv.uga.edu/cgi-bin/wa?A2=ind1106c&L=sas-l&D=1&O=D&P=14099).

Basically, if his tests were flawed, he showed that the difference does appear to be quite dramatic regarding character variables. In those cases, it appears that the if-then scenario fares much better.

Art
---------
> Hi,
> IF/THEN/ELSE and/or SELECT/WHEN are frequently
> used for TABLE lookups. So instead of focussing on
> only those techniques (which are really about the
> same in terms of processing), perhaps you should
> table lookup efficiencies and overall efficiencies:
> ttp://www2.sas.com/proceedings/forum2008/095-2008.pdf
> http://www2.sas.com/proceedings/forum2007/042-2007.pdf
>
> http://support.sas.com/resources/papers/sgf09/336-2009
> .pdf
>
> ynthia
SAS Super FREQ
Posts: 9,368

Re: The efficient difference of the IF-THEN/ELSE Statements and SELECT/WHEN

Hi:
Interesting results -- as with anything to do with efficiency -- it depends on your operating system, your data and your constraints, including programmer time for maintenance, etc. And whether you are optimizing for I/O, for CPU processing time, for clock time, for programmer maintenance time, or any number of factors. I always recommend that people run benchmarks with their data on their OS.

Nat provides a good example of pumping data through a program to generate benchmarking results. The next step would be to run a typical program using test data or a subset of the real data -- one with SELECT/WHEN and one with IF/THEN/ELSE or other TABLE lookup techniques (such as PROC FORMAT or hash table) to compare the performance of the real data to that of the benchmark with fake data.

cynthia
Super User
Posts: 10,784

Re: The efficient difference of the IF-THEN/ELSE Statements and SELECT/WHEN

I remembered that Arther.Carpenter has written a papa about this question before. He prefer to use select/when which is more efficient then if/else, Especially when you use expression in () of when not in select .Such as : select(); when(sex='F') .... when(sex='M')....
Certainly, Hope Art.C can write some words to explain this situation more.
Art.C is here now?

Ksharp
Valued Guide
Posts: 653

Re: The efficient difference of the IF-THEN/ELSE Statements and SELECT/WHEN

Thank you for the confidence Ksharp, but I suspect those may have been someone else's recommendations. Bob Virgle did a fair number of tests on these statements in his efficiencies book a number of years ago. And while I did write about the relative efficiencies in the narrow context of performing table look ups. I would defer to Cynthia's comment that you need to know your data, your machine, your situation. That said I find the differences between the comparisons on numerics and characters to be very interesting and probably worthy of more research.
New Contributor
Posts: 4

Re: The efficient difference of the IF-THEN/ELSE Statements and SELECT/WHEN

Thank you, your kind words !!

But, I don't know the fundamental reason.

1. Why are the diffrent for the Character and Numeric variable between IF/ELSE
and Select statement?

2. Why are the diffrent for The distribution of variable's values between IF/ELSE
and Select statement?

3. Why are the diffrent for the number of conditions between IF/ELSE
and Select statement? Message was edited by: kk760105
Discussion stats
• 8 replies
• 384 views
• 0 likes
• 6 in conversation