DATA Step, Macro, Functions and more

the rolling prior five-year average computation

Accepted Solution Solved
Reply
Contributor
Posts: 26
Accepted Solution

the rolling prior five-year average computation

Hi,

 

The following is the datset I have.

gvkey   fyear  rds 

0001    1980   0.05

0001    1981   0.02

0001    1982   0.03

0001    1983   0.04

0001    1984   0.04

0001    1985   0.05

0001    1986   0.06

 

I would like to compute the rolling prior five-year average for variable 'rds'. In other words, in 1986, average rds of prior five year is (rds in1985 + rds in 1984+rds in 1983+rds in 1982+rds in 1981) should be computed, and in 1985, average rds of prior five year is (rds in 1984 + rds in 1983 + rds in 1982 + rds in 1981 + rds in 1980) should be computed, and so on. And I also want to require at least three non-missing observations per gvkey. In this example, in 1983, average of rds of prior three year is (rds in 1982 + rds in 1981 + rds in 1980) can be computed. (not 1982, 1981). Any advice will be highly appreciated.

proc sql: create table want as

select *, select ave(rds) from have where fyear between intnx ('fyear',-5) and ('fyear',-1) from have as  rds5

 


Accepted Solutions
Solution
‎12-09-2015 09:58 PM
Contributor
Posts: 26

Re: the rolling prior five-year average computation

Thank you very much

It works well.

View solution in original post


All Replies
Super User
Posts: 17,898

Re: the rolling prior five-year average computation

Do you have SAS/ETS? If so, Proc Expand is a good solution. If not, you can use a temporary array solution:

https://communities.sas.com/message/244232
Contributor
Posts: 26

Re: the rolling prior five-year average computation

Thank you for your prompt reply

According to my search, PROC EXPAND 'movave' option computes the rolling five-year mean using current year value and values of prior years (lag4, lag3,lag2,lag1 values). My example needs to compute the folling prior five-year mean using values of lag5, lag4, lag3, lag2, and lag1. It wll be appreicated if you advise me more in detail about array statement.

Super User
Posts: 17,898

Re: the rolling prior five-year average computation

[ Edited ]
data want;
array p{0:4} _temporary_;
set have; 
by object;
if first.object then call missing(of p{*});

if n(of p{*})>=3 then average = mean(of p{*});

p{mod(_n_,5)} = price;
run;

 

EDIT: Modified to account for 3+ years of data present. 

Respected Advisor
Posts: 4,654

Re: the rolling prior five-year average computation

Can be done efficiently with arrays but you have to worry about missing years. Not a problem with SQL:

 

data have;
input gvkey fyear rds;
datalines;
0001    1980   0.05
0001    1981   0.02
0001    1982   0.03
0001    1983   0.04
0001    1984   0.04
0001    1985   0.05
0001    1986   0.06
;

proc sql;
create table want as
select 
    a.gvkey, 
    a.fyear, 
    a.rds, 
    case 
        when count(b.rds) >= 3 then mean(b.rds) 
        else . 
        end as rds5y
from 
    have as a left join
    have as b 
        on  a.gvkey=b.gvkey and 
            b.fyear between a.fyear-5 and a.fyear-1
group by a.gvkey, a.fyear, a.rds;
select * from want;
quit;
PG
Contributor
Posts: 26

Re: the rolling prior five-year average computation

Thank you so much for your help and time!

Your program worked successfully. Can I ask one more question?

 

I have six dataset with over 200,000 observations like the following and want to merge these six datasets efficiently.

 

dataset 1:gvkey fyear rds

dataset 2:gvkey fyear emp

dataset 3:gvkey fyear emps

dataset 4:gvkey fyear ddd

dataset 5:gvkey fyear eee

dataset 6:gvkey fyear aaa

 

wanted integrated dataset: gvkey fyear rds emp emps ddd eee aaa

Using 'set' and 'merge' statements don't work well.

Thank you again for your help

 

 

Respected Advisor
Posts: 4,654

Re: the rolling prior five-year average computation

MERGE should work well for joining your datasets once they are sorted. What do you mean by "don't work well". What code did you try? What problem did you encounter?

 

data all;

merge dataset1 dataset2 dataset3 dataset4 dataset5 dataset6;

by gvkey fyear;

run; 

PG
Solution
‎12-09-2015 09:58 PM
Contributor
Posts: 26

Re: the rolling prior five-year average computation

Thank you very much

It works well.

Super User
Posts: 17,898

Re: the rolling prior five-year average computation

Generally you mark the correct answer as correct, not your response....
Super User
Posts: 17,898

Re: the rolling prior five-year average computation

You should mark this question answered and post a new question, which people would be happy to help with Smiley Happy
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 331 views
  • 1 like
  • 3 in conversation