BookmarkSubscribeRSS Feed
Akarsh91
Calcite | Level 5

Dear Experts, 

 

I have the folloowing information

 

 

data have;
  infile datalines dlm=',' dsd truncover;
  input ID. Returns  year;
 datalines;
1,1.01,1968
1,1.04,1969
1,1.001,1970
1,1.005,1971
1,1.02,1972
1,1.02,1973
1,1.06,1980
1,1.06,1981
1,1.0014,1982
run; 

 

ID    Date                        year

1        1.01                     1968

1        1.04                     1969

1       1.001                    1970

1       1.005   .                1971

1         1.02                    1972

1         1.02                    1973

1         1.06                    1980

1         1.06                    1981

1        1.0014                 1982

 

I would like to calculate the 2 yearly moving averages for group ID 1. The problem however is that the returns from 1974 to 1979 i are missing. How can one calculate moving average by year, such that for the years 1968 to 1973 and seperately for 1980 to 1982 in the same dataset? Could anyone help with the SAS code that could accomplish this task. 

Please write if you need more information. 

 

Regards 
Akarsh91

3 REPLIES 3
Reeza
Super User

PROC EXPAND. Search on here or the DOCS for examples.

Patrick
Opal | Level 21

@Akarsh91

This article could be helpful to you: http://blogs.sas.com/content/iml/2016/01/27/moving-average-in-sas.html

 

The following Google search string will return quite a few helpful hits:

site:*.sas.com rick wicklin moving average 

 

 

 

Ksharp
Super User

If you don't have big table, try SQL.

 

data have;
  infile datalines dlm=',' dsd truncover;
  input ID Returns  year;
 datalines;
1,1.01,1968
1,1.04,1969
1,1.001,1970
1,1.005,1971
1,1.02,1972
1,1.02,1973
1,1.06,1980
1,1.06,1981
1,1.0014,1982
run; 
proc sql;
select *,(select mean(returns) from have 
where id=a.id and year between a.year-2 and a.year) as rolling_mean
 from have as a;
quit;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1021 views
  • 0 likes
  • 4 in conversation