****************************************************************************************************
*                                           PROGRAM OVERVIEW
****************************************************************************************************
*
* PROGRAM: ms_loopdth.sas  
*
* Created (mm/dd/yyyy): 01/19/2022
*
*--------------------------------------------------------------------------------------------------
* PURPOSE:
* 	This program will extract death and cause of death records associated with the cohort of interest. 
*
*  Program inputs:                                                                                   
* 	-datasets containing the claims  
*	-dataset containing the lookup parameters
* 
*  Program outputs:                                                                                                                                                            
*	-one dataset with the extracted claims
*
* PARAMETERS:
*	-deathdatafile   = Name of the the dataset(s) containing the death raw claims 
*	-coddatafile   	 = Name of the the dataset(s) containing the cause of death raw claims 
*   -lookfile	     = Name of the dataset containing the lookup parameters
*   -keepvars        = List of variables in the lookfile dataset to keep in the outfile dataset
*   -outfile		 = Name of the dataset with the extracted claims 
*
*--------------------------------------------------------------------------------------------------
* CONTACT INFO: 
*  Sentinel Coordinating Center
*  info@sentinelsystem.org
*
***************************************************************************************************;

%macro ms_loopdth(deathdatafile=, coddatafile=, lookfile=, keepvars=, outfile=);

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

    %isdata(dataset=&lookfile.);
	%if %eval(&nobs.>=1) %then %do;
	    proc sql noprint;
        create table &outfile. (drop = CodeType EncType) as
        select claim.patid,
			   claim.DeathDt,
			   codes.*
        from &deathdatafile. as claim,
             &lookfile. (keep = &keepvars.) as codes
		where ((codes.CodeType = "DT" and claim.death_source in ("death","both")) or
			   (codes.CodeType = "ET" and claim.death_source in ("enc","both")) ) and
               (index((upcase(claim.death_enctype)),codes.EncType) > 0 or  /*No Wilcards*/
               (codes.EncType = "**"));  /*Any EncType*/  
        quit;

		%isdata(dataset=&coddatafile.);
		%if %eval(&nobs.>0) %then %do;
			proc sql noprint;
	        create table _cod (drop = EncType) as
	        select claim.patid,
				   claim.DeathDt,
				   codes.*
	        from &coddatafile. as claim,
	             &lookfile. (keep = &keepvars. codecat code) as codes
			where codes.CodeType = claim.CodeType and codes.Code = claim.Code;
	        quit;

			data &outfile.;
			set &outfile.
				_cod;
			run;

			proc datasets nowarn nolist lib=work;
			delete _cod;
			quit;
		%end;

		proc sort data=&outfile. nodupkey;
		by _ALL_;
		run;
    %end;
    %else %do;
        data &outfile. (drop = CodeType EncType);
        set &deathdatafile.(obs=0 keep=patid DeathDt) &lookfile.(obs=0 keep = &keepvars.);
        run;
    %end;

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

%mend ms_loopdth;