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

I want to calculate difference in dates in consecutive rows by the ID variable. That is, for the first occurrence of the ID value the difference should be missing. I tried with the following code with no luck.

data date;
   input ID DATE;
   format date date10.;
datalines;
1	44562
1	44563
1	44564
1	44583
1	44584
1	44585
1	44586
2	44960
2	44961
2	44962
2	44963
2	44964
2	44965
2	44966
2	44972
2	44973
3	44974
3	44975
3	44976
3	44977
3	44978
3	44979
;
run;

data date1;
set date;
by id;
difference=dif(date); run;
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data date1;
   set date;
   by id;
   difference=dif(date); 
   if first.id then difference=.;
run;
--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26
data date1;
   set date;
   by id;
   if first.id then difference=.;
   else difference=dif(date); 
run;
--
Paige Miller
BayzidurRahman
Obsidian | Level 7

It didn't work.

Obs ID DATE difference 
1 1 02JAN2082 . 
2 1 03JAN2082 . 
3 1 04JAN2082 1 
4 1 23JAN2082 19 
5 1 24JAN2082 1 
6 1 25JAN2082 1 
7 1 26JAN2082 1 
8 2 04FEB2083 . 
9 2 05FEB2083 375 
10 2 06FEB2083 1 
11 2 07FEB2083 1 
12 2 08FEB2083 1 
13 2 09FEB2083 1 
14 2 10FEB2083 1 
15 2 16FEB2083 6 
16 2 17FEB2083 1 
17 3 18FEB2083 . 
18 3 19FEB2083 2 
19 3 20FEB2083 1 
20 3 21FEB2083 1 
21 3 22FEB2083 1 
22 3 23FEB2083 1 
PaigeMiller
Diamond | Level 26
data date1;
   set date;
   by id;
   difference=dif(date); 
   if first.id then difference=.;
run;
--
Paige Miller

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 172 views
  • 0 likes
  • 2 in conversation