/***************************************************************************************************************** SAS file name: hash_moving_average.sas File location: __________________________________________________________________________________________________________________ Purpose: To demonstrate the Author: Creation Date: This program supports the blog post "" on SASnrd.com *****************************************************************************************************************/ /* Example Data */ data have; input time value; datalines; 0 100 1 101 2 103 3 107 4 116 5 108 6 113 7 106 8 110 9 115 10 121 11 117 12 123 13 127 14 121 15 126 16 131 17 125 18 125 19 130 20 135 ; /* Proc Expand code */ proc expand data = have out = MoveAv method = none; id time; convert value = MovAv / transformout = (movave 3) ; convert value = CMovAv / transformout = (cmovave 3); run; /* Moving Average */ data MovAv(drop = s t); if _N_ = 1 then do; dcl hash h(dataset : "have"); h.definekey("time"); h.definedata("value"); h.definedone(); end; set have; s = 0; d = 0; do t = time - 2 to time; value = .; if h.find(key : t) = 0 then do; s = sum(s, value); d + 1; end; end; MovAv = divide(s, d); run; /* Centered Moving Average */ data CMovAv(drop = s t); if _N_ = 1 then do; dcl hash h(dataset : "have"); h.definekey("time"); h.definedata("value"); h.definedone(); end; set have; s = 0; d = 0; do t = time - 1 to time + 1; value = .; if h.find(key : t) = 0 then do; s = sum(s, value); d + 1; end; end; MovAv = divide(s, d); run;