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