****************************************************************************************************
*                                           PROGRAM OVERVIEW
****************************************************************************************************
*
* PROGRAM: _customize_input.sas
* Created (mm/dd/yyyy): 06/12/2023
*
*--------------------------------------------------------------------------------------------------
* PURPOSE: The macro allows for customization and saving of input files based on select tokens. 
*          Altered files will be saved to custom folder in inputfiles directory 
*          The custom folder will also be referenced as the first location in the infolder 
*          libref assignment, ensuring customized file will be used if one exists 
*
* Program inputs:NA
* Program outputs:
*   - modified version of inputfile saved to custom folder using same filename 
*
*  PARAMETERS: NA
*
*--------------------------------------------------------------------------------------------------
* CONTACT INFO:
*  Sentinel Coordinating Center
*  info@sentinelsystem.org
*
***************************************************************************************************;


%macro customize_input;

     %* Create qcr specific version of lkp_all_flags. No other input files require modification for this workplan type;
     %if %lowcase(&WPType.) = qcr
     %then %do;

        data custom.lkp_all_flags;
            set infolder.lkp_all_flags;
            if     flagid not in ('DEM_2_07_00-0_227' ,'ENC_2_03_00-0_226', 'ENR_2_02_00-0_226','LAB_2_15_00-0_226','LAB_2_16_00-0_226', 'LAB_2_18_00-0_227')
               and not(lowcase(tableid) = 'lab' and checkid in ('230', '231', '232', '234', '235'))
               and not(lowcase(tableid) = 'lab' and CheckID = "222" and lowcase(Variable1) = "result_type" and lowcase(MS_Test_Name) ne "unmapped" );
        run;

     %end;

     %* adjust zip code lookup if parameter specified for length 3 value;
	 %if %sysfunc(exist(infolder.lkp_dem_zip,data)) %then %do;
		%if &ZIP3 = Y %then %do;

			data custom.lkp_dem_zip ( drop = PostalCode 
				rename = ( substrPostalCode = PostalCode ) );
    		length substrPostalCode $ 3;
    		set infolder.lkp_dem_zip;
      		substrPostalCode = cats(substr(PostalCode,1,3));
			proc sort nodupkey; by Postalcode StateCode;
		run;
		%end ;
	%end;

    %* create backwards compatible through SCDM 8.0.0 version of lookups;
    %if &SCDMVer. ne 8.2.0 
    %then %do;

         %* Update lkp_all_l1 to remove tabID/VarID introduced in SCDM to 8.2.0;
         %* Ignore mods to valid values -- checks against earlier SCDM versions will run despite addition of new valid values;
         data custom.lkp_all_l1
              _remove_l1;
 
            set infolder.lkp_all_l1;

            if upcase(tabid) = 'ENR' and upcase(variable) in ("PLANTYPE", "PAYERTYPE") 
                then output _remove_l1;

            else if upcase(tabid) = 'DEM' and upcase(variable) in ("IMPUTEDRACE", "IMPUTEDHISPANIC") 
                then output _remove_l1;

            else output custom.lkp_all_l1;
        run;

        %let remove_tabidList = ;

        %* create parameters for combinations of tabid/variable name to iteratively remove lookups where applicable (flags and all_l2;
        proc sql noprint;
            select   upcase(tabid)
                   , upcase(variable)
            into   :remove_tabidList separated by " "
                 , :remove_varList separated by " "
            from _remove_l1  ;
        quit;

        %if %length(&remove_tabidList.) >0 
        %then %do rn = 1 %to %sysfunc(countw(&remove_tabidList.));

            %let remTabid = %scan(&remove_tabidList., &rn.);              
            %let remVar  = %scan(&remove_varList., &rn.);                

            %* update to remove flags for table and variable match in any position;
            data custom.lkp_all_flags
                 _remove_all_flags&rn.;

                set infolder.lkp_all_flags;

                if index(upcase(TableID), "&remTabid.")
                   and (   upcase(Variable1) = "&remVar."
                        or upcase(Variable2) = "&remVar."
                        or upcase(Variable3) = "&remVar."
                        or upcase(Variable4) = "&remVar.")
                then output _remove_all_flags&rn.;
                else output custom.lkp_all_flags;
            run;

            %* update to remove flags for table and variable match in any position;
            data custom.lkp_all_l2
                 _remove_lkp_all_l2&rn.;

                set infolder.lkp_all_l2;

                if index(upcase(TabID), "&remTabid.")
                   and (   upcase(Variable1) = "&remVar."
                        or upcase(Variable2) = "&remVar.")
                then output _remove_lkp_all_l2&rn.;
                else output custom.lkp_all_l2;
            run;

        %end; %* end rn do loop to modify lookups if SCDM version not 8.2.0;
    %end; %* end SCDM 8.2.0 conditional do statement;                 

%mend customize_input;