hello again @andreas_lds ; I almost succeed what I wanted to produce but as you can see, it's very repetitive and not automatic. I've tried to use " do loop" to reduce the amount of code but unsuccessfuly. If you've any idea on how to achieve the same results in a better way, I'd happy to learn. Thank you 🙂 data Have;
length ID Status $ 40;
input Id Status $ Date_of_effect Date_Status ;
informat Date_of_effect Date_Status date9.;
format Date_of_effect Date_Status date9.;
infile datalines missover;
datalines;
1 Ongoing 05jul2015 05jul2015
1 Transformation 23Oct2014 05jul2015
2 Death 07jan2017 02dec2018
3 Finished 13feb2005 13feb2015
4 NoEffect 06nov2016 06nov2016
5 Cancellation 08MAR2014 19dec2018
6 Pending 21oct2018 21oct2018
;
run;
data Want1 ;
set Have;
if Status = "Ongoing" or Status="Pending" then
Date_Status=today();
Expo_days_2015 = min(mdy(01,01,2016),Date_Status)-max(Date_of_effect,mdy(1,1,2015));
Expo_month_2015 = round((min(mdy(01,01,2016),Date_Status)-max(Date_of_effect,mdy(1,1,2015)))/(365.25/12),1);
if Expo_days_2015 <0 then Expo_days_2015=0;
if Expo_month_2015 <0 then Expo_month_2015=0;
Expo_days_2016 = min(mdy(01,01,2017),Date_Status)-max(Date_of_effect,mdy(1,1,2016));
Expo_month_2016 = round((min(mdy(01,01,2017),Date_Status)-max(Date_of_effect,mdy(1,1,2016)))/(365.25/12),1);
if Expo_days_2016 <0 then Expo_days_2016=0;
if Expo_month_2016 <0 then Expo_month_2016=0;
Expo_days_2017 = min(mdy(01,01,2018),Date_Status)-max(Date_of_effect,mdy(1,1,2017));
Expo_month_2017 = round((min(mdy(01,01,2018),Date_Status)-max(Date_of_effect,mdy(1,1,2017)))/(365.25/12),1);
if Expo_days_2017 <0 then Expo_days_2017=0;
if Expo_month_2017 <0 then Expo_month_2017=0;
Expo_days_2018 = min(mdy(01,01,2019),Date_Status)-max(Date_of_effect,mdy(1,1,2018));
Expo_month_2018 = round((min(mdy(01,01,2019),Date_Status)-max(Date_of_effect,mdy(1,1,2018)))/(365.25/12),1);
if Expo_days_2018 <0 then Expo_days_2018=0;
if Expo_month_2018 <0 then Expo_month_2018=0;
Expo_days_2019 = min(mdy(01,01,2020),Date_Status)-max(Date_of_effect,mdy(1,1,2019));
Expo_month_2019 = round((min(mdy(01,01,2020),Date_Status)-max(Date_of_effect,mdy(1,1,2019)))/(365.25/12),1);
if Expo_days_2019 <0 then Expo_days_2019=0;
if Expo_month_2019 <0 then Expo_month_2019=0;
run;
proc sql noprint;
select distinct sum(Expo_days_2015) into: Expo_days_2015
from work.want1;
select distinct sum(Expo_days_2016) into: Expo_days_2016
from work.want1;
select distinct sum(Expo_days_2017) into: Expo_days_2017
from work.want1;
select distinct sum(Expo_days_2018) into: Expo_days_2018
from work.want1;
select distinct sum(Expo_days_2019) into: Expo_days_2019
from work.want1;
select distinct sum(Expo_month_2015) into: Expo_month_2015
from work.want1;
select distinct sum(Expo_month_2016) into: Expo_month_2016
from work.want1;
select distinct sum(Expo_month_2017) into: Expo_month_2017
from work.want1;
select distinct sum(Expo_month_2018) into: Expo_month_2018
from work.want1;
select distinct sum(Expo_month_2019) into: Expo_month_2019
from work.want1;
quit;
run;
%put Expo_days_2015;
%put Expo_days_2016;
%put Expo_days_2017;
%put Expo_days_2018;
%put Expo_days_2019;
%put Expo_month_2015;
%put Expo_month_2016;
%put Expo_month_2017;
%put Expo_month_2018;
%put Expo_month_2019;
data Want2 ( drop=i);
do i = 2015 to 2019;
year= i;
if i =2015 then do;
Expo_days=&Expo_days_2015;
Expo_month=&Expo_month_2015;
end;
else if i = 2016 then do ;
Expo_days=&Expo_days_2016;
Expo_month=&Expo_month_2016;
end;
else if i = 2017 then do ;
Expo_days=&Expo_days_2017;
Expo_month=&Expo_month_2017;
end;
else if i = 2018 then do ;
Expo_days=&Expo_days_2018;
Expo_month=&Expo_month_2018;
end;
else if i = 2019 then do ;
Expo_days=&Expo_days_2019;
Expo_month=&Expo_month_2019;
end;
output;
end;
run;
data Want3 ;
set have;
if Status = "Ongoing" or Status="Pending" then
Date_Status=today();
Expo_1_2015 = min(mdy(2,1,2015),Date_Status)-max(Date_of_effect,mdy(1,1,2015));
Expo_2_2015 = min(mdy(3,1,2015),Date_Status)-max(Date_of_effect,mdy(2,1,2015));
Expo_3_2015 = min(mdy(4,1,2015),Date_Status)-max(Date_of_effect,mdy(3,1,2015));
Expo_4_2015 = min(mdy(5,1,2015),Date_Status)-max(Date_of_effect,mdy(4,1,2015));
Expo_5_2015 = min(mdy(6,1,2015),Date_Status)-max(Date_of_effect,mdy(5,1,2015));
Expo_6_2015 = min(mdy(7,1,2015),Date_Status)-max(Date_of_effect,mdy(6,1,2015));
Expo_7_2015 = min(mdy(8,1,2015),Date_Status)-max(Date_of_effect,mdy(7,1,2015));
Expo_8_2015 = min(mdy(9,1,2015),Date_Status)-max(Date_of_effect,mdy(8,1,2015));
Expo_9_2015 = min(mdy(10,1,2015),Date_Status)-max(Date_of_effect,mdy(9,1,2015));
Expo_10_2015 = min(mdy(11,1,2015),Date_Status)-max(Date_of_effect,mdy(10,1,2015));
Expo_11_2015 = min(mdy(12,1,2015),Date_Status)-max(Date_of_effect,mdy(11,1,2015));
Expo_12_2015 = min(mdy(1,1,2016),Date_Status)-max(Date_of_effect,mdy(12,1,2015));
Expo_1_2016 = min(mdy(2,1,2016),Date_Status)-max(Date_of_effect,mdy(1,1,2016));
Expo_2_2016 = min(mdy(3,1,2016),Date_Status)-max(Date_of_effect,mdy(2,1,2016));
Expo_3_2016 = min(mdy(4,1,2016),Date_Status)-max(Date_of_effect,mdy(3,1,2016));
Expo_4_2016 = min(mdy(5,1,2016),Date_Status)-max(Date_of_effect,mdy(4,1,2016));
Expo_5_2016 = min(mdy(6,1,2016),Date_Status)-max(Date_of_effect,mdy(5,1,2016));
Expo_6_2016 = min(mdy(7,1,2016),Date_Status)-max(Date_of_effect,mdy(6,1,2016));
Expo_7_2016 = min(mdy(8,1,2016),Date_Status)-max(Date_of_effect,mdy(7,1,2016));
Expo_8_2016 = min(mdy(9,1,2016),Date_Status)-max(Date_of_effect,mdy(8,1,2016));
Expo_9_2016 = min(mdy(10,1,2016),Date_Status)-max(Date_of_effect,mdy(9,1,2016));
Expo_10_2016 = min(mdy(11,1,2016),Date_Status)-max(Date_of_effect,mdy(10,1,2016));
Expo_11_2016 = min(mdy(12,1,2016),Date_Status)-max(Date_of_effect,mdy(11,1,2016));
Expo_12_2016 = min(mdy(1,1,2017),Date_Status)-max(Date_of_effect,mdy(12,1,2016));
if Expo_1_2016 <0 then Expo_1_2016 =0;
if Expo_2_2016 <0 then Expo_2_2016 =0;
if Expo_3_2016 <0 then Expo_3_2016 =0;
if Expo_4_2016 <0 then Expo_4_2016 =0;
if Expo_5_2016 <0 then Expo_5_2016 =0;
if Expo_6_2016 <0 then Expo_6_2016 =0;
if Expo_7_2016 <0 then Expo_7_2016 =0;
if Expo_8_2016 <0 then Expo_8_2016 =0;
if Expo_9_2016 <0 then Expo_9_2016 =0;
if Expo_10_2016 <0 then Expo_10_2016=0;
if Expo_11_2016 <0 then Expo_11_2016=0;
if Expo_12_2016 <0 then Expo_12_2016=0;
if Expo_1_2015 <0 then Expo_1_2015 =0;
if Expo_2_2015 <0 then Expo_2_2015 =0;
if Expo_3_2015 <0 then Expo_3_2015 =0;
if Expo_4_2015 <0 then Expo_4_2015 =0;
if Expo_5_2015 <0 then Expo_5_2015 =0;
if Expo_6_2015 <0 then Expo_6_2015 =0;
if Expo_7_2015 <0 then Expo_7_2015 =0;
if Expo_8_2015 <0 then Expo_8_2015 =0;
if Expo_9_2015 <0 then Expo_9_2015 =0;
if Expo_10_2015 <0 then Expo_10_2015 =0;
if Expo_11_2015 <0 then Expo_11_2015 =0;
if Expo_12_2015 <0 then Expo_12_2015 =0;
/*
.
.
.
.
*/
run;
/*%macro loop_expo;
%do i=1 %to 12;
proc sql noprint;
select distinct sum(Expo_&i._2015) into: Expo_&i._2015
from work.want3;
quit;
run;
%end;
%mend;
%loop_expo;*/
proc sql noprint;
select distinct sum(Expo_1_2015) into: Expo_1_2015
from work.want3;
select distinct sum(Expo_2_2015) into: Expo_2_2015
from work.want3;
select distinct sum(Expo_3_2015) into: Expo_3_2015
from work.want3;
select distinct sum(Expo_4_2015) into: Expo_4_2015
from work.want3;
select distinct sum(Expo_5_2015) into: Expo_5_2015
from work.want3;
select distinct sum(Expo_6_2015) into: Expo_6_2015
from work.want3;
select distinct sum(Expo_7_2015) into: Expo_7_2015
from work.want3;
select distinct sum(Expo_8_2015) into: Expo_8_2015
from work.want3;
select distinct sum(Expo_9_2015) into: Expo_9_2015
from work.want3;
select distinct sum(Expo_10_2015) into: Expo_10_2015
from work.want3;
select distinct sum(Expo_11_2015) into: Expo_11_2015
from work.want3;
select distinct sum(Expo_12_2015) into: Expo_12_2015
from work.want3;
select distinct sum(Expo_1_2016) into: Expo_1_2016
from work.want3;
select distinct sum(Expo_2_2016) into: Expo_2_2016
from work.want3;
select distinct sum(Expo_3_2016) into: Expo_3_2016
from work.want3;
select distinct sum(Expo_4_2016) into: Expo_4_2016
from work.want3;
select distinct sum(Expo_5_2016) into: Expo_5_2016
from work.want3;
select distinct sum(Expo_6_2016) into: Expo_6_2016
from work.want3;
select distinct sum(Expo_7_2016) into: Expo_7_2016
from work.want3;
select distinct sum(Expo_8_2016) into: Expo_8_2016
from work.want3;
select distinct sum(Expo_9_2016) into: Expo_9_2016
from work.want3;
select distinct sum(Expo_10_2016) into: Expo_10_2016
from work.want3;
select distinct sum(Expo_11_2016) into: Expo_11_2016
from work.want3;
select distinct sum(Expo_12_2016) into: Expo_12_2016
from work.want3;
quit;
run;
%put Expo_1_2015;
%put Expo_2_2015;
%put Expo_3_2015;
%put Expo_4_2015;
%put Expo_5_2015;
%put Expo_6_2015;
%put Expo_7_2015;
%put Expo_8_2015;
%put Expo_9_2015;
%put Expo_10_2015;
%put Expo_11_2015;
%put Expo_12_2015;
%put Expo_1_2016;
%put Expo_2_2016;
%put Expo_3_2016;
%put Expo_4_2016;
%put Expo_5_2016;
%put Expo_6_2016;
%put Expo_7_2016;
%put Expo_8_2016;
%put Expo_9_2016;
%put Expo_10_2016;
%put Expo_11_2016;
%put Expo_12_2016;
data Want4 ( drop=i j);
do i = 2015 to 2016;
do j= 1 to 12;
year= i;
month= j;
if i =2015 then do;
if j= 1 then
Expo_days = &Expo_1_2015;
else if j=2 then
Expo_days = &Expo_2_2015;
else if j=3 then
Expo_days = &Expo_3_2015;
else if j=4 then
Expo_days=&Expo_4_2015;
else if j=5 then
Expo_days=&Expo_5_2015;
else if j=6 then
Expo_days=&Expo_6_2015;
else if j=7 then
Expo_days=&Expo_7_2015;
else if j=8 then
Expo_days=&Expo_8_2015;
else if j=9 then
Expo_days=&Expo_9_2015;
else if j=10 then
Expo_days=&Expo_10_2015;
else if j=11 then
Expo_days=&Expo_11_2015;
else if j=12 then
Expo_days=&Expo_12_2015;
end;
else if i = 2016 then do ;
if j= 1 then
Expo_days=&Expo_1_2016;
else if j=2 then
Expo_days=&Expo_2_2016;
else if j=3 then
Expo_days=&Expo_3_2016;
else if j=4 then
Expo_days=&Expo_4_2016;
else if j=5 then
Expo_days=&Expo_5_2016;
else if j=6 then
Expo_days=&Expo_6_2016;
else if j=7 then
Expo_days=&Expo_7_2016;
else if j=8 then
Expo_days=&Expo_8_2016;
else if j=9 then
Expo_days=&Expo_9_2016;
else if j=10 then
Expo_days=&Expo_10_2016;
else if j=11 then
Expo_days=&Expo_11_2016;
else if j=12 then
Expo_days=&Expo_12_2016;
end;
output;
end;
end;
run;
... View more