****************************************************************************************************
*                                           PROGRAM OVERVIEW
****************************************************************************************************
*
* PROGRAM: ms_envelope.sas  
* Created (mm/dd/yyyy): 07/31/2014
*
*--------------------------------------------------------------------------------------------------
* PURPOSE:
*   This program uses data from the SDD to recode non-inpatient encounters to inpatient if they were   
*  	determined to have occurred during an inpatient stay.  Please note that this operation is done      
*  	without using the ENCOUNTERID variable, as this variable can be inconsistent across data            
*  	partner datasets.                                       
*   
*  Program inputs:                                                                                   
*	-SAS data file with diagnosis/procedure claims data      
*	-SAS data file with encounter data  
* 
*  Program outputs:                                                                                                                                       
*	-SAS data file (.SAS7BDAT format) with reclassified claims 
* 
*  PARAMETERS:                                                                       
*	-INFILE			 = Name of SAS dataset with diagnosis and/or procedure claims data    			  
*	-ENCOUNTERINFILE = Name of SAS dataset with encounter data           							  
*	-OUTFILE		 = Name of output file with reclassified claims      
* 
*  Programming Notes:                                                                                
*
*
*--------------------------------------------------------------------------------------------------
* CONTACT INFO: 
*  Sentinel Coordinating Center
*  info@sentinelsystem.org
*
***************************************************************************************************;

%macro MS_ENVELOPE(INFILE=,ENCOUNTERINFILE=,OUTFILE=);       

%put =====> MACRO CALLED: ms_envelope;

	*Reclassification as inpatient all selected claims within admission and discharge dates of an inpatient stay;
	*Get Unique ADate-Ddate combinations;
	proc sort nodupkey data=&ENCOUNTERINFILE.(keep=PatId Adate Ddate EncType where=(EncType='IP')) 
	                 out=_IPdates(keep=PatId Adate Ddate);
	by PatId Adate Ddate;
	run;     

	*Assessing claim number for unique record merging;
    data &OUTFILE. 
		 _Not_IP(keep=PatId ADate Clm);
    	set &INFILE.;
    	Clm = _N_; 
    	if EncType ne 'IP' then output _Not_IP;
    	output &OUTFILE.;
	run;

	%let sign = %str(<=);
	%if &run_envelope. = 1 %then %let sign = %str(<); 

	proc sql noprint;
    	create table _datematch as
    	select distinct claimtb.Clm 
    	from _Not_IP as claimtb,
    	     _IPDates as datetb
    	where claimtb.PatId = datetb.PatId and 
    	      datetb.ADate &sign. claimtb.ADate <=  max(datetb.ADate,datetb.DDate)
        order by clm;
	quit;

	*For each record in INFILE matching an inpatient day date, recode encounter type as inpatient;
	data &OUTFILE.(drop=Clm);
    	merge &OUTFILE.(in=a) _datematch(in=b);
    	by Clm;
    	if b then do;
    		pdx='X';
    	    EncType='IP';                      
		end;
	run;              

	proc datasets library = work nolist nowarn nodetails;
	delete _IPDates _datematch _not_ip;
	quit;       

%put NOTE: ******** END OF MACRO: ms_envelope ********;

%mend MS_ENVELOPE;