<?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 sums based on condition in SAS Visual Analytics</title>
    <link>https://communities.sas.com/t5/SAS-Visual-Analytics/Aggregate-sums-based-on-condition/m-p/960423#M18655</link>
    <description>&lt;P&gt;How about this - the first data step is just generating some fake data.&amp;nbsp; This allows gaps in months.&amp;nbsp; I am not sure what your ID variable is supposed to be, though:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
date='01Jan2023'd;
format date date9.;
do i=1 to 50;
	date+rand('integer',5,45);
	co2=rand('integer',1,20);
	output;
end;
drop i;
run;

proc sort data=have; by date; run;

proc sql noprint;
select min(intnx('month',date,0)), max(intnx('month',date,0)) 
into :firstmonth trimmed, :lastmonth trimmed from have;
quit;

data _null_;
call symputx('nmonths',intck('month',&amp;amp;firstmonth,&amp;amp;lastmonth)+1);
run;

%put NMONTHS: &amp;amp;nmonths;

data want;
set have end=last;
array T {-1:&amp;amp;nmonths} _temporary_;
T[intck('month',&amp;amp;firstmonth,date)+1]+co2;
if last then do;
	do i=1 to &amp;amp;nmonths;
		yrmonth=put(intnx('month',&amp;amp;firstmonth,i-1),yymmn6.);
		month_m0=T[i];
		month_m1=T[i-1];
		month_m2=T[i-2];
		output;
	end;
end;
keep yrmonth month_:;
run;

proc print data=want; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 27 Feb 2025 15:34:57 GMT</pubDate>
    <dc:creator>quickbluefish</dc:creator>
    <dc:date>2025-02-27T15:34:57Z</dc:date>
    <item>
      <title>Aggregate sums based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Visual-Analytics/Aggregate-sums-based-on-condition/m-p/960394#M18652</link>
      <description>&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Id&lt;/TD&gt;&lt;TD&gt;date&lt;/TD&gt;&lt;TD&gt;month&lt;/TD&gt;&lt;TD&gt;co2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;12/12/24&lt;/TD&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;5/1/25&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;13/12/24&lt;/TD&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;5/2/25&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;27/2/25&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;14/12/24&lt;/TD&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;I'm trying to set up some month over month reports. Since my data can encompass a wide range of dates, I've created two calculated items: the_month_two_months_ago and the_month_one_month_ago which represent those respective month digits.&lt;BR /&gt;&lt;BR /&gt;My goal now is to create two additional computed items: 1 for two month's ago co2 aggregate sum and the other for the last month's co2 aggregate sum. But when creating the calculated item for an aggregate sum, I can't figure out how to aggregate based on a conditional using the&amp;nbsp;the_month_two_months_ago and the_month_one_month_ago computed items. Is that even possible? It only seems to ask me ByGroup for ForAll and then the specific metric (co2).&lt;BR /&gt;&lt;BR /&gt;Any guidance would be appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2025 12:36:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Visual-Analytics/Aggregate-sums-based-on-condition/m-p/960394#M18652</guid>
      <dc:creator>VMn</dc:creator>
      <dc:date>2025-02-27T12:36:44Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregate sums based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Visual-Analytics/Aggregate-sums-based-on-condition/m-p/960421#M18654</link>
      <description>&lt;P&gt;Would the AggregateTable function help you?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/SAS-Visual-Analytics-Understanding-the-AggregateTable-Function/ta-p/959903" target="_blank" rel="noopener"&gt;SAS Visual Analytics: Understanding the AggregateTable Function for Beginners&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2025 15:18:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Visual-Analytics/Aggregate-sums-based-on-condition/m-p/960421#M18654</guid>
      <dc:creator>Madelyn_SAS</dc:creator>
      <dc:date>2025-02-27T15:18:28Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregate sums based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Visual-Analytics/Aggregate-sums-based-on-condition/m-p/960423#M18655</link>
      <description>&lt;P&gt;How about this - the first data step is just generating some fake data.&amp;nbsp; This allows gaps in months.&amp;nbsp; I am not sure what your ID variable is supposed to be, though:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
date='01Jan2023'd;
format date date9.;
do i=1 to 50;
	date+rand('integer',5,45);
	co2=rand('integer',1,20);
	output;
end;
drop i;
run;

proc sort data=have; by date; run;

proc sql noprint;
select min(intnx('month',date,0)), max(intnx('month',date,0)) 
into :firstmonth trimmed, :lastmonth trimmed from have;
quit;

data _null_;
call symputx('nmonths',intck('month',&amp;amp;firstmonth,&amp;amp;lastmonth)+1);
run;

%put NMONTHS: &amp;amp;nmonths;

data want;
set have end=last;
array T {-1:&amp;amp;nmonths} _temporary_;
T[intck('month',&amp;amp;firstmonth,date)+1]+co2;
if last then do;
	do i=1 to &amp;amp;nmonths;
		yrmonth=put(intnx('month',&amp;amp;firstmonth,i-1),yymmn6.);
		month_m0=T[i];
		month_m1=T[i-1];
		month_m2=T[i-2];
		output;
	end;
end;
keep yrmonth month_:;
run;

proc print data=want; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Feb 2025 15:34:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Visual-Analytics/Aggregate-sums-based-on-condition/m-p/960423#M18655</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-02-27T15:34:57Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregate sums based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Visual-Analytics/Aggregate-sums-based-on-condition/m-p/960503#M18656</link>
      <description>&lt;P&gt;Thank you! This proved very useful!&lt;/P&gt;</description>
      <pubDate>Fri, 28 Feb 2025 06:30:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Visual-Analytics/Aggregate-sums-based-on-condition/m-p/960503#M18656</guid>
      <dc:creator>VMn</dc:creator>
      <dc:date>2025-02-28T06:30:58Z</dc:date>
    </item>
  </channel>
</rss>

