BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kimx0961
Obsidian | Level 7

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
kimx0961
Obsidian | Level 7

Thank you very much

It works well.

View solution in original post

9 REPLIES 9
Reeza
Super User
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
kimx0961
Obsidian | Level 7

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.

Reeza
Super User
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. 

PGStats
Opal | Level 21

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
kimx0961
Obsidian | Level 7

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

 

 

PGStats
Opal | Level 21

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
kimx0961
Obsidian | Level 7

Thank you very much

It works well.

Reeza
Super User
Generally you mark the correct answer as correct, not your response....
Reeza
Super User
You should mark this question answered and post a new question, which people would be happy to help with 🙂

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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