BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

I want to calculate the difference between two dates by row per customer_id.

Please find the raw data


data a;
  input CUSTOMER_ID date anydtdte23.;
  format date date9.;
cards;
1057086 21AUG2014
1057086 25AUG2014
1057086 17SEP2014
1057086 17SEP2014
1057086 19SEP2014
1057086 26SEP2014
9888221 01Feb2014
9888221 23Apr2014
9888221 23Apr2014
9888221 26May2014
9888221 27May2014
;
Run;
4 REPLIES 4
Ronein
Onyx | Level 15

Thank you.

For second customer 9888221 in first row I get -237 instead of null.

Why?

data a;
  input CUSTOMER_ID date anydtdte23.;
  format date date9.;
cards;
1057086 21AUG2014
1057086 25AUG2014
1057086 17SEP2014
1057086 17SEP2014
1057086 19SEP2014
1057086 26SEP2014
9888221 01Feb2014
9888221 23Apr2014
9888221 23Apr2014
9888221 26May2014
9888221 27May2014
;
Run;

proc sort data=a; by customer_id date; run;

Data b;
set a;
days_between = dif(date);
IF first.customer_id then days_between=.; 
Run;
Ksharp
Super User
Data b;
set a;


by customer_id ;

days_between = dif(date); IF first.customer_id then days_between=.; Run;

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 4 replies
  • 1088 views
  • 1 like
  • 3 in conversation