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

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

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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;

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20

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;
ballardw
Super User

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.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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