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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 402 views
  • 0 likes
  • 4 in conversation