Hi,
I need to know how to code the difference in days between two dates within each group of observations. The constant observation that will be compared to other observations will be the 1st. I will need to code for the first observation and the second observation difference in days, the first observation and third observation in difference in days, the 1st and 3rd, and so on. For instance......
proc sort data=have out=need;
by customer descending date;
run;
Customer Date DateDiff
1 2018-08-13
1 2018-06-24 ? Compared to 1st (ex.54)
1 2017-08-14 ? Compared to 1st (ex.364)
2 2017-08-23
2 2016-04-25 ? Compared to 1st in group2
2 2016-03-02 ? Compared to 1st in group2
something like this?
data have;
input Customer Date : yymmdd10. ;
format date yymmdd10.;
cards;
1 2018-08-13
1 2018-06-24
1 2017-08-14
2 2017-08-23
2 2016-04-25
2 2016-03-02
;
data want;
set have;
by customer;
retain p;
if first.customer then p=date;
else datediff=intck('day',date,p);
drop p;
run;
something like this?
data have;
input Customer Date : yymmdd10. ;
format date yymmdd10.;
cards;
1 2018-08-13
1 2018-06-24
1 2017-08-14
2 2017-08-23
2 2016-04-25
2 2016-03-02
;
data want;
set have;
by customer;
retain p;
if first.customer then p=date;
else datediff=intck('day',date,p);
drop p;
run;
Note that to manipulate date information you want the dates to be actual SAS date values. If your variables are character then you need to create an additional variable to hold the actual date. If your current variable is numeric and with a SAS format such as yymmdd10. applied then that is a SAS date value. If the format is $10. (or anything starting with $) then it is character.
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 16. 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.