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.

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