Id | date | month | co2 |
0 | 12/12/24 | 12 | 5 |
1 | 5/1/25 | 1 | 10 |
2 | 13/12/24 | 12 | 1 |
3 | 5/2/25 | 2 | 5 |
4 | 27/2/25 | 2 | 10 |
5 | 14/12/24 | 12 | 5 |
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.
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 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).
Any guidance would be appreciated!
Would the AggregateTable function help you?
SAS Visual Analytics: Understanding the AggregateTable Function for Beginners
-------------------------------------------------------------------------
Four tips to remember when you contact SAS Technical Support
Tricks for SAS Visual Analytics Report Builders
SAS Visual Analytics Learning Center
Would the AggregateTable function help you?
SAS Visual Analytics: Understanding the AggregateTable Function for Beginners
-------------------------------------------------------------------------
Four tips to remember when you contact SAS Technical Support
Tricks for SAS Visual Analytics Report Builders
SAS Visual Analytics Learning Center
Thank you! This proved very useful!
How about this - the first data step is just generating some fake data. This allows gaps in months. I am not sure what your ID variable is supposed to be, though:
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',&firstmonth,&lastmonth)+1);
run;
%put NMONTHS: &nmonths;
data want;
set have end=last;
array T {-1:&nmonths} _temporary_;
T[intck('month',&firstmonth,date)+1]+co2;
if last then do;
do i=1 to &nmonths;
yrmonth=put(intnx('month',&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;
See how to use one filter for multiple data sources by mapping your data from SAS’ Alexandria McCall.
Find more tutorials on the SAS Users YouTube channel.