A slight variation on what you ask turns a difficult problem into an easy one. Consider this approach and whether it would suit your needs. The set-up is up to you: your data set must be sorted by ID and DATE1, and DATE1 must actually be a SAS date and not a character string. From that point:
data want;
set have;
by id;
prior_date = lag(date);
interval = date - prior_date;
if first.id then do;
interval = .;
prior_date = .;
interval_count = 0;
end;
if interval >= 46 and interval_count=0 then do;
interval_count + 1;
flag = 1;
end;
format prior_date date11.;
drop interval_count;
run;
The difference between what you ask and what the program does: the flag appears on the SECOND date of the interval you are seeking. However, the observation contains the variable PRIOR_DATE, which holds the first date of the interval. So it should support what you need to do. For your second bullet point, SAS has many ways to count. This would be one of them:
proc freq data=want;
tables ID;
where flag = 1;
run;
... View more