<?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 Re: Proc SQL is not efficient with OR operator in where clause? in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-is-not-efficient-with-OR-operator-in-where-clause/m-p/108927#M30342</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm not an SQL expert, but does this program give you the right result (no matter how long it takes to run)?&amp;nbsp; Doesn't this join every Trans_Type="O" record with every record from the lookup table?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 30 Aug 2012 17:57:36 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2012-08-30T17:57:36Z</dc:date>
    <item>
      <title>Proc SQL is not efficient with OR operator in where clause?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-is-not-efficient-with-OR-operator-in-where-clause/m-p/108926#M30341</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-size: 14px; text-align: -webkit-auto; background-color: #ffffff;"&gt;Hi, &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-size: 14px; text-align: -webkit-auto; background-color: #ffffff;"&gt;I have a huge table (Main) which contains millions of records and so many variables like Account,Token and Trans_Type etc. There is an another table (Lookup ) which contains only two variables - Account and Tokens. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-size: 14px; text-align: -webkit-auto; background-color: #ffffff;"&gt;Now I have to make a final table which will be based on below condition- &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-size: 14px; text-align: -webkit-auto; background-color: #ffffff;"&gt;1) Corresponding Token from Lookup where substr(Account,15,1)= "2" or Corresponding Token from Main table where Trans_Type = "O". &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-size: 14px; text-align: -webkit-auto; background-color: #ffffff;"&gt;I wrote this code - &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-size: 14px; text-align: -webkit-auto; background-color: #ffffff;"&gt;proc sql; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-size: 14px; text-align: -webkit-auto; background-color: #ffffff;"&gt;create table Final as &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-size: 14px; text-align: -webkit-auto; background-color: #ffffff;"&gt;select a.*, b.* &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-size: 14px; text-align: -webkit-auto; background-color: #ffffff;"&gt;from Main as a Lookup as b &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-size: 14px; text-align: -webkit-auto; background-color: #ffffff;"&gt;where a.token = b.token | a.Trans_Type = "O" ; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-size: 14px; text-align: -webkit-auto; background-color: #ffffff;"&gt;quit; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-size: 14px; text-align: -webkit-auto; background-color: #ffffff;"&gt;Actually I need to join the table because going forward I have to remove the account number column and the corresponding Token values from Lookup Table will be added in the Token Column in Main table. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-size: 14px; text-align: -webkit-auto; background-color: #ffffff;"&gt;That's why I am first putting Where substr(Account,15,1)="2" in the Lookup table to get the corresponding Token from the Lookup table. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-size: 14px; text-align: -webkit-auto; background-color: #ffffff;"&gt;Now I have to join both the table where all the Tokens from Lookup table or those token where Trans_Type ="O" in the Main table. So this will like Full Join not left join because of OR condition. So if we execute the full join on huge table size will be increased. So need to search the alternate method. Please let me know any other way except Hash Join. Because demerit with Hash join is it takes lot of memory.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background-color: #ffffff; color: #000000; font-size: 14px; text-align: -webkit-auto;"&gt;As Lookup table is index created on both the column and Main table is also indexed on Account, Token, Trans_Type because Main table is huge table. So this query eventually taking 5-6 hrs of time and also taking the huge memory. I need to reduce the time while extracting the data.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Aug 2012 16:57:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-is-not-efficient-with-OR-operator-in-where-clause/m-p/108926#M30341</guid>
      <dc:creator>SudhirU</dc:creator>
      <dc:date>2012-08-30T16:57:29Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL is not efficient with OR operator in where clause?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-is-not-efficient-with-OR-operator-in-where-clause/m-p/108927#M30342</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm not an SQL expert, but does this program give you the right result (no matter how long it takes to run)?&amp;nbsp; Doesn't this join every Trans_Type="O" record with every record from the lookup table?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Aug 2012 17:57:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-is-not-efficient-with-OR-operator-in-where-clause/m-p/108927#M30342</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2012-08-30T17:57:36Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL is not efficient with OR operator in where clause?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-is-not-efficient-with-OR-operator-in-where-clause/m-p/108928#M30343</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;1. You say you need a join but one isn't specified, so by default its a cross join which is inefficient.&lt;/P&gt;&lt;P&gt;2. You show a restriction that doesn't appear in the code: &lt;SPAN style="color: #000000; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 14px; text-align: -webkit-auto; background-color: #ffffff;"&gt;substr(Account,15,1)= "2"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 14px; text-align: -webkit-auto; background-color: #ffffff;"&gt;3. the tables are not separated by a , so the code included would probably return an error (not 100% sure)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 14px; text-align: -webkit-auto; background-color: #ffffff;"&gt;4. where are your tables? is the main in a server and lookup on your computer or are both in a server or your work library?&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Aug 2012 18:19:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-is-not-efficient-with-OR-operator-in-where-clause/m-p/108928#M30343</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2012-08-30T18:19:41Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL is not efficient with OR operator in where clause?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-is-not-efficient-with-OR-operator-in-where-clause/m-p/108929#M30344</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;My guess is that you are trying to join the tables on the account field and then set up some decision rules based on values of other fields. If my understanding of your need is correct, then I think this or something like this is what you want:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; create table Final as&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a.*,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; b.token as lookup_token,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; when substr(Account,15,1)= "2" then b.token&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; when Trans_Type = "O" then a.token&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end as final_token&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Main as a,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Lookup as b&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a.account = b.account;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Aug 2012 19:25:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-is-not-efficient-with-OR-operator-in-where-clause/m-p/108929#M30344</guid>
      <dc:creator>tish</dc:creator>
      <dc:date>2012-08-30T19:25:36Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL is not efficient with OR operator in where clause?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-is-not-efficient-with-OR-operator-in-where-clause/m-p/108930#M30345</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Regarding the efficiency aspect of your question, I work with data sets with hundreds of millions of records. For me, PROC SQL is much more time efficient than DATA step programming. I can usually cut through a file in a few minutes. One trick I apply is that I will use a keep= dataset option to limit the fields available on input. So, in the above example, 'd have "from Main (keep=account trans_type token) as a, Lookup as b". This speeds processing tremendously.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I hope that this note or my other comment helps.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Aug 2012 21:18:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-is-not-efficient-with-OR-operator-in-where-clause/m-p/108930#M30345</guid>
      <dc:creator>tish</dc:creator>
      <dc:date>2012-08-30T21:18:40Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL is not efficient with OR operator in where clause?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-is-not-efficient-with-OR-operator-in-where-clause/m-p/108931#M30346</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Are these SAS tables or what is the database. If it's SAS tables then a simple data step with a hash object&amp;nbsp;&amp;nbsp; (containing the lookup table) could be very efficient. How big is the lookup table (only needed rows and columns)?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I agree with tish that your SQL and your narrative don't add up. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Aug 2012 22:10:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-is-not-efficient-with-OR-operator-in-where-clause/m-p/108931#M30346</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2012-08-30T22:10:33Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL is not efficient with OR operator in where clause?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-is-not-efficient-with-OR-operator-in-where-clause/m-p/108932#M30347</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Your query will not return the desired result because every observation in Lookup will match every record in Main where Trans_type="O". You can avoid the inefficient OR condition with a UNION query such as :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt;proc sql; &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt;create table Final as &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select A.*, B.account&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from Main as A inner join Lookup as B on A.token = B.token&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where A.Trans_Type ne "O"&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt;UNION ALL&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select A.*, B.account&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from Main as A left join Lookup as B on A.token = B.token&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where A.Trans_Type = "O";&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-size: 12pt; font-family: calibri, verdana, arial, sans-serif;"&gt;quit;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This will pull out every Main record that matches a Lookup observation plus every Main record that has Trans_Type="O". That seems to be what you want.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PG&lt;/P&gt;&lt;P&gt;&lt;BR /&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 31 Aug 2012 00:38:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-SQL-is-not-efficient-with-OR-operator-in-where-clause/m-p/108932#M30347</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2012-08-31T00:38:14Z</dc:date>
    </item>
  </channel>
</rss>

