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=.;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.