
* PROGRAM: COST_SHARING_ORTHOTICS.SAS;
* DESCRIPTION: PERFORM COST SHARING ALGORITHM FOR ORTHOTICS CLAIMS;

%MACRO ORTHOTICS_COST_SHARE(SERV_CAT);

  PROC SUMMARY DATA=&INC NWAY MISSING;
    WHERE CATEGORY IN('11b');
    CLASS BASEID YEAR;
    VAR AMTTOT;
    OUTPUT OUT=CAT_SUMMARY (DROP=_TYPE_ _FREQ_) SUM=;
  RUN;
  
  %obsnvars(CAT_SUMMARY);
  %LET CATOBS=&NOBS.;
  
  DATA FMTDATA;
    SET CAT_SUMMARY END=LAST;
    LENGTH HLO $1 START $12;
    FMTNAME='$CATFMT';
    START=BASEID || YEAR;
    LABEL=AMTTOT;
    HLO=' ';
    OUTPUT;
    IF LAST THEN DO;
      START='OTHER';
      LABEL=0;
      HLO='O';
      OUTPUT;
    END;
    KEEP FMTNAME START LABEL HLO;
  RUN;
  
  PROC FORMAT CNTLIN=FMTDATA;
  RUN;

  PROC SUMMARY DATA=&INC NWAY MISSING;
    WHERE CATEGORY IN('11a');
    CLASS BASEID YEAR;
    VAR AMTTOT;
    OUTPUT OUT=CAT_SUMMARY_DME (DROP=_TYPE_ _FREQ_) SUM=;
  RUN;

  %obsnvars(CAT_SUMMARY_DME);
  %LET DMEOBS=&NOBS.;
    
  DATA FMTDATA_DME;
    SET CAT_SUMMARY_DME END=LAST;
    LENGTH HLO $1 START $12;
    FMTNAME='$DMEFMT';
    START=BASEID || YEAR;
    LABEL=AMTTOT;
    HLO=' ';
    OUTPUT;
    IF LAST THEN DO;
      START='OTHER';
      LABEL=0;
      HLO='O';
      OUTPUT;
    END;
    KEEP FMTNAME START LABEL HLO;
  RUN;
    
  PROC FORMAT CNTLIN=FMTDATA_DME;
  RUN;

  DATA ORTHOTICS_BASEID;
    RETAIN JOIN_ID '1';
    SET &INC(WHERE=(SOURCE='orthotics'));
  RUN;

  DATA ORTHOTICS_FILE;
    SET PBP_FILE (KEEP = CONTRACT_ID PLAN_ID SEGMENT_ID HPLAN
                  PBP_B11B_DED_AMT PBP_B11B_MAXENR_AMT
                  PBP_B11B_MAXENR_PER PBP_B11B_COINS_PCT_MC PBP_B11B_COPAY_MCMIN_AMT
                  PBP_B11B_MAXENR_COST_TYPE PBP_B11A_MAXENR_AMT PBP_B11A_MAXENR_PER
                  PBP_B11B_COPAY_EHC PBP_B11B_COINS_EHC
                  PBP_A_PLAN_TYPE
                  PBP_D_DIFF_DEDUCT_11B_AMT);
    JOIN_ID = '1'; 
    %ANNUALIZATION(PBP_B11B_MAXENR_PER,PBP_B11B_MAXENR_AMT);
    %ANNUALIZATION(PBP_B11A_MAXENR_PER,PBP_B11A_MAXENR_AMT);
  RUN;

  PROC SQL;
    CREATE TABLE CAT_JOIN AS
      (SELECT B.CATEGORY, B.BASEID, B.VISITS, B.AMTTOT, B.YEAR, P.*
      FROM ORTHOTICS_BASEID B, ORTHOTICS_FILE P
      WHERE B.JOIN_ID = P.JOIN_ID)
      ORDER BY HPLAN, BASEID, YEAR, CATEGORY;
    QUIT;
  RUN;
      
  DATA CAT_JOIN;
    MERGE CAT_JOIN (IN = A) 
          PBPCATS_MCCOST_DED (IN = B WHERE=(CATEGORY='11b'));
    BY HPLAN BASEID YEAR CATEGORY;
    IF A;
    LENGTH PLAN_DED_CAT $1;
    IF B THEN PLAN_DED_CAT='1';
    ELSE PLAN_DED_CAT='0';
  RUN;
  
  DATA &SERV_CAT;
    SET CAT_JOIN (DROP = JOIN_ID);
    COST_SHARE_COST = 0;
    OTHER_COST = 0;
    COST = 0;
    CAT_DEDUCTIBLE=0;

    BASEIDYR=BASEID || YEAR;       
    %IF "&CATOBS." > "0" %THEN %DO;
      PBPCAT_AMTTOT=INPUT(PUT(BASEIDYR,$CATFMT.),8.);
    %END;
    %ELSE %DO;
      PBPCAT_AMTTOT=0;
    %END;

    IF PLAN_DED_CAT='1' THEN DO;
      IF PLAN_DED_AMTTOT_MC_PTB>0 THEN CAT_DEDUCTIBLE=PLAN_DED_AMTTOT_MC_PTB;
      ELSE CAT_DEDUCTIBLE=PLAN_DED_AMTTOT_MC;
    END;
    ELSE IF PBP_A_PLAN_TYPE IN('04','31','44','47') THEN 
      PBP_CAT_DEDUCTIBLE=PBP_D_DIFF_DEDUCT_11B_AMT;
    ELSE PBP_CAT_DEDUCTIBLE=PBP_B11B_DED_AMT;
                    
    IF PLAN_DED_CAT^='1' & PBPCAT_AMTTOT>0 THEN  
      CAT_DEDUCTIBLE = PBP_CAT_DEDUCTIBLE*(AMTTOT/PBPCAT_AMTTOT);
    
    IF PBP_B11B_MAXENR_AMT = . &
      PBP_B11B_MAXENR_COST_TYPE = '1' THEN DO;
      %IF "&DMEOBS." > "0" %THEN %DO;
        AMTTOTE=INPUT(PUT(BASEID,$DMEFMT.),8.);
      %END;
      %ELSE %DO;
        AMTTOTE=0;
      %END;
      PBPCAT_TOT=SUM(AMTTOTE,AMTTOT);
      IF PBPCAT_TOT>0 THEN 
        MAXENR_AMT = PBP_B11A_MAXENR_AMT*(AMTTOT/PBPCAT_TOT);
    END;
    ELSE DO;
      MAXENR_AMT = PBP_B11B_MAXENR_AMT;
    END;
    
    IF PBPCAT_AMTTOT>0 THEN
      BENE_MAXIMUM=MAXENR_AMT*(AMTTOT/PBPCAT_AMTTOT);

    IF CAT_DEDUCTIBLE = . THEN DO;
      IF SUBSTR(PBP_B11B_COPAY_EHC,2,1)='1' THEN 
        COST = SUM(COST,(VISITS*PBP_B11B_COPAY_MCMIN_AMT));
      IF SUBSTR(PBP_B11B_COINS_EHC,2,1)='1' THEN 
        COST = SUM(COST,AMTTOT*PBP_B11B_COINS_PCT_MC);
    END;
    ELSE DO;
      IF AMTTOT <= CAT_DEDUCTIBLE THEN DO;
        COST = AMTTOT;
      END;
      ELSE DO;
        COST = CAT_DEDUCTIBLE;
        DEDUCT_SHARE = CAT_DEDUCTIBLE / AMTTOT;
        AMTTOT = SUM(AMTTOT,-CAT_DEDUCTIBLE);
        VISITS = VISITS * (1 - DEDUCT_SHARE);
        IF SUBSTR(PBP_B11B_COPAY_EHC,2,1)='1' THEN 
          COST = SUM(COST,(VISITS*PBP_B11B_COPAY_MCMIN_AMT));
        IF SUBSTR(PBP_B11B_COINS_EHC,2,1)='1' THEN 
          COST = SUM(COST,AMTTOT*PBP_B11B_COINS_PCT_MC);
      END;
    END;

    IF BENE_MAXIMUM >= 0 AND COST >= 0 THEN 
      COST = MIN(COST,BENE_MAXIMUM);

    COST_SHARE_COST = COST;

    COST_SHARE_COST_MC  = COST;
    COST_SHARE_COST_AMO = 0;

    CATEGORY = "11b";
  RUN;

  PROC DATASETS LIBRARY=WORK NODETAILS NOLIST;
    DELETE ORTHOTICS_BASEID ORTHOTICS_FILE CAT_JOIN;
    QUIT;
  RUN;

%MEND;

