SAS Programming

DATA Step, Macro, Functions and more
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.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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
  • 1348 views
  • 2 likes
  • 2 in conversation