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

I would like to subtract the second IncarcerationDate minus the first ReleaseDate based on ID. For example there is an ID with the number 1 with eight different start dates and end dates, Id 2 has one record, id 3 has four records. I would like a new variable "DaysAtHome", substract first.id ReleaseDate from second.id IncarcerationDate and for last record (last.id) susbtract from SysDate(Today's Date) -ReleaseDate. Any help would be greatly appreciated. Thanks. 

 

Here is a dataset:

data incarceration;
input Id $
IncarcerationDate :mmddyy10.
ReleaseDate :mmddyy10.;
format IncarcerationDate ReleaseDate mmddyy10.;
datalines;
1 08/12/2019 08/13/2019
1 08/20/2019 10/02/2019
1 10/31/2019 11/01/2019
1 11/07/2019 11/08/2019
1 11/29/2019 01/02/2020
1 01/15/2020 03/25/2020
1 08/05/2020 08/08/2020
1 10/21/2020 01/07/2021
2 01/21/2018 01/23/2018
3 11/07/2018 01/07/2019
3 04/25/2018 05/09/2018
3 08/14/2020 08/17/2020
3 03/22/2018 04/11/2018
;
run;

 

I want a new Variable "DaysAtHome":

IdIncarcerationDateReleaseDateDaysAtHome 
18/12/20198/13/201978/20/2019 - 8/13/2019
18/20/201910/2/20192910/31/2019 - 10/02/2019
110/31/201911/1/20196 
111/7/201911/8/201921 
111/29/20191/2/202013 
11/15/20203/25/2020133 
18/5/20208/8/202074 
110/21/20201/7/20211317today's date -01/07/2021
21/21/20181/23/20182397today's date -01/23/2018
33/22/20184/11/201814 
34/25/20185/9/2018182 
311/7/20181/7/2019585 
38/14/20208/17/20201460 

 

Wrote few codes but did not get the results.

proc sort data=incarceration;
by Id IncarcerationDate;

run;

 

data days_home;
set incarceration;
by Id;


SysDate = today();

 

prevReleaseDate=lag(ReleaseDate);
if not last.id then DaysAtHome=IncarcerationDate-prevReleaseDate;
if last.Id then DaysAtHome = SysDate - ReleaseDate;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Two ways:

  • sort the dataset in reverse chronological order, so you can use LAG, then re-sort to the original order
  • or do a look-ahead:
data days_home;
merge
  incarceration
  incarceration (
    firstobs=2
    keep=id incarcerationdate
    rename=(
      id=_id
      incarcerationdate=_inc
    )
  )
;
if id = _id
then daysathome = _inc - releasedate;
else daysathome = today() - releasedate;
drop _id _inc;
run;

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

Two ways:

  • sort the dataset in reverse chronological order, so you can use LAG, then re-sort to the original order
  • or do a look-ahead:
data days_home;
merge
  incarceration
  incarceration (
    firstobs=2
    keep=id incarcerationdate
    rename=(
      id=_id
      incarcerationdate=_inc
    )
  )
;
if id = _id
then daysathome = _inc - releasedate;
else daysathome = today() - releasedate;
drop _id _inc;
run;
dnbhatta
Calcite | Level 5

Thank you so much, I appreciate.