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;
data date1;
set date;
by id;
difference=dif(date);
if first.id then difference=.;
run;
data date1;
set date;
by id;
if first.id then difference=.;
else difference=dif(date);
run;
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
data date1;
set date;
by id;
difference=dif(date);
if first.id then difference=.;
run;
Thanks.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.