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
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;
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.