BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Eugenio211
Quartz | Level 8

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.

1 ACCEPTED SOLUTION

Accepted Solutions
vellad
Obsidian | Level 7

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;

 

View solution in original post

2 REPLIES 2
vellad
Obsidian | Level 7

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;

 

Eugenio211
Quartz | Level 8
Thank you, It worked as I expected.

AI Navigator GIF.gif


Navigate AI governance with SAS tools for compliance and ethical AI practices. Try SAS Ai Navigator for free for the first six months.

Check it out on Microsoft Marketplace →

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 3131 views
  • 2 likes
  • 2 in conversation