Hello! I'm new to SAS and I love it! Hopefully I can explain my question clearly. I'm working with a data set which contains daily security returns (and a variable to mark the dates of the returns). I need to create a data set in the same format with the average standard deviation of the previous five days of returns, including the current day. I found code at http://support.sas.com/kb/41/380.html on calculating a rolling standard deviation using an array. My university's statistician helped me to adapt the code: data sub(keep = B); set returns; run; %let roll_num = 5; data sub1; set sub; array summed[&roll_num] _temporary_; if E = &roll_num then E = 1; else E + 1; summed = B; /*start summing once &roll_num values have been read from the data set*/ if _N_ >= &roll_num then do; roll_std = std(of summed ); end; format roll_std comma10.4; id = _n_; run; proc print data = sub1 (obs=10); run; This produces the following results: B E roll_std id 0.0055 1 . 1 -0.0098 2 . 2 0.0057 3 . 3 0.0029 4 . 4 0.0069 5 0.0069 5 0.0252 1 0.0125 6 0.0081 2 0.0088 7 0.0081 3 0.0086 8 0.0361 4 0.0132 9 0.0050 5 0.0135 10 This does some of what I want, but it is structured to perform the calculations on one variable only, as the KEEP=option [data sub(keep = B);] drops everything but the second variable in my data set. I think the intent was to use DO loops to perform this calculation on all the variables, but I do not know how to do this or to capture the output that I want (the roll_std variable). This would be one way to complete this problem. Alternatively, it may be easier to keep all the variables in the sub data set and expand this code to the entire data set. I tried removing the KEEP= option, but this gives me the following error after the last DATA step: "ERROR: Array subscript out of range" at line xx column y. Thanks in advance for any help with this!
... View more