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

Hi guys, I know that this might be a naive question, but let's say that I have the following data.

 

Client_id         Purchase_date

1                           1/02/2020

2                           4/02/2020

1                           5/02/2020

1                           7/02/2020

2                           9/02/2020

1                          11/02/2020

 

How could I use SAS create a new column with the number of days since last purchase for clients with the same ID?

The desired output would be:

 

Client_id         Purchase_date           days_since_last_purchase

1                           1/02/2020                 0

2                           4/02/2020                 0

1                           5/02/2020                 4

1                           7/02/2020                 2

2                           9/02/2020                 5

1                          11/02/2020                4

1 ACCEPTED SOLUTION

Accepted Solutions
japelin
Rhodochrosite | Level 12

how about this

data have;
  input Client_id Purchase_date:mmddyy10.;
  format Purchase_date mmddyys10.;
datalines;
1  1/02/2020
2  4/02/2020
1  5/02/2020
1  7/02/2020
2  9/02/2020
1 11/02/2020
;
run;

proc sort data=have out=sorted;
  by client_id Purchase_date;
run;

data want;
  set sorted;
  by client_id Purchase_date;
  retain dt;
  if first.client_id then do;
    days_since_last_purchase=0;
  end; else
  do;
    days_since_last_purchase=intck('day',dt,Purchase_date);
  end;
  dt=Purchase_date;
  drop dt;
run;

View solution in original post

4 REPLIES 4
pink_poodle
Barite | Level 11
This is a difference in days (intk function) between date and lag(date) in groups by id and date.
macedonataan
Fluorite | Level 6
Thanks for the reply.

would you be able to draft how the code would look like?
japelin
Rhodochrosite | Level 12

how about this

data have;
  input Client_id Purchase_date:mmddyy10.;
  format Purchase_date mmddyys10.;
datalines;
1  1/02/2020
2  4/02/2020
1  5/02/2020
1  7/02/2020
2  9/02/2020
1 11/02/2020
;
run;

proc sort data=have out=sorted;
  by client_id Purchase_date;
run;

data want;
  set sorted;
  by client_id Purchase_date;
  retain dt;
  if first.client_id then do;
    days_since_last_purchase=0;
  end; else
  do;
    days_since_last_purchase=intck('day',dt,Purchase_date);
  end;
  dt=Purchase_date;
  drop dt;
run;
pink_poodle
Barite | Level 11
@japelin, could you please elaborate about the variable dt? What is it?

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
  • 4 replies
  • 524 views
  • 0 likes
  • 3 in conversation