 * ------------------------------------------------------------- ;
 *  TITLE: PSI MODULE DIAGNOSIS AND PROCEDURE MACROS         --- ;
 *  DESCRIPTION: Assigns diagnosis and procedure codes       --- ;
 *               using macros called by other SAS programs.  --- ;
 *               The user does not need to open or modify.   --- ;
 *  VERSION: SAS QI v2020                                    --- ;
 *  RELEASE DATE: JULY 2020                                  --- ;
 * ------------------------------------------------------------- ;

/*Macro to run Format, Measure and observed programs from within Control program.*/
Filename PROGRMS "&PATHNAME.\Programs";
%MACRO PROG_EXE(PATHNAME,EXE_FMT,EXE_MSR,EXE_HOBS,EXE_HRSK,EXE_HCMP);
    %if %sysfunc(CEXIST(LIBRARY.FORMATS.VENACIP.FORMATC)) = 0 or &EXE_FMT. = 1 %then %do;
        %include PROGRMS(PSI_HOSP_FORMATS.SAS);
    %end;
  
   %if %sysfunc(CEXIST(LIBRARY.FORMATS.VENACIP.FORMATC)) = 1 and &EXE_MSR. = 1  %then %do;
    	%include PROGRMS(PSI_HOSP_MEASURES.sas) /source2;
   %end;

   %if %sysfunc(CEXIST(LIBRARY.FORMATS.VENACIP.FORMATC)) = 0 and &EXE_MSR. = 1  %then %do;
    	 %PUT PSI Format library not found. Verify Format Library created prior to running Measures.;
		  %goto exit;
   %end;
   
   %if %sysfunc(exist(OUTMSR.&OUTFILE_MEAS.)) and &EXE_HOBS. = 1 %then %do;
    	%include PROGRMS(PSI_HOSP_OBSERVED.sas) /source2;
   %end;
   
   %if %sysfunc(exist(OUTMSR.&OUTFILE_MEAS.)) = 0 and &EXE_HOBS. = 1 %then %do;
        %PUT PSI Measure output not found. Verify OUTMSR Library or rerun Control Program with EXE_MSR = 1;
		 %goto exit;
    %end;
	
   %if %sysfunc(exist(OUTMSR.&OUTFILE_MEAS.)) and &EXE_HRSK. = 1 %then %do;
    	%include PROGRMS(PSI_HOSP_RISKADJ.sas) /source2;
   %end;
   
   %if (%sysfunc(exist(OUTMSR.&OUTFILE_MEAS.)) = 0 OR %sysfunc(exist(OUTHOBS.&OUTFILE_HOSPOBS.)) = 0) and &EXE_HRSK. = 1 %then %do;
        %PUT PSI output not found. Verify OUTMSR and OUTOBS Library or rerun Control Program with EXE_MSR = 1 and EXE_OBS=1;
		 %goto exit;
    %end;
	
   %if %sysfunc(exist(OUTMSR.&OUTFILE_MEAS.)) and &EXE_HCMP. = 1 %then %do;
    	%include PROGRMS(PSI_HOSP_COMPOSITE.sas) /source2;
   %end;
   
   %if %sysfunc(exist(OUTMSR.&OUTFILE_MEAS.)) = 0 and &EXE_HCMP. = 1 %then %do;
        %PUT PSI Measure output not found. Verify OUTMSR Library or rerun Control Program with EXE_MSR = 1;
		 %goto exit;
    %end;
  %exit:
%MEND;

 /*Macro to compare all discharge diagnosis codes against format.*/
 %MACRO MDX(FMT);

 (%DO I = 1 %TO &NDX.-1;
  (put(DX&I.,&FMT.) = '1') or
  %END;
  (put(DX&NDX.,&FMT.) = '1'))

 %MEND;

 /*Macro to compare discharge primary diagnosis code against format.*/
 %MACRO MDX1(FMT);

 (put(DX1,&FMT.) = '1')

 %MEND;
 
 /*Macro to compare discharge secondary diagnosis codes against format.*/
 %MACRO MDX2(FMT);

 (%DO I = 2 %TO &NDX.-1;
  (put(DX&I.,&FMT.) = '1') or
  %END;
  (put(DX&NDX.,&FMT.) = '1'))
 %MEND;

/*Macro to determine if measure format diagnosis is included in any secondary discharge diagnosis code as not present on admission.*/
 %MACRO MDX2Q1(FMT);

1 = 1 then do;
  result = 0;
  do i = 2 to &NDX.;
    if put(DX{i},&FMT.) = '1' then
      if DXPOA{i} IN ('N','U',' ','E','1','X') then
        if (ICDVER = 37 AND put(DX{i},$poaxmpt_v37fmt.)='0') OR
           (ICDVER = 36 AND put(DX{i},$poaxmpt_v36fmt.)='0') OR
           (ICDVER = 35 AND put(DX{i},$poaxmpt_v35fmt.)='0') OR
           (ICDVER = 34 AND put(DX{i},$poaxmpt_v34fmt.)='0') OR
           (ICDVER = 33 AND put(DX{i},$poaxmpt_v33fmt.)='0')
        then result = 1;
    if result = 1 then leave;
  end;
end;
if result = 1

 %MEND;

/*Macro to determine if measure format diagnosis is included in any secondary discharge diagnosis code as present on admission.*/
 %MACRO MDX2Q2(FMT);

1 = 1 then do;
  result = 0;
  do i = 2 to &NDX.;
    if put(DX{i},&FMT.) = '1' then do;
      if DXPOA{i} IN ('Y','W') then
        if (ICDVER = 37 AND put(DX{i},$poaxmpt_v37fmt.)='0') OR
           (ICDVER = 36 AND put(DX{i},$poaxmpt_v36fmt.)='0') OR
           (ICDVER = 35 AND put(DX{i},$poaxmpt_v35fmt.)='0') OR
           (ICDVER = 34 AND put(DX{i},$poaxmpt_v34fmt.)='0') OR
           (ICDVER = 33 AND put(DX{i},$poaxmpt_v33fmt.)='0')
        then result = 1;
      if (ICDVER = 37 AND put(DX{i},$poaxmpt_v37fmt.)='1') OR
         (ICDVER = 36 AND put(DX{i},$poaxmpt_v36fmt.)='1') OR
         (ICDVER = 35 AND put(DX{i},$poaxmpt_v35fmt.)='1') OR
         (ICDVER = 34 AND put(DX{i},$poaxmpt_v34fmt.)='1') OR
         (ICDVER = 33 AND put(DX{i},$poaxmpt_v33fmt.)='1')
      then result = 1;
    end;
  if result = 1 then leave;
  end;
end;
if result = 1

 %MEND;

/*Macro to determine if measure format diagnosis is included in any discharge diagnosis code as present on admission.*/
 %MACRO MDXAQ2(FMT);

1 = 1 then do;
  result = 0;
  do i = 1 to &NDX.;
    if put(DX{i},&FMT.) = '1' then do;
      if DXPOA{i} IN ('Y','W') then
        if (ICDVER = 37 AND put(DX{i},$poaxmpt_v37fmt.)='0') OR
           (ICDVER = 36 AND put(DX{i},$poaxmpt_v36fmt.)='0') OR
           (ICDVER = 35 AND put(DX{i},$poaxmpt_v35fmt.)='0') OR
           (ICDVER = 34 AND put(DX{i},$poaxmpt_v34fmt.)='0') OR
           (ICDVER = 33 AND put(DX{i},$poaxmpt_v33fmt.)='0')
        then result = 1;
      if (ICDVER = 37 AND put(DX{i},$poaxmpt_v37fmt.)='1') OR
         (ICDVER = 36 AND put(DX{i},$poaxmpt_v36fmt.)='1') OR
         (ICDVER = 35 AND put(DX{i},$poaxmpt_v35fmt.)='1') OR
         (ICDVER = 34 AND put(DX{i},$poaxmpt_v34fmt.)='1') OR
         (ICDVER = 33 AND put(DX{i},$poaxmpt_v33fmt.)='1')
      then result = 1;
    end;
  if result = 1 then leave;
  end;
end;
if result = 1

 %MEND;

 /*Macro to compare discharge DRG code against format.*/
 %MACRO MDR(FMT);

 (put(put(DRG,Z3.),&FMT.) = '1')

 %MEND;
 
  /*Macro to compare all discharge procedure codes against format.*/
 %MACRO MPR(FMT);

 (%DO I = 1 %TO &NPR.-1;
  (put(PR&I.,&FMT.) = '1') or
  %END;
  (put(PR&NPR.,&FMT.) = '1'))

 %MEND;
 
/*Macro to compare first discharge procedure code against format.*/
 %MACRO MPR1(FMT);

 (put(PR1,&FMT.) = '1')

 %MEND;
 
/*Macro to compare secondary discharge procedure codes against format.*/
 %MACRO MPR2(FMT);

 (%DO I = 2 %TO &NPR.-1;
  (put(PR&I.,&FMT.) = '1') or
  %END;
  (put(PR&NPR.,&FMT.) = '1'))

 %MEND;
 
/*Macro to compare discharge procedure codes against measure format and operating room format and count total.*/

 %MACRO MPRCNT(FMT);

    MPRCNT = 0;
    %DO I = 1 %TO &NPR.;
       if put(PR&I.,&FMT.) = '1'    AND
          put(PR&I.,$ORPROC.) = '1' then MPRCNT + 1;
    %END;

 %MEND;
 
/*Macro to return the first day a measure format procedure was conducted based on PRDAY.*/
 %MACRO MPRDAY(FMT);

    MPRDAY = .;
    %DO I = 1 %TO &NPR.;
       if put(PR&I.,&FMT.) = '1' and PRDAY&I. GT .Z then do;
          if MPRDAY LE .Z then MPRDAY = PRDAY&I;
          else if MPRDAY > PRDAY&I. then MPRDAY = PRDAY&I;
       end;
    %END;

 %MEND;
 
/*Macro to count the number of procedures on the discharge that are operating room procedures.*/

 %MACRO ORCNT;

    ORCNT = 0;
    %DO I = 1 %TO &NPR.;
       if put(PR&I.,$ORPROC.) = '1' then ORCNT + 1;
    %END;

 %MEND;

/*Macro to return first day an operating room procedure was conducted that was not the measure format based on PRDAY.*/
 %MACRO ORDAY(FMT);

    ORDAY = .;
    %DO I = 1 %TO &NPR.;
       if put(PR&I.,$ORPROC.) = '1' and
          put(PR&I.,&FMT.)    = '0' then do;
          if PRDAY&I. GT .Z then do;
             if ORDAY = . then ORDAY = PRDAY&I;
             else if ORDAY > PRDAY&I. then ORDAY = PRDAY&I;
          end;
       end;
    %END;

 %MEND;

 /*Macro to add PRDAY fields onto input statement based on Control file setting.*/
 %MACRO ADDPRDAY;
    %IF &PRDAY EQ 1 %THEN PRDAY1-PRDAY&NPR. ;
 %MEND;
