Data have;
Input id $ date1;
Datalines;
101 20-Oct-2016
101 15-Nov-2016
101 25-Jan-2017
101 6-May-2017
102 10-Oct-2016
102 25-Nov-2016
102 15-Jan-2017
102 26-May-2017
103 27-Oct-2016
103 05-Nov-2016
103 25-Jan-2017
103 16-Feb-2017
;
run;
I want achieve two things:
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;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.