The DATA stepis very good at working sequences, so use that.
First, let us read the data so that we have usable SAS dates:
data item1;
input ID $ Transaction_number $ Date1 :yymmdd8.;
format date1 yymmdd10.;
datalines;
1 1000 20190112
1 1001 20190121
1 1002 20200111
2 1003 20190102
2 1004 20200110
3 1005 20210123
5 1006 20210102
6 1007 20200101
;
data item2;
input ID $ Transaction_number $ Date2 :yymmdd8.;
format date2 yymmdd10.;
datalines;
1 2000 20190211
1 2001 20210102
2 2002 20200521
3 2003 20210101
4 2004 20200101
5 1006 20210102
5 2005 20210202
;
data item3;
input ID $ Transaction_number $ Date3 :yymmdd8.;
format date3 yymmdd10.;
cards;
1 3000 20210411
1 3001 20200102
2 3002 20200521
3 3003 20200101
4 3004 20190101
5 3006 20200102
5 3005 20210202
;
Next, combine all datasets into one and sort:
data all;
length dname $41;
set
item1 (rename=(date1=date))
item2 (rename=(date2=date))
item3 (rename=(date3=date))
indsname=dname
;
length item $32;
item = scan(dname,2,".");
run;
proc sort data=all;
by id date;
run;
If the individual dataset are already sorted by id and date, you can use a BY in the DATA step to "interleave" the observations, so you do not need the extra sort.
Now, a data step will find your hits:
data want;
set all;
by id;
retain yn_2 yn_3 date_1;
if first.id
then do;
yn_2 = 0;
yn_3 = 0;
date_1 = .;
end;
if date_1 = .
then do;
if item = "ITEM1" then date_1 = date;
end;
else do;
diff = intck('year',date_1,date,"c");
if diff le 1
then do;
if item = "ITEM2" then yn_2 = 1;
if item = "ITEM3" then yn_3 = 1;
end;
end;
if last.id;
keep id yn_2 yn_3;
run;
To see the inner workings of this step, comment the last two statements, so all observations and variables can be seen.
... View more