<?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: Sql union in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Sql-union/m-p/622273#M77268</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;how about subquery?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
input x 2.;
datalines;
10
20
;
run;

data b;
input x 2.;
datalines;
25 
;
run;

 

proc sql;
select sum(x) as tot
from
(
  select x
  from a

  union all

  select x
  from b
)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 04 Feb 2020 19:09:40 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2020-02-04T19:09:40Z</dc:date>
    <item>
      <title>Sql union</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Sql-union/m-p/622268#M77267</link>
      <description>&lt;P&gt;I'm trying to sum totals across datasets using proc sql.&amp;nbsp; &amp;nbsp;Is there away to avoid the third select statement (i.e. incorporating it in to the first two?)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data a;&lt;/P&gt;&lt;P&gt;input x 2.;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;10&lt;/P&gt;&lt;P&gt;20&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;data b;&lt;/P&gt;&lt;P&gt;input x 2.;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;25 ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table c as select sum(x) as tot&lt;/P&gt;&lt;P&gt;from a&lt;/P&gt;&lt;P&gt;union&lt;/P&gt;&lt;P&gt;select sum(x) as tot&lt;/P&gt;&lt;P&gt;from b;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;select sum(tot)&lt;/P&gt;&lt;P&gt;from c;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Feb 2020 18:54:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Sql-union/m-p/622268#M77267</guid>
      <dc:creator>Batman</dc:creator>
      <dc:date>2020-02-04T18:54:21Z</dc:date>
    </item>
    <item>
      <title>Re: Sql union</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Sql-union/m-p/622273#M77268</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;how about subquery?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
input x 2.;
datalines;
10
20
;
run;

data b;
input x 2.;
datalines;
25 
;
run;

 

proc sql;
select sum(x) as tot
from
(
  select x
  from a

  union all

  select x
  from b
)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Feb 2020 19:09:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Sql-union/m-p/622273#M77268</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-02-04T19:09:40Z</dc:date>
    </item>
    <item>
      <title>Re: Sql union</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Sql-union/m-p/622274#M77269</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/9248"&gt;@Batman&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can do this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc sql;
Create table c as 
Select sum(x) as tot
From (select x from a union all select x from b);
Quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;it is important to specify « all » to avoid to remove duplicate records from input tables&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Feb 2020 19:15:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Sql-union/m-p/622274#M77269</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-02-04T19:15:20Z</dc:date>
    </item>
    <item>
      <title>Re: Sql union</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Sql-union/m-p/622298#M77270</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/9248"&gt;@Batman&lt;/a&gt;&amp;nbsp; I am afraid there is some glitch in either&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;1. my comprehension of the requirement&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;2. Or the fix&lt;/P&gt;
&lt;P&gt;&amp;nbsp;3. or the requirement itself&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My understanding from what you wrote is&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. You have 2 queries that involves a SET operator.&lt;/P&gt;
&lt;P&gt;2. It appears the independent queries compute an independent sum of the the values of x. This would perfectly yield 1 record for each select clause i.e one for a A and two for B with two independent grand_totals&lt;/P&gt;
&lt;P&gt;3. At this point, you merely want to compute the grand_total of the two grand_totals, or in other words sum of the 2 sums.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the real and meaningful fix doesn't warrant an UNION &lt;STRIKE&gt;ALL &lt;/STRIKE&gt;rather your existing query is indeed not wrong.&amp;nbsp; The fix is actually to get your two SELECT clauses that are combined using a SET operator in a subquery as mentioned by genius&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;for the reason the SQL processor needs a copy aka akin to FROM table/view to process the sum=&amp;gt;sum (of the sums).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course doing one combined sum from an appended copy usng UNION ALL is essentially the right way albeit the slight tweak that is required&amp;nbsp; in your existing query to make it work&amp;nbsp;after all is&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select sum(x) as tot

from a

union

select sum(x) as tot

from b

/*to have the above as SUBQUERY below*/

proc sql;

create table c as 
select sum(tot) as g_tot
from
(select sum(x) as tot

from a

union

select sum(x) as tot

from b);
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;I hope this clears the crack for others who perhaps weren't clear initially like me&lt;/P&gt;</description>
      <pubDate>Tue, 04 Feb 2020 20:43:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Sql-union/m-p/622298#M77270</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-02-04T20:43:01Z</dc:date>
    </item>
    <item>
      <title>Re: Sql union</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Sql-union/m-p/622384#M77277</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your post got me thinking about performance (thanks 1e6 for the inspiration!)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, what I did:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a b c d;
  do _N_ = 1 to 1e8;
    x = 1;
    output;
  end; 
run;

options fullstimer msglevel=i;
resetline;
options ps=max ls=max;
proc sql feedback _method _tree;
select sum(tot) as tot
from
(
  select sum(x) as tot
  from a

  union all 

  select sum(x) as tot
  from b
)
;
quit;

proc sql feedback _method _tree;
select sum(x) as tot
from
(
  select x
  from c

  union all

  select x
  from d
)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Your idea of doing sum() in subquery ran half the time mine idea did:&lt;/P&gt;&lt;PRE&gt;71   data a b c d;
72     do _N_ = 1 to 1e8;
73       x = 1;
74       output;
75     end;
76   run;

NOTE: The data set WORK.A has 100000000 observations and 1 variables.
NOTE: The data set WORK.B has 100000000 observations and 1 variables.
NOTE: The data set WORK.C has 100000000 observations and 1 variables.
NOTE: The data set WORK.D has 100000000 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           19.85 seconds
      cpu time            19.78 seconds

NOTE: Statement transforms to:

        select SUM(tot) as tot
          from ( select SUM(A.x) as tot
                   from WORK.A
               )
                 union all
               ( select SUM(B.x) as tot
                   from WORK.B
               ));


NOTE: SQL execution methods chosen are:

      sqxslct
          sqxsumn
              sqxuall
                  sqxsumn
                      sqxsrc( WORK.A )
                  sqxsumn
                      sqxsrc( WORK.B )

NOTE: PROCEDURE SQL used (Total process time):
      real time           15.98 seconds
      user cpu time       14.34 seconds
      system cpu time     1.60 seconds
      memory              5649.09k
      OS Memory           27384.00k


NOTE: Statement transforms to:

        select SUM(x) as tot
          from ( select C.x
                   from WORK.C
               )
                 union all
               ( select D.x
                   from WORK.D
               ));


NOTE: SQL execution methods chosen are:

      sqxslct
          sqxsumn
              sqxuall
                  sqxsrc( WORK.C )
                  sqxsrc( WORK.D )

NOTE: PROCEDURE SQL used (Total process time):
      real time           31.27 seconds
      user cpu time       29.82 seconds
      system cpu time     1.54 seconds
      memory              5622.53k
      OS Memory           27384.00k&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But I think we need `union all` in any case. There is one data setup which cause it:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
input x 2.;
datalines;
10
20
;
run;

data b;
input x 2.;
datalines;
25
5 
;
run;

proc sql;
select sum(x) as tot
from a

union /* ALL */

select sum(x) as tot
from b
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Without `all` we will get 30, but we would like to have 60.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All the best&lt;/P&gt;&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Wed, 05 Feb 2020 08:36:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Sql-union/m-p/622384#M77277</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-02-05T08:36:41Z</dc:date>
    </item>
    <item>
      <title>Re: Sql union</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Sql-union/m-p/622420#M77278</link>
      <description>&lt;P&gt;Good morning Cop&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp; Great catch. I didn't think of identical sum that caused the elimination of dup. Yes, UNION ALL will be required in cases where have sum(of identical sums).&amp;nbsp; Indeed union all is essentially the right way of doing it. I agree.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can't thank you enough for your time and details. Much appreciate it bro!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Okay, that being said one funny workaround(&lt;EM&gt;turn on your sense of humor&lt;/EM&gt;) with a dsn label making it unique&amp;nbsp;&lt;img id="smileywink" class="emoticon emoticon-smileywink" src="https://communities.sas.com/i/smilies/16x16_smiley-wink.png" alt="Smiley Wink" title="Smiley Wink" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
input x 2.;
datalines;
10
20
;
run;

data b;
input x 2.;
datalines;
25
5 
;
run;

proc sql;
select sum(tot) as grand_tot
from
(select 'a' as dsn,sum(x) as tot
from a

union 

select 'b' as dsn, sum(x) as tot
from b)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure SQL: Query Results" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r b header" scope="col"&gt;grand_tot&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;60&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Wed, 05 Feb 2020 12:40:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Sql-union/m-p/622420#M77278</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-02-05T12:40:59Z</dc:date>
    </item>
    <item>
      <title>Re: Sql union</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Sql-union/m-p/622429#M77279</link>
      <description>lovely &lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt; &lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt;</description>
      <pubDate>Wed, 05 Feb 2020 13:00:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Sql-union/m-p/622429#M77279</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-02-05T13:00:37Z</dc:date>
    </item>
  </channel>
</rss>

