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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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