Hello,
Not sure if I'm understanding you logic well, but I think that using BY-groups for your ID variable , and adding some if-the-else statements could get you pretty far.
data have;
infile cards;
input ID Training_T : $3. Date_start : mmddyy10. Date_End : mmddyy10. ;
last_day= intnx('month',Date_Start,0,'end');
Months_F=(intck('day', Date_start, Date_End)+1) / day(last_day);
format Date_start date9.
Date_End date9.
last_day date9.;
drop last_day ;
cards;
1 IT 1/1/2019 1/31/2019
1 BT 2/1/2019 2/08/2019
1 IT 2/9/2019 2/28/2019
2 BT 11/1/2018 11/11/2018
2 IT 11/12/2018 11/30/2018
2 IIT 12/1/2018 12/31/2018
2 BT 1/1/2019 1/31/2019
2 IIT 2/1/2019 2/28/2019
3 IT 10/1/2018 10/31/2018
3 IT 11/1/2018 11/30/2018
3 IT 1/1/2019 1/31/2019
3 IT 2/1/2019 2/28/2019
3 IT 12/1/2019 12/31/2019
;
run;
and here is a data step with by groups:
data want;
set have;
by ID;
if first.id then sum_temp=0;
Prev_Training=LAG(Training_T);
if training_t ne 'IT' then Prev_Training='';
if Training_T ne 'IT' then sum_temp + Months_F;
if Training_t = 'IT' then Sum_Prev_Training = sum_temp;
drop sum_temp ;
run;
and this is the result:
as the ID "3" has only taken IT training, it is not clear how you want to process that data ?
Greetings,
Mathias.
... View more