****************************************************************************************************
*                                           PROGRAM OVERVIEW
****************************************************************************************************
*
* PROGRAM: ms_extractanniversarydate.sas  
*
* Created (mm/dd/yyyy): 07/17/2017
* Last modified: 
* Version: 1.0
*
*--------------------------------------------------------------------------------------------------
* PURPOSE:
*	This program extracts enrollment and demographic claims based on either age or fixed calendar date,
*	then creates claim based on valid date. 
*
*  Program inputs:                                                                                   
*  	-ENR_&GROUP.
*
*  Program outputs:                                                                                                                                       
*  	-dataset containing the extracted records
* 
* PARAMETERS:
* 	-datafile	= Name of the dataset to containing enrollment and birth date records.
*	-lookfile	= Name of the sas dataset containing the list of dates to extract.
*  	-lookvar	= Name of the variable containing the dates to extract.
*  	-outfile	= Name of the output dataset containing the extracted records.
*
*  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_extractanniversarydate(datafile=, lookfile=, lookvar=, outfile=);

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

	/*Index date defined by Fixed Calendar Date*/
	data _fixeddate(drop=&lookvar.);
		set &lookfile.(where=(codecat='DT'));
		date= input(&lookvar., date9.);
	run;

	%isdata(dataset=_fixeddate);
	%if %eval(&nobs.>0) %then %do;
		*create claim for each date;
		proc sql noprint;
			create table _fixeddateclaim as 
			select in.patid,
				   in.enr_start,
				   in.enr_end,
				   codes.date as adate length 4 format date9.,
				   codes.*
			from &datafile. as in,
				_fixeddate as codes
		    where codes.date <= enr_end and enr_start <= codes.date ;  
		quit;
	%end;
	%else %do;
		data _fixeddateclaim;
	        length codetype $3. adate 4;
	        set &lookfile.(obs=0 drop=code);
			adate = .;
        run;
	%end;

	/*Index date defined by Age Anniversary*/
	data _ageanniversary;
		set &lookfile.(where=(codecat='AN'));
		agecode= input(&lookvar., 4.);
	run;

	%isdata(dataset=_ageanniversary);
	%if %eval(&nobs.>0) %then %do;
		
		/*For each age, calculate date and create claim on date*/
		proc sql noprint;
			create table _ageanniversaryclaim as 
			select in.patid,
				   in.enr_start,
				   in.enr_end,
				   in.birth_date,
				   codes.*,
				   case when upcase(codetype) = 'Y' then 365.25
				   		when upcase(codetype) = 'M' then 30.5
						when upcase(codetype) = 'W' then 7
						when upcase(codetype) = 'D' then 1
						else .
					end as multiplier
			from &datafile. as in,
				_ageanniversary as codes;
		quit;

		data _ageanniversaryclaim;
			length adate 4;
			format adate date9.;
			set _ageanniversaryclaim;
			
			adate = floor(birth_date + (agecode*multiplier));

			if (adate <= enr_end and enr_start <= adate);
	
			drop birth_date multiplier;
		run;
	%end;
	%else %do;
		data _ageanniversaryclaim;
	        length codetype $3. adate 4;
	        set &lookfile.(obs=0 drop=code);
			adate = .;
        run;
	%end;

	data &outfile.;
		set _ageanniversaryclaim _fixeddateclaim;
	run;

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

%MEND ms_extractanniversarydate;