<?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 How do i Sum a column based on its year (2 different years in the same table)? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-i-Sum-a-column-based-on-its-year-2-different-years-in-the/m-p/957075#M373642</link>
    <description>&lt;P&gt;How do i Sum a column based on its year (2 different years in the same table)?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data mockup;&lt;/P&gt;&lt;P&gt;ID='1';&lt;/P&gt;&lt;P&gt;year=2023;&lt;/P&gt;&lt;P&gt;amt=1;&lt;/P&gt;&lt;P&gt;output;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID='2';&lt;/P&gt;&lt;P&gt;year=2023;&lt;/P&gt;&lt;P&gt;amt=2;&lt;/P&gt;&lt;P&gt;output;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID='3';&lt;/P&gt;&lt;P&gt;year=2024;&lt;/P&gt;&lt;P&gt;amt=100;&lt;/P&gt;&lt;P&gt;output;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID='4';&lt;/P&gt;&lt;P&gt;year=2024;&lt;/P&gt;&lt;P&gt;amt=200;&lt;/P&gt;&lt;P&gt;output;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know i can easily sum up a column by using Proc SQL sum. However, in this case, i wish to achieve the following item:&lt;BR /&gt;Get the Difference of sum between the total of 2024 - total of 2023. Instead of separating 2 PROC SQL just to sum where year = xxxx, is there a quicker way?&lt;BR /&gt;&lt;BR /&gt;I can only think of this:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;Proc SQL;

create table out2024 as
select *
, sum(amt) as amt_2024
from mockup
where year=2024;
quit;



Proc SQL;
create table out2023 as
select *
, sum(amt) as amt_2023
from mockup
where year=2023;
quit;



proc sql;
create table diff as
select a2024.*&amp;nbsp;
,&amp;nbsp; b2023.*
from out2024 a2024 left join
out2023 b2023
on a2024.ID = b2023.ID;
quit;



data Diff_final;
set diff;
totaldiff = amt_2024-amt2023;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am sure there is a quicker way for achieving the same thing. Seeking your advice.&lt;/P&gt;</description>
    <pubDate>Fri, 24 Jan 2025 08:10:23 GMT</pubDate>
    <dc:creator>StickyRoll</dc:creator>
    <dc:date>2025-01-24T08:10:23Z</dc:date>
    <item>
      <title>How do i Sum a column based on its year (2 different years in the same table)?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-i-Sum-a-column-based-on-its-year-2-different-years-in-the/m-p/957075#M373642</link>
      <description>&lt;P&gt;How do i Sum a column based on its year (2 different years in the same table)?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data mockup;&lt;/P&gt;&lt;P&gt;ID='1';&lt;/P&gt;&lt;P&gt;year=2023;&lt;/P&gt;&lt;P&gt;amt=1;&lt;/P&gt;&lt;P&gt;output;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID='2';&lt;/P&gt;&lt;P&gt;year=2023;&lt;/P&gt;&lt;P&gt;amt=2;&lt;/P&gt;&lt;P&gt;output;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID='3';&lt;/P&gt;&lt;P&gt;year=2024;&lt;/P&gt;&lt;P&gt;amt=100;&lt;/P&gt;&lt;P&gt;output;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID='4';&lt;/P&gt;&lt;P&gt;year=2024;&lt;/P&gt;&lt;P&gt;amt=200;&lt;/P&gt;&lt;P&gt;output;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know i can easily sum up a column by using Proc SQL sum. However, in this case, i wish to achieve the following item:&lt;BR /&gt;Get the Difference of sum between the total of 2024 - total of 2023. Instead of separating 2 PROC SQL just to sum where year = xxxx, is there a quicker way?&lt;BR /&gt;&lt;BR /&gt;I can only think of this:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;Proc SQL;

create table out2024 as
select *
, sum(amt) as amt_2024
from mockup
where year=2024;
quit;



Proc SQL;
create table out2023 as
select *
, sum(amt) as amt_2023
from mockup
where year=2023;
quit;



proc sql;
create table diff as
select a2024.*&amp;nbsp;
,&amp;nbsp; b2023.*
from out2024 a2024 left join
out2023 b2023
on a2024.ID = b2023.ID;
quit;



data Diff_final;
set diff;
totaldiff = amt_2024-amt2023;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am sure there is a quicker way for achieving the same thing. Seeking your advice.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jan 2025 08:10:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-i-Sum-a-column-based-on-its-year-2-different-years-in-the/m-p/957075#M373642</guid>
      <dc:creator>StickyRoll</dc:creator>
      <dc:date>2025-01-24T08:10:23Z</dc:date>
    </item>
    <item>
      <title>Re: How do i Sum a column based on its year (2 different years in the same table)?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-i-Sum-a-column-based-on-its-year-2-different-years-in-the/m-p/957082#M373646</link>
      <description>&lt;P&gt;Untested code. Something like this should work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select sum(AMT * (YEAR=2023)) - sum(AMT * (YEAR=2024)) as DIFF
  from MOCKUP;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The amount is nullified when the year test is not true.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jan 2025 09:57:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-i-Sum-a-column-based-on-its-year-2-different-years-in-the/m-p/957082#M373646</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2025-01-24T09:57:49Z</dc:date>
    </item>
  </channel>
</rss>

