<?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: Summarize variables on PROC SQL UNION? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Summarize-variables-on-PROC-SQL-UNION/m-p/760411#M240455</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19745"&gt;@G_I_Jeff&lt;/a&gt;&amp;nbsp;Below two options with different result. Want1 first concatenates (union corr) the tables and then aggregates, Want2 first aggregates and then concatenates (union corr) the table. The usual case is Want1 but I've added Want2 to showcase what the logic does&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/335473"&gt;@HarrySnart&lt;/a&gt;&amp;nbsp;proposed.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Create sample data*/
data have1;
  do key=1,2,3;
    var=key;
    output;
  end;
  stop;
run;
data have2;
  do key=2,3,4;
    var=key;
    output;
    output;
  end;
  stop;
run;

/*Union both tables including duplicates and summation*/
proc sql;
  create table want1 as    
    select key, sum(var) as sum_var
    from
      ( 
        select key, var                                                 
        from work.have1 
        union all                     
        select key, var                                           
        from work.have2   
      ) 
    group by key
    ;
quit;

/*Union both tables including duplicates and summation*/
proc sql;
  create table want2 as  
    ( 
      select key, sum(var) as sum_var                                                 
      from work.have1 
      group by key
    )
    union all    
    ( 
      select key, sum(var) as sum_var                                           
      from work.have2   
      group by key
    )
    ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 09 Aug 2021 16:24:42 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2021-08-09T16:24:42Z</dc:date>
    <item>
      <title>Summarize variables on PROC SQL UNION?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summarize-variables-on-PROC-SQL-UNION/m-p/760377#M240442</link>
      <description>&lt;P&gt;Need to concatenate duplicate tables but also need to summarize variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;                                                    
 create table user.nfctape as                                
  select ident,                                              
         system,                                             
         account,                                            
         start_date,                                         
         end_date,                                           
         sum(ddsusize) as size format 20.5,                  
         hlq                                                 
  from user.plex_init union corresponding                    
  select ident,                                              
         system,                                             
         account,                                            
         start_date,                                         
         end_date,                                           
         sum(ddsusize) as size format 20.5,                  
         hlq                                                 
  from user.test_init                                        
  group by ident, system, account, start_date, end_date, hlq;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is this even possible in one statement? This code runs but the summarization from first table doesn't take account the GROUP BY statement.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Aug 2021 14:24:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summarize-variables-on-PROC-SQL-UNION/m-p/760377#M240442</guid>
      <dc:creator>G_I_Jeff</dc:creator>
      <dc:date>2021-08-09T14:24:54Z</dc:date>
    </item>
    <item>
      <title>Re: Summarize variables on PROC SQL UNION?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summarize-variables-on-PROC-SQL-UNION/m-p/760402#M240451</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19745"&gt;@G_I_Jeff&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You're trying to union two queries, so I think both should have the group by. Also, if I understand your question, if you are concatenating duplicate records and want to retain those you should look at UNION ALL.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've tried to re-create your code using the cars dataset twice - it adds all duplicates and the sum works with the respective GROUP BY statements.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*Create Dataset1;
data cars1;
set sashelp.cars;
run;

*Create Dataset2;
data cars2;
set sashelp.cars;
run;

*Union both tables including duplicates and summation;
proc sql;                                                    
 create table work.test_union as                                
  (select Make,	Type,	Origin,	DriveTrain,	sum(MSRP)                                                 
  from work.cars1 group by make,type,origin,drivetrain)
 union all                     
  (select Make,	Type,	Origin,	DriveTrain,	sum(MSRP)                                             
  from work.cars2                                        
  group by make,type,origin,drivetrain);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Does this answer your question?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;Harry&lt;/P&gt;</description>
      <pubDate>Mon, 09 Aug 2021 15:52:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summarize-variables-on-PROC-SQL-UNION/m-p/760402#M240451</guid>
      <dc:creator>HarrySnart</dc:creator>
      <dc:date>2021-08-09T15:52:52Z</dc:date>
    </item>
    <item>
      <title>Re: Summarize variables on PROC SQL UNION?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summarize-variables-on-PROC-SQL-UNION/m-p/760411#M240455</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19745"&gt;@G_I_Jeff&lt;/a&gt;&amp;nbsp;Below two options with different result. Want1 first concatenates (union corr) the tables and then aggregates, Want2 first aggregates and then concatenates (union corr) the table. The usual case is Want1 but I've added Want2 to showcase what the logic does&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/335473"&gt;@HarrySnart&lt;/a&gt;&amp;nbsp;proposed.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Create sample data*/
data have1;
  do key=1,2,3;
    var=key;
    output;
  end;
  stop;
run;
data have2;
  do key=2,3,4;
    var=key;
    output;
    output;
  end;
  stop;
run;

/*Union both tables including duplicates and summation*/
proc sql;
  create table want1 as    
    select key, sum(var) as sum_var
    from
      ( 
        select key, var                                                 
        from work.have1 
        union all                     
        select key, var                                           
        from work.have2   
      ) 
    group by key
    ;
quit;

/*Union both tables including duplicates and summation*/
proc sql;
  create table want2 as  
    ( 
      select key, sum(var) as sum_var                                                 
      from work.have1 
      group by key
    )
    union all    
    ( 
      select key, sum(var) as sum_var                                           
      from work.have2   
      group by key
    )
    ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Aug 2021 16:24:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summarize-variables-on-PROC-SQL-UNION/m-p/760411#M240455</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-08-09T16:24:42Z</dc:date>
    </item>
    <item>
      <title>Re: Summarize variables on PROC SQL UNION?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summarize-variables-on-PROC-SQL-UNION/m-p/760412#M240456</link>
      <description>Thanks Harry. This got me moving forward. The union all was what I needed along with getting the group by clause on the first select.</description>
      <pubDate>Mon, 09 Aug 2021 16:26:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summarize-variables-on-PROC-SQL-UNION/m-p/760412#M240456</guid>
      <dc:creator>G_I_Jeff</dc:creator>
      <dc:date>2021-08-09T16:26:17Z</dc:date>
    </item>
  </channel>
</rss>

