****************************************************************************************************
*                                           PROGRAM OVERVIEW
****************************************************************************************************
*
* PROGRAM: ms_shaveoutside.sas  
*
* Created (mm/dd/yyyy): 07/31/2014
* Last modified: 07/31/2014
* Version: 1.0
*
*--------------------------------------------------------------------------------------------------
* PURPOSE:
* 	This program will identify overlapping claims and trim exposure that is outside of the  
*  	reference period
*
*  Program inputs:                                                                                   
* 	-Dataset containing the period that claims must overlap         
*	-Dataset containing the claims that must overlap the reference period 
*
*  Program outputs:                                                                                                                                       
*  	-Dataset containing the shaved claims
* 
* PARAMETERS:
*  	-reffile		= Dataset containing the periods that claims must overlap
*   -refstart		= variable name containing the date claims cannot go before
*   -refend			= Variable name containing the date claims cannot go beyond
*	-KeepPartBf		= Indicator to determine if claims with a starting date prior to the start of the 
					  reference period should be kept.
*   -ToShaveFile	= Dataset containing the claims that must overlap the reference period
*   -ToShaveStart	= Variable containing the begin date of the claims to be shaven
*   -ToShaveEnd		= Variable containing the end date of the claims to be shaven
*   -outfile		= Output dataset containing the shaved claims
*
*  Programming Notes:                                                                                
*                                                                           
*
*
*--------------------------------------------------------------------------------------------------
* CONTACT INFO: 
*  Mini-Sentinel Coordinating Center
*  info@mini-sentinel.org
*
*--------------------------------------------------------------------------------------------------
*  CHANGE LOG: 
*
*   Version   Date       Initials	   Comment (reference external documentation when available)
*   -------   --------   --------   ---------------------------------------------------------------
*             mm/dd/yy
*
***************************************************************************************************;


%macro ms_shaveoutside(reffile=,
                       refstart=,
                       refend=,
                       id=,
                       KeepPartBf=N,
                       ToShaveFile=,
                       ToShaveStart=,
                       ToShaveEnd=,
                       outfile=);

    %put =====> MACRO CALLED: ms_shaveoutside v1.0;

    %let id2=;
    %if %str("&id.") ne %str("") %then %let id2=,&id.;

   	%if %str("&reffile.") ne %str("") %then %do;  *need cartesian product first;
 		proc sql noprint;
		create table  _Shave as 
			select in.*,
				   ref.&refstart.,
				   ref.&refend.
                   &id2.
			from &ToShaveFile. as in,
                 &reffile. as ref
		    where in.PatId=ref.PatId and 
                  ref.&refstart. <= in.&ToShaveEnd. and in.&ToShaveStart. <= ref.&refend.;   *Overlap needed to shave;
		quit;
        %let ToShaveFile=_Shave;
    %end;

    data &outfile.;
    	set &ToShaveFile.;
        ToDeductBf=0;
        ToDeductAf=0;
    	if &ToShaveStart. < &refstart. then do; *Starts before reference span;
            ToDeductBf=&refstart.-&ToShaveStart.;
            &ToShaveStart.=&refstart.;
            if "&KeepPartBf"="N" then delete;
        end; 
    	if &ToShaveEnd.   > &refend. then do;   *Ends after reference span;
            ToDeductAf=&ToShaveEnd.-&refend.;
            &ToShaveEnd.=&refend.;     
        end;
    
        if RxAmt > 0 then RxAmt=RxAmt *(RxSup-sum(ToDeductBf,ToDeductAf))/RxSup; 
        if RxSup >=1 then RxSup=&ToShaveEnd.-&ToShaveStart.+1;

    	durat=&ToShaveEnd.-&ToShaveStart.+1;

    	if durat <=0 then delete;
    	drop durat ToDeductBf ToDeductAf;
    run;

   	%if %str("&reffile.") ne %str("") %then %do;  
		proc datasets library=work nolist nowarn;
        *delete  _ToShave;
        quit;
    %end;

    %put NOTE: ********END OF MACRO: ms_shaveoutside v1.0********;


%mend ms_shaveoutside;