<?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: In Proc Sql statment, include SAS data to use as a primary key in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/In-Proc-Sql-statment-include-SAS-data-to-use-as-a-primary-key/m-p/459470#M70248</link>
    <description>There were literally tons of such inquiries on the forum, tip: do a search.&lt;BR /&gt;Hint: DBKEY.</description>
    <pubDate>Wed, 02 May 2018 19:26:43 GMT</pubDate>
    <dc:creator>LinusH</dc:creator>
    <dc:date>2018-05-02T19:26:43Z</dc:date>
    <item>
      <title>In Proc Sql statment, include SAS data to use as a primary key</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/In-Proc-Sql-statment-include-SAS-data-to-use-as-a-primary-key/m-p/459466#M70246</link>
      <description>&lt;P&gt;Hey SAS Community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to use Proc Sql to&amp;nbsp;select&amp;nbsp;data from a database. I have a SAS data set that contains the unique key for the data I want to select in the Proc Sql.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I include the SAS data, with the unique keys, in the Proc Sql statement so that I can pull only the data I need from the database?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've looked around for an answer to this already, but am new to SAS so excuse my ignorance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Wed, 02 May 2018 19:21:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/In-Proc-Sql-statment-include-SAS-data-to-use-as-a-primary-key/m-p/459466#M70246</guid>
      <dc:creator>lynagh18</dc:creator>
      <dc:date>2018-05-02T19:21:07Z</dc:date>
    </item>
    <item>
      <title>Re: In Proc Sql statment, include SAS data to use as a primary key</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/In-Proc-Sql-statment-include-SAS-data-to-use-as-a-primary-key/m-p/459468#M70247</link>
      <description>&lt;P&gt;How are you extracting data from your server? An example of the query may help.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The answer depends on how you're set up and partially on the size of the data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS can use data directly from multiple sources, though it can be inefficient at times.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select t1.*, t2.Description
from server.ServerData as t1
right join SASdata.SASdata as t2
on t1.id = t2.id;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/206085"&gt;@lynagh18&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hey SAS Community,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to use Proc Sql to&amp;nbsp;select&amp;nbsp;data from a database. I have a SAS data set that contains the unique key for the data I want to select in the Proc Sql.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I include the SAS data, with the unique keys, in the Proc Sql statement so that I can pull only the data I need from the database?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've looked around for an answer to this already, but am new to SAS so excuse my ignorance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 May 2018 19:25:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/In-Proc-Sql-statment-include-SAS-data-to-use-as-a-primary-key/m-p/459468#M70247</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-05-02T19:25:32Z</dc:date>
    </item>
    <item>
      <title>Re: In Proc Sql statment, include SAS data to use as a primary key</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/In-Proc-Sql-statment-include-SAS-data-to-use-as-a-primary-key/m-p/459470#M70248</link>
      <description>There were literally tons of such inquiries on the forum, tip: do a search.&lt;BR /&gt;Hint: DBKEY.</description>
      <pubDate>Wed, 02 May 2018 19:26:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/In-Proc-Sql-statment-include-SAS-data-to-use-as-a-primary-key/m-p/459470#M70248</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2018-05-02T19:26:43Z</dc:date>
    </item>
    <item>
      <title>Re: In Proc Sql statment, include SAS data to use as a primary key</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/In-Proc-Sql-statment-include-SAS-data-to-use-as-a-primary-key/m-p/459471#M70249</link>
      <description>&lt;P&gt;Assuming&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;datlib is the library defined for your database&lt;/LI&gt;
&lt;LI&gt;saslib is the library where your SAS dataset resides&lt;/LI&gt;
&lt;LI&gt;dbtable is the table in your database&lt;/LI&gt;
&lt;LI&gt;sastable is your dataset with the unique keys&lt;/LI&gt;
&lt;LI&gt;key is the name of the unique key&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select a.*
from datlib.dbtable a, saslib.sastable b
where a.key = b.key;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is the basic blueprint for a SQL inner join.&lt;/P&gt;</description>
      <pubDate>Wed, 02 May 2018 19:30:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/In-Proc-Sql-statment-include-SAS-data-to-use-as-a-primary-key/m-p/459471#M70249</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-05-02T19:30:44Z</dc:date>
    </item>
    <item>
      <title>Re: In Proc Sql statment, include SAS data to use as a primary key</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/In-Proc-Sql-statment-include-SAS-data-to-use-as-a-primary-key/m-p/459518#M70251</link>
      <description>&lt;P&gt;1. DBKEY is a good option if an index exists on the database side.&lt;/P&gt;
&lt;P&gt;Otherwise it can destroy&amp;nbsp;performance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2.&amp;nbsp;&lt;SPAN&gt;Another common option if you have a small number of values is&amp;nbsp;storing them in one or several macro variables which are used with the in() operator:&lt;BR /&gt;select unique VAL into :values ...&amp;nbsp; &amp;nbsp; /* you have to quote strings here */&amp;nbsp;&amp;nbsp;&lt;BR /&gt;select * from RDBMS.TAB where VAL in ( &amp;amp;values. )&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;3. A third option is to upload the list of values to the RDBMS as a table if you are authorised.&lt;/P&gt;</description>
      <pubDate>Thu, 03 May 2018 00:03:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/In-Proc-Sql-statment-include-SAS-data-to-use-as-a-primary-key/m-p/459518#M70251</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-05-03T00:03:23Z</dc:date>
    </item>
    <item>
      <title>Re: In Proc Sql statment, include SAS data to use as a primary key</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/In-Proc-Sql-statment-include-SAS-data-to-use-as-a-primary-key/m-p/459535#M70254</link>
      <description>&lt;P&gt;I never really trusted DBKEY as it seldom delivered on its promise. Don't forget it generates one query per lookup value.&lt;/P&gt;
&lt;P&gt;Here is a (not so quick it turns out) benchmark.&lt;/P&gt;
&lt;P&gt;Done on our&amp;nbsp;Teradata server; As usual, only valid for our data on our platform etc....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;4 methods used,&lt;/P&gt;
&lt;P&gt;- DBKEY is the worst (2 hours!!, and there is an index),&lt;/P&gt;
&lt;P&gt;- macro list or&amp;nbsp;MULTI_DATASRC_OPT are the fastest,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;- full download in between&lt;/P&gt;
&lt;P&gt;I am *not* allowed to upload, so can't test this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data LOOKUP; do I=1 to 2e3; ACCOUNT_ID=put(I,z10.); output; end ;  run;

%****** Macro list ;
proc sql noprint; 
  select unique quote(trim(ACCOUNT_ID)) into :values separated by ',' from LOOKUP;
  create table T1 as 
  select ACCOUNT_ID            
  from TERALIB.TAB       b 
  where ACCOUNT_ID  in(&amp;amp;values.) 
    and b.MONTH_KEY = '201712';
quit;
/* 
SELECT "ACCOUNT_ID","MONTH_KEY" FROM TERA."TAB"  WHERE  ( ( ("ACCOUNT_ID" IN  ( '0000000001' , '0000000002' , 
... 2000 values .....
'0000001995' , '0000001996' , '0000001997' , '0000001998' , '0000001999' , '0000002000' ) ) ) AND  ("MONTH_KEY" = '201712' ) )
NOTE: PROCEDURE SQL used (Total process time):
      real time           24.00 seconds
*/

%****** Full download;
proc sql;                       
  create table T2 as select b.ACCOUNT_ID 
  from LOOKUP a, TERALIB.TAB b
  where a.ACCOUNT_ID = b.ACCOUNT_ID
    and b.MONTH_KEY  = '201712';           
quit;
/* 
SELECT "ACCOUNT_ID","MONTH_KEY" FROM TERA."TAB"  WHERE  ("MONTH_KEY" = '201712' )
NOTE: PROCEDURE SQL used (Total process time):
      real time           2:57.98  
*/

%****** DBKEY= option;
proc sql;                       
  create table T3 as select b.ACCOUNT_ID 
  from LOOKUP a, TERALIB.TAB(dbkey=ACCOUNT_ID)  b 
  where a.ACCOUNT_ID = b.ACCOUNT_ID
    and b.MONTH_KEY  = '201712';    *2s;
quit;
/*
2000 queries:  USING ("ACCOUNT_ID" VARCHAR (22))SELECT "ACCOUNT_ID","MONTH_KEY" FROM TERA."TAB" WHERE "ACCOUNT_ID"=:"ACCOUNT_ID"
NOTE: PROCEDURE SQL used (Total process time):
      real time        02:03:47.07
*/

%****** MULTI_DATASRC_OPT= option;
proc sql;
  create view _V as
  select b.ACCOUNT_ID 
  from LOOKUP a, DBLIB.TAB b 
  where a.ACCOUNT_ID = b.ACCOUNT_ID
    and b.MONTH_KEY  = '201712' 
  using libname DBLIB teradata user=xxx .... multi_datasrc_opt=in_clause;
  create table T4 as select * from _V;
quit;
/*
NOTE: PROCEDURE SQL used (Total process time):
      real time           19.67 seconds
*/
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes there is&amp;nbsp;a primary&amp;nbsp;index on ACCOUNT_ID:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql; 
  connect using TERALIB;
  select * from connection to TERALIB (
    select count(*)
    from dbc.indices
    where tablename   = 'TAB'
      and columnname  in('MONTH_KEY', 'ACCOUNT_ID')
      and IndexType   in('P', 'Q') 
  );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;returns 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's also worth noting that the full download is automatically optimised by SAS to a query containing an in() clause if the LOOKUP table is smaller.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 May 2018 03:59:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/In-Proc-Sql-statment-include-SAS-data-to-use-as-a-primary-key/m-p/459535#M70254</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-05-03T03:59:19Z</dc:date>
    </item>
  </channel>
</rss>

