<?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: Aggregate and difference amount in one step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Aggregate-and-difference-amount-in-one-step/m-p/700837#M214531</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;As a alternative approach, can we handle the calculation with 'calculated'&lt;BR /&gt;keyword?&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you have a different code idea, try it (Maxim 4).&lt;/P&gt;</description>
    <pubDate>Mon, 23 Nov 2020 05:32:41 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-11-23T05:32:41Z</dc:date>
    <item>
      <title>Aggregate and difference amount in one step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregate-and-difference-amount-in-one-step/m-p/700758#M214493</link>
      <description>&lt;P&gt;Is there a way to combine the below three steps into one step? I just want to sum the two fields from two tables with group by and then I've to find the difference between those values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Same calculation for both the variables ULTIMATE_PREMIUMS_1 and ULTIMATE_PREMIUMS_2&amp;nbsp;as I want to see same values in these two variables. Any help?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead of creating three WORK datasets I want to accomplish it in one dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;/*Premium - aggregate ULTIMATE_PREMIUMS*/
proc sql;
create table prem as select * ,sum(ULTIMATE_PREMIUMS) as prem_sum from have1
group by
      UNIT,
      BRANCH,
      SUB_SEGMENT,
      ;
quit;

/*History - aggregate PERIOD_VALUE*/
proc sql;
create table hist as select distinct * ,sum(PERIOD_VALUE) as hist_sum from have2
group by
      UNIT,
      BRANCH,
      SUB_SEGMENT,
      ;
quit;

/*Join Premium and History */

proc sql;
    create table want as
        select prem.*,sum(prem.prem_sum,- hist.hist_sum) as ULTIMATE_PREMIUMS_1 length = 8,
            sum(prem.prem_sum,- hist.hist_sum)  as ULTIMATE_PREMIUMS_2 length = 8,
        from
            prem as prem left join 
            hist as hist
            on
            (
            prem.UNIT = hist.UNIT
            and prem.BRANCH = hist.BRANCH
            and prem.SUB_SEGMENT = hist.SUB_SEGMENT
                )
            order by
                prem.UNIT,
                prem.BRANCH,
                prem.SUB_SEGMENT
    ;
quit;&lt;/PRE&gt;</description>
      <pubDate>Sun, 22 Nov 2020 16:06:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregate-and-difference-amount-in-one-step/m-p/700758#M214493</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2020-11-22T16:06:39Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregate and difference amount in one step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregate-and-difference-amount-in-one-step/m-p/700775#M214500</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
  select
    coalesce (a.unit,b.unit) as unit,
    coalesce (a.branch,b.branch) as branch,
    coalesce (a.sub_segment,b.sub_segment) as sub_segment,
    a.prem_sum,
    b.hist_sum,
    sum(a.prem_sum, -b.hist_sum) as ultimate_premiums
  from (
    select
      unit,
      branch,
      sub_segment,
      sum(ultimate__premiums) as prem_sum
    from have1
    group by
      unit,
      branch,
      sub_segment
  ) a
  full join (
    select
      unit,
      branch,
      sub_segment,
      sum(period_value) as hist_sum
    from have2
    group by
      unit,
      branch,
      sub_segment
  ) b
  on
    a.unit = b.unit and
    a.branch = b.branch and
    a.sub_segment = b.sub_segment
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 22 Nov 2020 18:57:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregate-and-difference-amount-in-one-step/m-p/700775#M214500</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-22T18:57:00Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregate and difference amount in one step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregate-and-difference-amount-in-one-step/m-p/700776#M214501</link>
      <description>&lt;P&gt;You could put them all in a single PROC SQL step, which in itself wouldn't reduce resources.&amp;nbsp; But you would likely improve efficiency greatly if use use &lt;EM&gt;&lt;STRONG&gt;create view&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp;instead of &lt;EM&gt;&lt;STRONG&gt;create table&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp;for prem and hist.&amp;nbsp; &amp;nbsp;Just be sure to use the NOPRINT option on the proc sql statement - this allows proc sql to defer actual instantiation of the views until they are called for in the create table want statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint ;
   create view prem as .... ;
   create view hist as ... ;
   create table want as ...;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;True, this doesn't reduce the amount of code, but it does avoid writing to disk (and re-reading from disk) of the prem and hist data sets.&lt;/P&gt;</description>
      <pubDate>Sun, 22 Nov 2020 18:55:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregate-and-difference-amount-in-one-step/m-p/700776#M214501</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-11-22T18:55:17Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregate and difference amount in one step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregate-and-difference-amount-in-one-step/m-p/700786#M214504</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp; Please explain further "&lt;EM&gt;this allows proc sql to defer actual instantiation of the views until they are called for&lt;/EM&gt;". What do you mean by &lt;EM&gt;instantiation&lt;/EM&gt;?&lt;/P&gt;</description>
      <pubDate>Sun, 22 Nov 2020 20:44:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregate-and-difference-amount-in-one-step/m-p/700786#M214504</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-11-22T20:44:30Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregate and difference amount in one step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregate-and-difference-amount-in-one-step/m-p/700815#M214519</link>
      <description>&lt;P&gt;Why coalesce function has been used in select clause? If I want to select&lt;BR /&gt;numeric variable,which function to use and how to tackle it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also why you have used full join? Shouldn't be left join?&lt;/P&gt;</description>
      <pubDate>Mon, 23 Nov 2020 02:05:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregate-and-difference-amount-in-one-step/m-p/700815#M214519</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2020-11-23T02:05:33Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregate and difference amount in one step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregate-and-difference-amount-in-one-step/m-p/700827#M214523</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Why coalesce function has been used in select clause? If I want to select&lt;BR /&gt;numeric variable,which function to use and how to tackle it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also why you have used full join? Shouldn't be left join?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I used the full join to make sure the code catches cases where there are entries in have2 that don't exist in have1, and for such cases the COALESCE is needed.&lt;/P&gt;
&lt;P&gt;If the left join is sufficient for you, you can also omit the COALESCE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SQL COALESCE works for character and numeric variables.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Nov 2020 04:43:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregate-and-difference-amount-in-one-step/m-p/700827#M214523</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-23T04:43:25Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregate and difference amount in one step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregate-and-difference-amount-in-one-step/m-p/700835#M214529</link>
      <description>As a alternative approach, can we handle the calculation with 'calculated'&lt;BR /&gt;keyword?&lt;BR /&gt;</description>
      <pubDate>Mon, 23 Nov 2020 05:28:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregate-and-difference-amount-in-one-step/m-p/700835#M214529</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2020-11-23T05:28:57Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregate and difference amount in one step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregate-and-difference-amount-in-one-step/m-p/700837#M214531</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;As a alternative approach, can we handle the calculation with 'calculated'&lt;BR /&gt;keyword?&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you have a different code idea, try it (Maxim 4).&lt;/P&gt;</description>
      <pubDate>Mon, 23 Nov 2020 05:32:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregate-and-difference-amount-in-one-step/m-p/700837#M214531</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-23T05:32:41Z</dc:date>
    </item>
  </channel>
</rss>

