Hi there. I am trying to subtract dates by row as shown here:
PlayerID | Date |
1001 | 12JUN2020 |
1001 | 11JUN2020 |
1002 | 24AUG2020 |
1002 | 13JUL2020 |
1003 | 17MAY2021 |
What I want is this:
PlayerID | Date | DateDiff |
1001 | 12JUN2020 | . |
1001 | 11JUN2020 | 1 |
1002 | 24AUG2020 | . |
1002 | 13JUL2020 | 42 |
However, when I run this code
data table;
input PlayerID$ Date:date9.;
format Date date9.;
datalines;
1001 12JUN2021
1001 11JUN2021
1002 24AUG2020
1002 13JUL2020
;
pro sort data = table;
by PlayerID Date DESC;
run;
data datediff;
set table;
by PlayerID;
daydiff=abs(dif(Date));
if not first.PlayerID then output;
run;
It substracts the previous date regardless like this:
PlayerID | Date | DateDiff |
1001 | 12JUN2020 | |
1001 | 11JUN2020 | 1 |
1002 | 24AUG2020 | 223 |
1002 | 13JUL2020 | 42 |
Any ideas on how to resolve this?
daydiff=abs(dif(Date));
if first.PlayerID then daydiff=.;
daydiff=abs(dif(Date));
if first.PlayerID then daydiff=.;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.