BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
VMn
Calcite | Level 5 VMn
Calcite | Level 5
Iddatemonthco2
012/12/24125
15/1/25110
213/12/24121
35/2/2525
427/2/25210
514/12/24125


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!

 

1 ACCEPTED SOLUTION
3 REPLIES 3
VMn
Calcite | Level 5 VMn
Calcite | Level 5

Thank you! This proved very useful!

quickbluefish
Barite | Level 11

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Tips for filtering data sources in SAS Visual Analytics

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.

Discussion stats
  • 3 replies
  • 1039 views
  • 0 likes
  • 3 in conversation