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=.;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.