Skip to Main Content

Macro to Compute a “rolling” standard deviation | SAS Code Fragments

The macro below shows how you can compute a "rolling" standard deviation.
  For example, it gets the standard deviation for observations 1-25, then
observations 2-26 then 3-27 … up to 476-500, and then saves this into a SAS data file.

%macro rollsd;

  proc delete data=all;
  run;

  %do firstobs = 1 %to 476;
    %let lastobs = %eval( &firstobs + 24);
    proc means data=test(firstobs=&firstobs obs=&lastobs);
      var x;
      output out=tempfile stddev=sd25;
    run;

    proc append base=all data=tempfile ;
    run;
  %end;

%mend rollsd;