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

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1109 views
  • 1 like
  • 3 in conversation