<?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: SAS SQL Code Efficiency in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-SQL-Code-Efficiency/m-p/325888#M72528</link>
    <description>^ Thanks a ton &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
    <pubDate>Thu, 19 Jan 2017 06:31:44 GMT</pubDate>
    <dc:creator>gargi01</dc:creator>
    <dc:date>2017-01-19T06:31:44Z</dc:date>
    <item>
      <title>SAS SQL Code Efficiency</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-SQL-Code-Efficiency/m-p/325878#M72523</link>
      <description>&lt;P&gt;Could someone please help me with how SAS/SQL processes nested queries or subqueries. Would it do the inner select, "select sum(price) from MasterFile" only once or with each iteration of the outer query?&lt;/P&gt;&lt;PRE class="lang-sql prettyprint prettyprinted"&gt;proc sql; 
create table categorySpend as 
select categoryid, sum (price) as CategoryRevenue, (select sum(price) from MasterFile)as TotalRevenue 
from MasterFile 
group by categoryid; 
quit;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 05:29:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-SQL-Code-Efficiency/m-p/325878#M72523</guid>
      <dc:creator>gargi01</dc:creator>
      <dc:date>2017-01-19T05:29:21Z</dc:date>
    </item>
    <item>
      <title>Re: SAS SQL Code Efficiency</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-SQL-Code-Efficiency/m-p/325885#M72527</link>
      <description>&lt;P&gt;Good question!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a test I just ran:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data masterfile;
  input categoryid price;
  do i=1 to 1000000;
    output;
  end;
cards;
1 10
2 20
3 30
;

proc sql; 
  select categoryid, 
         sum (price) as CategoryRevenue,
         (select sum(price) from MasterFile)as TotalRevenue 
    from MasterFile 
      group by categoryid
  ; 
quit;

proc sql; 
  select categoryid, 
         sum (price) as CategoryRevenue
    from MasterFile 
      group by categoryid
  ; 
  select sum(price) as TotalRevenue
    from MasterFile
  ;
quit;&lt;/PRE&gt;
&lt;P&gt;Both sets of code select the same things. When I first ran the test it appeared that the embedded select ran for each subgroup. However, when I set up the code to run about 100 times, on average both sets took the same time to run.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As such, I'd have to guess that it is only run once.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HTH,&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 06:11:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-SQL-Code-Efficiency/m-p/325885#M72527</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-01-19T06:11:34Z</dc:date>
    </item>
    <item>
      <title>Re: SAS SQL Code Efficiency</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-SQL-Code-Efficiency/m-p/325888#M72528</link>
      <description>^ Thanks a ton &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Thu, 19 Jan 2017 06:31:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-SQL-Code-Efficiency/m-p/325888#M72528</guid>
      <dc:creator>gargi01</dc:creator>
      <dc:date>2017-01-19T06:31:44Z</dc:date>
    </item>
    <item>
      <title>Re: SAS SQL Code Efficiency</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-SQL-Code-Efficiency/m-p/325908#M72538</link>
      <description>&lt;P&gt;Just to add: the probably fastest method might be this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=masterfile;
var price;
class categoryid;
output out=sum1 (drop=_type_ _freq_ rename=(price=categoryrevenue)) sum=;
run;

data sum2;
set sum1;
retain totalrevenue;
if categoryid = .
then do;
  totalrevenue = categoryrevenue;
  delete;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As this really does only one pass through the original dataset.&lt;/P&gt;
&lt;P&gt;With 10 times as many observations as used by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt;, these steps outperformed the SQL 8:16 seconds to 44.53 seconds on my 2-CPU IBM p520.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 08:47:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-SQL-Code-Efficiency/m-p/325908#M72538</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-01-19T08:47:07Z</dc:date>
    </item>
  </channel>
</rss>

