<?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: How to convert SQL to SAS code efficiently in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-convert-SQL-to-SAS-code-efficiently/m-p/719608#M38172</link>
    <description>&lt;P&gt;Regarding the "if 0 then set" stuff: What&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;said.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regaring loading the big data into the hash object: In this case, I agree that the smart thing is to read the small data into the hash and read the big data sequentially using the Set Statement. However, there could be cases where it is the other way around.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Btw, the reverse logic looks like this. You can compare the run times yourself &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2(drop = ForeignId rc);

   if _N_ = 1 then do;
      dcl hash h(dataset : "bigTable");
      h.definekey("ForeignId");
      h.definedata("Field1", "Field2", "Field100");
      h.definedone();
   end;

   set smallTable;
   if 0 then set bigTable(keep = ForeignId Field1 Field2 Field100);

   rc = h.find(key : PrimaryId);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 16 Feb 2021 11:44:01 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2021-02-16T11:44:01Z</dc:date>
    <item>
      <title>How to convert SQL to SAS code efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-convert-SQL-to-SAS-code-efficiently/m-p/719392#M38162</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'd like to convert the following SQL to SAS efficiently.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have two datasets. smallTable (1000 rows, 1 Col), bigTable (10,000,000 rows 100 cols). I want to merge these into one dataset. The SQL would be...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;create table usefulStuff as
    select smallTable.PrimaryId, bigTable.Field1, bigTable.Field2, bigTable.FieldN
    from smallTable left join bigTable on smallTable.PrimaryId = bigTable.ForeignId&lt;BR /&gt;    order by smallTable.PrimaryId&lt;/PRE&gt;
&lt;P&gt;At the moment the tables are not sorted on the IDs. Therefore my understanding is that I need to first sort each table on the ID and rename the ID fields to the same name. Then I can do a Merge selecting only records in bigTable where there is one in smallTable. The sorting on bigTable seems very inefficient unless I can sort it using a where clause that says something like &lt;EM&gt;&lt;STRONG&gt;Where ForeignId in (select PrimaryId from smallTable)&lt;/STRONG&gt;&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The reason for doing this is that we have multiple records which in standard SQL can only be removed using group by and either max() or min(). This isn't what I want. I want to be able to select &lt;EM&gt;&lt;STRONG&gt;first.smallTable&lt;/STRONG&gt;&lt;/EM&gt; or &lt;EM&gt;&lt;STRONG&gt;last.smallTable&lt;/STRONG&gt;&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks for all constructive suggestions. - Martin&lt;/P&gt;</description>
      <pubDate>Mon, 15 Feb 2021 17:31:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-convert-SQL-to-SAS-code-efficiently/m-p/719392#M38162</guid>
      <dc:creator>Martin_Bryant</dc:creator>
      <dc:date>2021-02-15T17:31:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert SQL to SAS code efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-convert-SQL-to-SAS-code-efficiently/m-p/719395#M38163</link>
      <description>&lt;P&gt;Do you want all 100 columns or just 1, 2 and N?&lt;/P&gt;</description>
      <pubDate>Mon, 15 Feb 2021 17:34:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-convert-SQL-to-SAS-code-efficiently/m-p/719395#M38163</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-02-15T17:34:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert SQL to SAS code efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-convert-SQL-to-SAS-code-efficiently/m-p/719397#M38164</link>
      <description>I would like to know a good method to do this in multiple situations. For this specific example, let's say that I want about 10 columns.</description>
      <pubDate>Mon, 15 Feb 2021 17:37:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-convert-SQL-to-SAS-code-efficiently/m-p/719397#M38164</guid>
      <dc:creator>Martin_Bryant</dc:creator>
      <dc:date>2021-02-15T17:37:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert SQL to SAS code efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-convert-SQL-to-SAS-code-efficiently/m-p/719425#M38166</link>
      <description>&lt;P&gt;First, let us create some data that replicates your actual data. smallTable below has 1000 obs and 1 variable (PrimaryID) and bigTable has 101 cols: ForeignID, field1, field2, ..., field100. I assume that the ID variables are character, but it will work with numeric as well.&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data smallTable(rename = id = PrimaryId keep = id       )
     bigTable  (rename = id = ForeignId keep = id field:);
   do i = 1 to 1e7;
      id = uuidgen();
      array f field1 - field100;
      do over f;
         f = ceil(rand('uniform') * 10);
      end;
      output bigTable;
      if mod(i, 1e4) = 0 then output smallTable;
   end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;
&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;Next, I try to replicate your SQL code. I do not use Sorting since you mention a time heavy sort procedure. Instead I use hashing. This takes about 15 sec on my system.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let me know if this works for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop = PrimaryId);

   if _N_ = 1 then do;
      dcl hash h(dataset : "smallTable", hashexp : 20);
      h.definekey("PrimaryId");
      h.definedone();
   end;

   set bigTable(keep = ForeignId Field1 Field2 Field100);
   if 0 then set smallTable;

   if h.check(key : ForeignId) = 0;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Feb 2021 18:20:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-convert-SQL-to-SAS-code-efficiently/m-p/719425#M38166</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-02-15T18:20:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert SQL to SAS code efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-convert-SQL-to-SAS-code-efficiently/m-p/719558#M38167</link>
      <description>&lt;P&gt;For lookups like this, I also strongly recommend the hash object approach, as it will outperform any other method and does not need any sort of the large table.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2021 09:15:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-convert-SQL-to-SAS-code-efficiently/m-p/719558#M38167</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-02-16T09:15:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert SQL to SAS code efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-convert-SQL-to-SAS-code-efficiently/m-p/719575#M38168</link>
      <description>&lt;P&gt;Thanks, that looks like something that I really want to understand fully. A question that I can't find in the docs.&amp;nbsp;What does the zero in &lt;EM&gt;&lt;STRONG&gt;if 0 then set smallTable;&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp;refer to?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, having read the docs on hashing, it suggests that I load the large dataset into the hash table rather than the small one. What are your thoughts on this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for the help. Greatly appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2021 10:25:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-convert-SQL-to-SAS-code-efficiently/m-p/719575#M38168</guid>
      <dc:creator>Martin_Bryant</dc:creator>
      <dc:date>2021-02-16T10:25:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert SQL to SAS code efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-convert-SQL-to-SAS-code-efficiently/m-p/719578#M38169</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 0 then set smallTable;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since 0 is false, this statement is never &lt;EM&gt;executed&lt;/EM&gt;, but its &lt;EM&gt;declarative&lt;/EM&gt; element is recognized by the data step compiler, so it reads the dataset's metadata and prepares its columns in the PDV.&lt;/P&gt;
&lt;P&gt;This makes it unnecessary to define the variables "manually" by using a LENGTH statement (just naming the variables in the DEFINEKEY and/or DEFINEDATA methods is not sufficient to declare them correctly).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The PDV is set up at data step compile time, but the hash object is created entirely at runtime, so the data step compiler does not have an idea what the hash methods will do.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2021 10:31:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-convert-SQL-to-SAS-code-efficiently/m-p/719578#M38169</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-02-16T10:31:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert SQL to SAS code efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-convert-SQL-to-SAS-code-efficiently/m-p/719584#M38170</link>
      <description>&lt;P&gt;And you definitely want to load the small table into the hash. Put the lookup into memory, and process the large table sequentially.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2021 10:39:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-convert-SQL-to-SAS-code-efficiently/m-p/719584#M38170</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-02-16T10:39:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert SQL to SAS code efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-convert-SQL-to-SAS-code-efficiently/m-p/719600#M38171</link>
      <description>&lt;P&gt;Thanks. Found out what a PDV is and why you do the &lt;EM&gt;&lt;STRONG&gt;if 0&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp;. Looks good. Will try that solution.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2021 11:16:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-convert-SQL-to-SAS-code-efficiently/m-p/719600#M38171</guid>
      <dc:creator>Martin_Bryant</dc:creator>
      <dc:date>2021-02-16T11:16:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert SQL to SAS code efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-convert-SQL-to-SAS-code-efficiently/m-p/719608#M38172</link>
      <description>&lt;P&gt;Regarding the "if 0 then set" stuff: What&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;said.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regaring loading the big data into the hash object: In this case, I agree that the smart thing is to read the small data into the hash and read the big data sequentially using the Set Statement. However, there could be cases where it is the other way around.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Btw, the reverse logic looks like this. You can compare the run times yourself &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2(drop = ForeignId rc);

   if _N_ = 1 then do;
      dcl hash h(dataset : "bigTable");
      h.definekey("ForeignId");
      h.definedata("Field1", "Field2", "Field100");
      h.definedone();
   end;

   set smallTable;
   if 0 then set bigTable(keep = ForeignId Field1 Field2 Field100);

   rc = h.find(key : PrimaryId);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Feb 2021 11:44:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-convert-SQL-to-SAS-code-efficiently/m-p/719608#M38172</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-02-16T11:44:01Z</dc:date>
    </item>
  </channel>
</rss>

