Answer by @PaigeMiller [Had to remove original post since accepted the answer by misstake].
First, do not try to work with dates/months as character strings, because these cannot be sorted. (If you did sort them, then April would be the first month, and I'm guessing you don't want that). Work with numeric date values, as shown below in variable month. Then sort by segment and month, then compute the moving averages using PROC EXPAND (if you have a license for SAS/ETS)
data have;
input Period $ Segment $ Sum;
month=input(compress(period,'-'),monyy.);
format month monyy7.;
datalines;
jan-22 A 100 .
jan-22 B 50 .
jan-22 C 40 .
feb-22 A 80 .
feb-22 B 60 .
feb-22 C 30 .
mar-22 A 130 .
mar-22 B 110 .
mar-22 C 100 .
apr-22 A 200 .
apr-22 B 150 .
apr-22 C 100 .
may-22 A 100 .
may-22 B 80 .
may-22 C 60 .
jun-22 A 200 .
jun-22 B 150 .
jun-22 C 40 .
jul-22 A 500 1310
jul-22 B 100 700
jul-22 C 50 360
aug-22 A 400 1610
aug-22 B 200 850
aug-22 C 100 420
sep-22 A 200 1730
sep-22 B 100 890
sep-22 C 30 420
oct-22 A 400 2000
oct-22 B 300 1080
oct-22 C 200 520
nov-22 A 300 2100
nov-22 B 100 1030
nov-22 C 50 530
dec-22 A 40 2040
dec-22 B 20 970
dec-22 C 10 480
;
proc sort data=have;
by segment month;
run;
proc expand data=have out=want;
by segment;
convert sum=sum_rolling_6_mths / transformout=(movsum 6 trimleft 5);
run;
... View more