Hello, hope everyone is safe.
I have this data set and I want to flag a Customer_Key that have 3 consecutive datediff of less 1. What I'm trying to get is a customer that have a 3 or more transactions within a 2 day period. Thank you in advance.
Try this (made a small edit - see comment):
libname here '.';
proc import datafile="Data_Example.xlsx" out=have dbms=xlsx replace;
run;
proc sort;
by customer_key;
run;
data want;
retain window cnt;
set have;
by customer_key;
if first.customer_key then do;
cnt=0; window=0;
end;
if window + datediff <= 2 then do;
cnt=cnt+1; window = window + datediff;
end;
else do;
window=0; cnt = 1; /*changed cnt=0 to cnt=1*/
end;
if cnt >= 3 then flag='Y';
run;
Try this (made a small edit - see comment):
libname here '.';
proc import datafile="Data_Example.xlsx" out=have dbms=xlsx replace;
run;
proc sort;
by customer_key;
run;
data want;
retain window cnt;
set have;
by customer_key;
if first.customer_key then do;
cnt=0; window=0;
end;
if window + datediff <= 2 then do;
cnt=cnt+1; window = window + datediff;
end;
else do;
window=0; cnt = 1; /*changed cnt=0 to cnt=1*/
end;
if cnt >= 3 then flag='Y';
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.