/* test data */
data one;
input symbol $ value date :date9.;
format date date9.;
cards;
ABP1 -0.025 18feb1997
ABP1 0.05 25feb1998
ABP1 -0.025 05mar1999
ABP1 0.06 20mar2000
ABP1 0.25 05mar2001
ABP1 0.455 07mar2002
ABP1 0.73 25feb2003
ABP1 1.01 19feb2004
ABP1 1.25 16feb2005
ABP1 1.65 09feb2006
ABP1 1.87 08feb2007
ABT 0.555 14jan1991
ABT 0.6375 14jan1992
ABT 0.73 16jan1993
;
run;
/* 5 year moving avg, stdev, cv assuming:
one obs per year from 1990 to 2010.
observations are already in the sorted order by symbol. */
%let START = 1990;
%let FINISH = 2010;
data two;
array val[%eval(&START-3):&FINISH] val1-val3 val&START-val&FINISH;
call missing(of val&START-val&FINISH);
do until (last.symbol);
set one;
by symbol;
year = year(date);
if &START<=year<=&FINISH then val[year] = value;
end;
do year = %eval(&START+2) to &FINISH;
avg5 = mean(val[year-5],val[year-4],val[year-3],val[year-2],val[year-1]);
std5 = std(val[year-5],val[year-4],val[year-3],val[year-2],val[year-1]);
cv5 = divide(std5,avg5);
if not missing(cv5) then output;
end;
keep symbol year avg5 std5 cv5;
run;
/* check */
proc print data=two;
run;
/* on lst
Obs symbol year avg5 std5 cv5
/* test data */
data one;
input symbol $ value date :date9.;
format date date9.;
cards;
ABP1 -0.025 18feb1997
ABP1 0.05 25feb1998
ABP1 -0.025 05mar1999
ABP1 0.06 20mar2000
ABP1 0.25 05mar2001
ABP1 0.455 07mar2002
ABP1 0.73 25feb2003
ABP1 1.01 19feb2004
ABP1 1.25 16feb2005
ABP1 1.65 09feb2006
ABP1 1.87 08feb2007
ABT 0.555 14jan1991
ABT 0.6375 14jan1992
ABT 0.73 16jan1993
;
run;
proc sql;
create table two as
select distinct
a.symbol,
b.value,
year(a.date) as year,
b.date as date5
from
one a,
one b
where
a.symbol=b.symbol
and intck('year',b.date,a.date) between 1 and 5
order by
a.symbol,
year,
date5;
quit;
proc sql;
create table three as
select distinct
symbol,
year,
count(symbol) as n5,
avg(value) as avg5,
std(value) as std5
from
two
group by
symbol,
year;
quit;