Let's say N=8 and M= 6. If the number of non-missing values within a rolling window is fewer than 6, the program should set the output value to be missing. Not sure if the following does this:
proc expand data = have out = want method = none;
by firm;
id date;
convert x = y/ transformout = (movsum 8 trimleft 5);
run;
I'm not entirely sure here, but I don't think Proc Expand has such an option.
Do you want an alternative to Proc Expand?
@xyxu agree. Proc Expand is so short and smooth 🙂
/*Assuming there is no gap between dates*/ data have; call streaminit(123); do firm='A' ,'B' ,'C'; do date='01jan2010'd to '20dec2021'd; x=rand('uniform');output; end; end; format date date9.; run; %let n=8; %let m=6; data want; set have; by firm; array t{0:%eval(&n.-1)} _temporary_; if first.firm then do; n=0; call missing(of t{*});end; n+1; t{mod(n,&n.)}=x; if n ge &n. and n(of t{*}) ge &m. then moving_mean=mean(of t{*}); drop n; run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.