DATA Step, Macro, Functions and more

Rolling average returns

Occasional Contributor
Posts: 8

Rolling average returns

[ Edited ]


I'm trying to do rolling average returns for each "bemedeciles" (Or groups), from Aprile (t) to Aprile (t+1), Aprile (t+1) to (Aprile t+2) and so on. 

The thing that I'm struggling is how to do it. I've search the entire internet, but not found a quite "understandable" methods to do it. 
The part where it stops is how to get the rolling average returns in those intervals, based on "Names date", and "bemedecile". 

Can someone please help me? I'm on ground zero, no progress on it for 1 day now.




Posts: 8,164

Re: Rolling average returns

Posted in reply to TorTheHammer

It would help if you could provide have and want datasets in the form of datasteps. From your description it looks like you are only attempting to get averages of each adjacent pair of values, within each group, in your datastep


Art, CEO,


Super User
Posts: 23,733

Re: Rolling average returns

Posted in reply to TorTheHammer

I agree that there's a lot out there. Here's a post from Rick that lists many of the options:


I think you want the second example here - with BY groups:


In general, we can't really help off a picture, for starters we'd have to type it out if we want to work with your data, which many will not do. So a 'generic' question will get 'generic' answers. 

Super User
Posts: 10,784

Re: Rolling average returns

Posted in reply to TorTheHammer

Here is an example.(if you do not have a big table)

Length of rolling windows is a month forward.


data air;
 set sashelp.air;
 if mod(_n_,100)=1 then group+1;

proc sql;
create table want as
 select *,(select mean(air) from air where and
 date between and intnx('month',,1,'s')) as rolling_mean
  from air as a;
Trusted Advisor
Posts: 1,339

Re: Rolling average returns

Posted in reply to TorTheHammer

You have CRSP monthly data, sorted by PERMNO/DATE, where DATE is the last trading date of the month.  You want rolling 12 months average returns (monthly average, or yearly?)  (arithmetic average or geometric mean?).  Below is geometric mean monthly return for rolling 12-month window.


data want;

  do seq=1 by 1 until(last.permno);

    set have;

    by permno;




    if seq<=11 then continue;







Note that this program assumes there are no "holes" in the monthly series for any permno.


Occasional Contributor
Posts: 8

Re: Rolling average returns

First of all, thank you very much for your response!

Yes, I want yearly average returns (From the monthly).

Otherwise, yes I want geometric mean. 

Thank you Smiley Very Happy 

Ask a Question
Discussion stats
  • 5 replies
  • 5 in conversation