<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic IF THEN vs. SELECT WHEN --- which one is faster/better? in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/IF-THEN-vs-SELECT-WHEN-which-one-is-faster-better/m-p/74309#M21524</link>
    <description>in the DATA step, I have my conditional statements as IF THEN,&lt;BR /&gt;
&lt;BR /&gt;
But time is a practical concern for me, since the dataset is large, so I was wondering if SELECT WHEN tends to run faster than IF THEN.&lt;BR /&gt;
&lt;BR /&gt;
I've read different things, some posts said IF THEN is less efficient than SELECT WHEN and vice versa.&lt;BR /&gt;
&lt;BR /&gt;
So, I wanted to ask before I go ahead and change all my IF THEN's into SELECT WHEN's.&lt;BR /&gt;
&lt;BR /&gt;
And what makes people choose IF THEN or SELECT WHEN if one is more efficient than the other?&lt;BR /&gt;
&lt;BR /&gt;
Any suggestions from your experience? Thank you.</description>
    <pubDate>Thu, 23 Sep 2010 14:40:57 GMT</pubDate>
    <dc:creator>SAS_user_n</dc:creator>
    <dc:date>2010-09-23T14:40:57Z</dc:date>
    <item>
      <title>IF THEN vs. SELECT WHEN --- which one is faster/better?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/IF-THEN-vs-SELECT-WHEN-which-one-is-faster-better/m-p/74309#M21524</link>
      <description>in the DATA step, I have my conditional statements as IF THEN,&lt;BR /&gt;
&lt;BR /&gt;
But time is a practical concern for me, since the dataset is large, so I was wondering if SELECT WHEN tends to run faster than IF THEN.&lt;BR /&gt;
&lt;BR /&gt;
I've read different things, some posts said IF THEN is less efficient than SELECT WHEN and vice versa.&lt;BR /&gt;
&lt;BR /&gt;
So, I wanted to ask before I go ahead and change all my IF THEN's into SELECT WHEN's.&lt;BR /&gt;
&lt;BR /&gt;
And what makes people choose IF THEN or SELECT WHEN if one is more efficient than the other?&lt;BR /&gt;
&lt;BR /&gt;
Any suggestions from your experience? Thank you.</description>
      <pubDate>Thu, 23 Sep 2010 14:40:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/IF-THEN-vs-SELECT-WHEN-which-one-is-faster-better/m-p/74309#M21524</guid>
      <dc:creator>SAS_user_n</dc:creator>
      <dc:date>2010-09-23T14:40:57Z</dc:date>
    </item>
    <item>
      <title>Re: IF THEN vs. SELECT WHEN --- which one is faster/better?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/IF-THEN-vs-SELECT-WHEN-which-one-is-faster-better/m-p/74310#M21525</link>
      <description>It seems that you are asking two questions, performance and preference.&lt;BR /&gt;
&lt;BR /&gt;
Performance:  "your mileage may vary" and you will just need to test it for your situation.  My guess is that performance differences will be a function of the complexity of the SELECT &lt;EXPRESSION&gt; and the number of WHEN categories, as the &lt;EXPRESSION&gt; is complied just once for each SELECT and is compiled separately for each IF statement.  So, a SELECT with a NULL expression [e.g. SELECT () ] may well perform very similarly to an IF/THEN and a complex expression could well be faster.&lt;BR /&gt;
&lt;BR /&gt;
On preference, I tend to use IF statements for few levels of nesting or when the expression changes from one level to the next.  I find it easier to read.&lt;BR /&gt;
&lt;BR /&gt;
Doc Muhlbaier&lt;BR /&gt;
Duke&lt;/EXPRESSION&gt;&lt;/EXPRESSION&gt;</description>
      <pubDate>Thu, 23 Sep 2010 15:19:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/IF-THEN-vs-SELECT-WHEN-which-one-is-faster-better/m-p/74310#M21525</guid>
      <dc:creator>Doc_Duke</dc:creator>
      <dc:date>2010-09-23T15:19:28Z</dc:date>
    </item>
    <item>
      <title>Re: IF THEN vs. SELECT WHEN --- which one is faster/better?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/IF-THEN-vs-SELECT-WHEN-which-one-is-faster-better/m-p/74311#M21526</link>
      <description>The worst performer is the repeated IF... THEN... statements that decorate the code like an ugly wall-paper:&lt;BR /&gt;
[pre]&lt;BR /&gt;
  if a then x=1;&lt;BR /&gt;
  if b then x=2;&lt;BR /&gt;
  if c then x=3;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
As far as I know, there is no special compiler optimization going on for the SELECT statement, thus, the IF... THEN... ELSE IF... and SELECT... WHEN ... should perform equally well.&lt;BR /&gt;
&lt;BR /&gt;
You can speed up the execution a little bit by testing the most likely condition first and then least likely condition last.</description>
      <pubDate>Thu, 23 Sep 2010 18:14:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/IF-THEN-vs-SELECT-WHEN-which-one-is-faster-better/m-p/74311#M21526</guid>
      <dc:creator>chang_y_chung_hotmail_com</dc:creator>
      <dc:date>2010-09-23T18:14:49Z</dc:date>
    </item>
    <item>
      <title>Re: IF THEN vs. SELECT WHEN --- which one is faster/better?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/IF-THEN-vs-SELECT-WHEN-which-one-is-faster-better/m-p/74312#M21527</link>
      <description>probably more important than if then / else vs select when (I've done no testing comparing these) - but standard advice to optimize performance is to test the most common condition first, second most common second, etc.&lt;BR /&gt;
&lt;BR /&gt;
Easily seen using the reverse case - for a dataset with 10M rows, if the most common case is listed last in a sequence of 100 tests, then the most common case requires 100 * 10M tests. If listed first it requires 1 * 10M tests.</description>
      <pubDate>Thu, 23 Sep 2010 22:10:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/IF-THEN-vs-SELECT-WHEN-which-one-is-faster-better/m-p/74312#M21527</guid>
      <dc:creator>WaltSmith</dc:creator>
      <dc:date>2010-09-23T22:10:02Z</dc:date>
    </item>
    <item>
      <title>Re: IF THEN vs. SELECT WHEN --- which one is faster/better?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/IF-THEN-vs-SELECT-WHEN-which-one-is-faster-better/m-p/74313#M21528</link>
      <description>Hi:&lt;BR /&gt;
  Another possibility to test and benchmark, if you have only one variable/condition that you need to test with IF/SELECT statements is to use the PROC FORMAT lookup technique. Using the PUT statement with a user-defined format causes the lookup to be done in the format list. SAS uses a binary search technique to search the format list -- which can be efficient depending on the number of lookups and whether or not you list the most common condition first.&lt;BR /&gt;
 &lt;BR /&gt;
  This paper has some pros and cons for lookup techniques:&lt;BR /&gt;
&lt;A href="http://www2.sas.com/proceedings/forum2008/095-2008.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/forum2008/095-2008.pdf&lt;/A&gt;&lt;BR /&gt;
 &lt;BR /&gt;
cynthia&lt;BR /&gt;
[pre]proc format;&lt;BR /&gt;
  value newv 11, 12 = 'Yes'&lt;BR /&gt;
             13-15 = 'No'&lt;BR /&gt;
             16 = 'Maybe';&lt;BR /&gt;
run;&lt;BR /&gt;
                  &lt;BR /&gt;
data alt_approach;&lt;BR /&gt;
  length newflag $5;&lt;BR /&gt;
  set sashelp.class;&lt;BR /&gt;
  newflag = put(age,newv.);&lt;BR /&gt;
run;&lt;BR /&gt;
                &lt;BR /&gt;
ods html file='c:\temp\alt_if.html' style=sasweb;&lt;BR /&gt;
proc print data=alt_approach;&lt;BR /&gt;
  var name age newflag;&lt;BR /&gt;
run;&lt;BR /&gt;
ods html close;&lt;BR /&gt;
&lt;BR /&gt;
[/pre]</description>
      <pubDate>Fri, 24 Sep 2010 02:54:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/IF-THEN-vs-SELECT-WHEN-which-one-is-faster-better/m-p/74313#M21528</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2010-09-24T02:54:18Z</dc:date>
    </item>
    <item>
      <title>Re: IF THEN vs. SELECT WHEN --- which one is faster/better?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/IF-THEN-vs-SELECT-WHEN-which-one-is-faster-better/m-p/74314#M21529</link>
      <description>WaltSmith posted:&lt;BR /&gt;
&amp;gt;probably more important than if then / else vs select when (I've done no testing comparing these) - but standard advice to optimize performance is to test the most common condition first, second most common second, etc.&lt;BR /&gt;
&amp;gt;Easily seen using the reverse case - for a dataset with 10M rows, if the most common case is listed last in a sequence of 100 tests, then the most common case requires 100 * 10M tests. If listed first it requires 1 * 10M tests.&lt;BR /&gt;
...&lt;BR /&gt;
&lt;BR /&gt;
Hi, Walt, &lt;BR /&gt;
&lt;BR /&gt;
Your suggestion may have been true some time ago, but no longer. &lt;BR /&gt;
&lt;BR /&gt;
We don't have to argue about it. Below code shows that at least on my two year old PC, the CPU time saving by re-arranging the if conditions is not big enough to make the elapsed time difference at all, even when we force sas to load the input data on the memory.&lt;BR /&gt;
&lt;BR /&gt;
Notice that my input data set has 10M obs with only 2 vars, which is unrealistically conservative. In reality, there will be more variables and this will only increase the time SAS spends on I/O instead of evaluating logical expressions or branching.&lt;BR /&gt;
&lt;BR /&gt;
Conclusion? It does help to order your if conditions so that the most common cases listed first, but only a *little* bit, if any.&lt;BR /&gt;
&lt;BR /&gt;
(Code was too long for this forum: posted on sas-l:&lt;BR /&gt;
&lt;A href="http://www.listserv.uga.edu/cgi-bin/wa?A2=ind1009d&amp;amp;L=sas-l&amp;amp;D=1&amp;amp;O=D&amp;amp;P=18636)" target="_blank"&gt;http://www.listserv.uga.edu/cgi-bin/wa?A2=ind1009d&amp;amp;L=sas-l&amp;amp;D=1&amp;amp;O=D&amp;amp;P=18636)&lt;/A&gt;</description>
      <pubDate>Fri, 24 Sep 2010 17:58:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/IF-THEN-vs-SELECT-WHEN-which-one-is-faster-better/m-p/74314#M21529</guid>
      <dc:creator>chang_y_chung_hotmail_com</dc:creator>
      <dc:date>2010-09-24T17:58:46Z</dc:date>
    </item>
    <item>
      <title>Re: IF THEN vs. SELECT WHEN --- which one is faster/better?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/IF-THEN-vs-SELECT-WHEN-which-one-is-faster-better/m-p/74315#M21530</link>
      <description>I would agree that its going to help only a little bit - computers are very fast these days - but the question was about which is faster if then else vs select when - the difference between these options I suspect is going to be miniscule - greater savings (however small) will be obtained by ordering the test conditions.</description>
      <pubDate>Fri, 24 Sep 2010 18:09:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/IF-THEN-vs-SELECT-WHEN-which-one-is-faster-better/m-p/74315#M21530</guid>
      <dc:creator>WaltSmith</dc:creator>
      <dc:date>2010-09-24T18:09:17Z</dc:date>
    </item>
  </channel>
</rss>

