BookmarkSubscribeRSS Feed
deedums
Calcite | Level 5

Hi! I have a dataset which includes person IDs and one record for each of their transactions, looking something like this:

UINPURCHASEIDCREATIONDATE
ID_00456063611-May-17
ID_00736064613-Aug-17
ID_00736317928-Dec-17
ID_00736400107-Aug-18
ID_00846195201-Aug-17
ID_00846053901-Jun-18
ID_00926068613-May-17
ID_00926330228-May-18
ID_00926046507-Aug-18

 

 

I want to create a code that flags when a person has made a purchase both in this past 12 months, and as well as having made a purchase during the previous 12 months, so that it may look something like this:

 

UINPURCHASEIDCREATIONDATEFLAG
ID_00456063611-May-170
ID_00736064613-Aug-171
ID_00736317928-Dec-171
ID_00736400107-Aug-181
ID_00846195201-Aug-170
ID_00846053901-Jun-180
ID_00926068613-May-171
ID_00926330228-May-181
ID_00926046507-Aug-181

 

I'm not sure how exactly to go about this. I tried to use a dataset grouped by UIN (the dataset is already sorted by UIN) but this did not work as each record only has one creationdate. And, this is working from a fixed point in time so would not work for my whole dataset anyway.

 

data purchases2;
set purchases1;
	by UIN;
		if (creationdate between '08aug2017'd and '07aug2018'd) 
		and (creationdate between '08aug2016'd and '07aug2017'd)
		then flag=1; else flag=0;
run;

 

This seems quite complex but I'm hoping there's a simple solution!

 

Thank you! 🙂

6 REPLIES 6
data_null__
Jade | Level 19

I think you need a flag for each condition.

 

data purchase;
   input UIN $ PURCHASEID $ CREATIONDATE :date9.;
   format CR: date11.;
   cards;
ID_0045 60636 11-May-17 
ID_0073 60646 13-Aug-17 
ID_0073 63179 28-Dec-17 
ID_0073 64001 07-Aug-18 
ID_0084 61952 01-Aug-17 
ID_0084 60539 01-Jun-18 
ID_0092 60686 13-May-17 
ID_0092 63302 28-May-18 
ID_0092 60465 07-Aug-18 
   run;
proc print;
   run;
data who;
   do until(last.uin);
      set purchase;
      by uin;
      if '08aug2017'd le creationdate le '07aug2018'd then flag1=1;
      if '08aug2016'd le creationdate le '07aug2017'd then flag2=1;
		end;
   if flag1 and flag2;
   keep uin;
   run;
proc print;
   run;
      

Capture.PNG

 

gamotte
Rhodochrosite | Level 12

Hello,

 

Shouldn't the flag also be 1 for ID 84 ?

 

With proc sql :

proc sql noprint;
    CREATE TABLE want AS
    SELECT *,
           CASE WHEN sum(CREATIONDATE BETWEEN '08aug2017'd AND '07aug2018'd)
                 AND sum(CREATIONDATE BETWEEN '08aug2016'd AND '07aug2017'd)
           THEN 1 ELSE 0 END AS FLAG
    FROM have
    GROUP BY UIN    
    ;
quit;

Edit : Corrected typo  : 07aug2017 instead of 07aug2018 in the second interval.

deedums
Calcite | Level 5

Hi!

 

Thank you for your solution! 🙂

 

No ID 84 wouldn't be flagged as I would want the flag to show when the UIN has made a purchase in BOTH the past twelve months AND the previous twelve months. ID84 only made purchases in the past 12 months. 

 

gamotte
Rhodochrosite | Level 12
What about PURCHASEID 61952 ? 01-Aug-2017 is between 08-Aug-2016 and 07-Aug-2017 so shouldn't it be counted as a purchase in the previous twelve months ?
deedums
Calcite | Level 5

Sorry for the confusion, I'd meant the 12 months leading up to the final purchase per ID. 

gamotte
Rhodochrosite | Level 12

OK but why is ID 73 flagged ?

 

proc sql noprint;
    CREATE TABLE have2 AS
    SELECT *, max(CREATIONDATE) AS FINALPURCHASE format=date9.
    FROM have
    GROUP BY UIN;

    CREATE TABLE want AS
    SELECT *, CREATIONDATE BETWEEN intnx('year',FINALPURCHASE,-1,'s') AND FINALPURCHASE-1 AS LAST_12,
           CREATIONDATE BETWEEN intnx('year',FINALPURCHASE,-2,'s')AND intnx('year',FINALPURCHASE-1,-1,'s') AS PREV_12,
           CASE WHEN sum(CREATIONDATE BETWEEN intnx('year',FINALPURCHASE,-1,'s') AND FINALPURCHASE-1)
                 AND sum(CREATIONDATE BETWEEN intnx('year',FINALPURCHASE,-2,'s')AND intnx('year',FINALPURCHASE-1,-1,'s'))
           THEN 1 ELSE 0 END AS FLAG
    FROM have2
    GROUP BY UIN    
    ;
quit;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 670 views
  • 0 likes
  • 3 in conversation