Thanks so much. It worked. I just need to fix minor issues: 1) for cases with activity end date = program start date, reallocate the 1 day that falls under post-program period to the pre-program period, and 2) since it counts both the start and end dates it over-counts by 1 day and I need to tune it as you pointed out. And obviously transpose the data.
For people who are interested, here is the revised code combing pre- and post-cases. I also renamed the variable name (pre/post... to activity_SD and activity_ED, as activity period before and after the intervention).
data have; format id f2.0 Activity_SD Activity_ED Prog_SD Prog_ED yymmdd10.;
input @1 ID @4 Activity_SD MMDDYY10. @15 Activity_ED MMDDYY10. @26 Prog_SD MMDDYY10. @37 Prog_ED MMDDYY10.;
datalines;
1 01/18/2014 12/18/2014 12/18/2014 01/04/2017
2 03/19/2013 02/05/2014 02/18/2014 12/17/2014
3 01/01/2014 05/13/2014 09/08/2014 10/08/2015
4 01/26/2016 02/03/2016 03/03/2016 09/20/2016
5 01/11/2016 01/19/2016 01/19/2016 12/31/2016
6 08/10/2014 08/14/2014 08/14/2014 07/31/2017
11 03/02/2016 03/02/2017 05/29/2014 04/30/2017
12 04/21/2014 04/17/2015 09/23/2013 08/31/2014
13 01/01/2015 02/28/2015 10/01/2014 09/30/2015
14 03/24/2014 03/28/2014 01/13/2014 10/10/2014
15 11/11/2013 01/28/2014 10/01/2013 09/30/2017
;
data want; set have;
format prefrom preto postfrom postto yymmdd10.;
LOS_Period=Activity_ED-Activity_SD;
do n=1 to 12;
prefrom=intnx('month',Prog_SD,-n,'sameday')+1;
preto=intnx('month',Prog_SD,-n+1,'sameday');
postfrom=intnx('month',Prog_SD,n-1,'sameday');
postto=intnx('month',Prog_SD,n,'sameday')-1;
predays=MAX(MIN(Activity_ED,preto)-MAX(Activity_SD,prefrom)+1,0);
postdays=MAX(MIN(Activity_ED,postto)-MAX(Activity_SD,postfrom)+1,0);
output;
end;
run;
... View more