I agree on the issue of "holes" in the data series. Here's a modificatioon that accomodates gaps in the date sequence. In effect it acts as if the missing dates were present but VAR1 had a missing value:
data want (drop=_:);
array prior_30{0:29} _temporary_ ;
set have;
by field1 field2 field3 notsorted;
if first.field3 then do;
call missing(of prior_30{*});
_first_date=date;
end;
retain _first_date;
/* See if there are holes between current and preceding dates */
/* If so, assign missing values to corresponding array elements */
_date_increment=dif(date);
if _date_increment>1 then do _d=1 to _date_increment-1;
prior_30{mod(date-_d,30)}=.;
end;
if date>=_first_date+30 then maxvar=max(of prior_30{*});
prior_30{mod(date,30)}=var;
run;
... View more