Skip to content

Instantly share code, notes, and snippets.

@statgeek
Last active November 19, 2017 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save statgeek/e5e43ff45a4ba1f64d0873ff3bc35974 to your computer and use it in GitHub Desktop.
Save statgeek/e5e43ff45a4ba1f64d0873ff3bc35974 to your computer and use it in GitHub Desktop.
SAS macro moving averages proc means
proc sql noprint;
select min(period) into :min_period TRIMMED
from have;
select max(period) into :max_period TRIMMED
from have;
quit;
%put &min_period;
%put &max_period;
options nomprint nosymbolgen;
%macro moving_summary(datain=, window=, dataout=);
*remove previous summary table;
proc sql;
drop table &dataout;
quit;
%do i=&min_period. %to %eval(&max_period.-&window.);
proc means data=&datain noprint;
var x;
where period between &i and %eval(&i. + &window. - 1);
output out=_temp mean=mean_x std=std_x stderr=stderr_x;
run;
data _temp;
set _temp;
start = &i;
end = &i + &window - 1;
run;
proc append base=&dataout data=_temp;
run;
proc sql;
drop table _temp;
quit;
%end;
%mend;
%moving_summary(datain=have, window=6, dataout=summary_stats);
proc means data=have;
where period between 1 and 6;
var x;
run;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment