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

Hello,
I am a new SAS user.
I want to calculate the date difference by group

I have the following data:

 

Company_ID Date
1001 05JAN2021
1001 06JAN2021
1001 07JAN2021
1001 08JAN2021
1001 11JAN2021
1002 18FEB2021
1002 21FEB2021
1002 22FEB2021

 

I want the following output:

 

Company_IDDateDate_Diff
10012021-01-05.
10012021-01-061
10012021-01-071
10012021-01-081
10012021-01-113
10022021-02-18.
10022021-02-213
10022021-02-221

 

Thank you so much for your kind support!

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Use the DIF function and set a missing value when the group changes:

data want;
set have;
by company_id;
date_diff = dif(date);
if first.company_id then date_diff = .;
run;

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

Use the DIF function and set a missing value when the group changes:

data want;
set have;
by company_id;
date_diff = dif(date);
if first.company_id then date_diff = .;
run;
mmh
Obsidian | Level 7 mmh
Obsidian | Level 7
Thank you so much Kurt!
It was really kind of you 🙂
I was just curious. If I wanted to get the difference between months instead, then I should use the following? Or is there a better way?

data want;
set have;
by Company_ID;
mon_diff = intck('month',Date,Date-1);
if first.Company_ID then mon_diff = .;
run;

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 513 views
  • 1 like
  • 2 in conversation