if mrx_alt_ded_type_yn = '2' & (deductible < plan_deductible) then do;
  select(tier_level_value);
    when(1) do;
       if substr(mrx_alt_no_ded_tier,2,1) = '1' then deductible_exemption = '1';
    end;
    when(2) do;
       if substr(mrx_alt_no_ded_tier,3,1) = '1' then deductible_exemption = '1';
    end;
    when(3) do;
       if substr(mrx_alt_no_ded_tier,4,1) = '1' then deductible_exemption = '1';
    end;
    when(4) do;
       if substr(mrx_alt_no_ded_tier,5,1) = '1' then deductible_exemption = '1';
    end;
    when(5) do;
       if substr(mrx_alt_no_ded_tier,6,1) = '1' then deductible_exemption = '1';
    end;
    when(6) do;
       if substr(mrx_alt_no_ded_tier,1,1) = '1' then deductible_exemption = '1';
    end;
    otherwise;
  end;
end;

if mrx_first_fill='1' & put(cpdn,$fffmt.)='yes' & rx_script_order=1 then do;
  remaining_avg_cost=0;
end;
else do;
  if (deductible < plan_deductible) and (icl < plan_icl) then do;
    if ((mrx_alt_ded_type_yn = '1') or
       (mrx_alt_ded_type_yn = '2' and deductible_exemption = ' ')) then do;
      remaining_deductible = sum(plan_deductible,-deductible);
      deductible + min(remaining_deductible,avg_price);
      total_covered_oopc + min(remaining_deductible,avg_price);
      remaining_avg_cost = max(0,sum(avg_price,-remaining_deductible));
    end;
    else if (mrx_alt_ded_type_yn = '2' and deductible_exemption = '1') then do;
      remaining_icl = sum(plan_icl,-icl);
      select(mrx_alt_gen_cstshr_struct);
        when('1') do;
          coins_oopc + (avg_price * plan_coins_pre);
          total_covered_oopc + (avg_price * plan_coins_pre);
        end;
        when('2') do;
          copay_oopc + min(avg_price,plan_copay_pre);
          total_covered_oopc + min(avg_price,plan_copay_pre);
        end;
        when('3') do;
          coins_oopc + (avg_price * plan_coins_pre);
          copay_amt = min(avg_price,plan_copay_pre);
          if coins_amt > copay_amt then do;
            coins_oopc + coins_amt;
            total_covered_oopc + coins_amt;
          end;
          else do;
            copay_oopc + copay_amt;
            total_covered_oopc + copay_amt;
          end;
        end;
        when('4') do;
          coins_oopc + (avg_price * plan_coins_pre);
          copay_amt = min(avg_price,plan_copay_pre);
          if coins_amt < copay_amt then do;
            coins_oopc + coins_amt;
            total_covered_oopc + coins_amt;
          end;
          else do;
            copay_oopc + copay_amt;
            total_covered_oopc + copay_amt;
          end;
        end;
        otherwise;
      end;
    end;
  end;
  else remaining_avg_cost = avg_price;
end;

if remaining_avg_cost > 0 and icl < plan_icl then do;
  remaining_icl = sum(plan_icl,-icl);
  select(mrx_alt_pre_icl_cost_share);
    when('1') do;
      remaining_avg_cost = max(0,sum(avg_price,-remaining_icl));
    end;
    when('2') do;
      coins_oopc + min(remaining_avg_cost,avg_price * 0.25);
      total_covered_oopc + min(remaining_avg_cost,avg_price * 0.25);
      remaining_avg_cost = max(0,sum(avg_price,-remaining_icl));
    end;
    when('3') do;
      select(SUBSTR(mrx_tier_cstshr_struct_type,1,1));
        when('1') do;
          coins_oopc + min(remaining_avg_cost,avg_price * plan_coins);
          total_covered_oopc + min(remaining_avg_cost,avg_price * plan_coins);
        end;
        when('2') do;
          copay_oopc + min(remaining_avg_cost,plan_copay);
          total_covered_oopc +  min(remaining_avg_cost,plan_copay);
        end;
        when('3') do;
          coins_amt = min(remaining_avg_cost,avg_price * plan_coins);
          copay_amt = min(remaining_avg_cost,plan_copay);
          if coins_amt > copay_amt then do;
            coins_oopc + coins_amt;
            total_covered_oopc + coins_amt;
          end;
          else do;
            copay_oopc + copay_amt;
            total_covered_oopc + copay_amt;
          end;
        end;
        when('4') do;
          coins_amt = min(remaining_avg_cost,avg_price * plan_coins);
          copay_amt = min(remaining_avg_cost,plan_copay);
          if coins_amt < copay_amt then do;
            coins_oopc + coins_amt;
            total_covered_oopc + coins_amt;
          end;
          else do;
            copay_oopc + copay_amt;
            total_covered_oopc + copay_amt;
          end;
        end;
        otherwise;
      end;
    end;
    otherwise;
  end;
  remaining_avg_cost = 0;
end;

if (remaining_avg_cost > 0) and (threshold < plan_threshold) and
   (mrx_alt_gap_covg_yn = '2' or
     (mrx_alt_gap_covg_yn = '1' and gap_coverage_flag = ' ') or
     (mrx_alt_gap_covg_yn = '1' and gap_coverage_flag = '2' and put(cpdn,$gapf.)='no')
    ) then do;
  if drug_type='G' then discount_avg_price=avg_price*(1-&GENERIC_GAP_DISCOUNT.);
  else if drug_type='B' then discount_avg_price=avg_price*(1-&BRAND_GAP_DISCOUNT.-&BRAND_GAP_LIABDISC.);
  total_covered_oopc + min(discount_avg_price,sum(plan_threshold,-threshold));
  remaining_avg_cost = max(0,sum(discount_avg_price,-sum(plan_threshold,-threshold)));
end;
else if (remaining_avg_cost > 0) and (threshold < plan_threshold) and mrx_alt_gap_covg_yn = '1'
      and gap_coverage_flag in ('1','2') then do;
  if drug_type='G' then discount_avg_price=avg_price;
  else if drug_type='B' then discount_avg_price=avg_price*(1-&BRAND_GAP_DISCOUNT.);
  select(SUBSTR(mrx_tier_cstshr_struct_type,3,1));
    when('1') do;
      coins_oopc + min(remaining_avg_cost,discount_avg_price * plan_coins_gap);
      total_covered_oopc + min(remaining_avg_cost,discount_avg_price * plan_coins_gap);
    end;
    when('2') do;
      copay_oopc + min(remaining_avg_cost,plan_copay_gap*(discount_avg_price/avg_price));
      total_covered_oopc +  min(remaining_avg_cost,plan_copay_gap*(discount_avg_price/avg_price));
    end;
    when('3') do;
      coins_amt = min(remaining_avg_cost,discount_avg_price * plan_coins_gap);
      copay_amt = min(remaining_avg_cost,plan_copay_gap*(discount_avg_price/avg_price));
      if coins_amt > copay_amt then do;
        coins_oopc + coins_amt;
        total_covered_oopc + coins_amt;
      end;
      else do;
        copay_oopc + copay_amt;
        total_covered_oopc + copay_amt;
      end;
    end;
    when('4') do;
      coins_amt = min(remaining_avg_cost,discount_avg_price * plan_coins_gap);
      copay_amt = min(remaining_avg_cost,plan_copay_gap*(discount_avg_price/avg_price));
      if coins_amt < copay_amt then do;
        coins_oopc + coins_amt;
        total_covered_oopc + coins_amt;
      end;
      else do;
        copay_oopc + copay_amt;
        total_covered_oopc + copay_amt;
      end;
    end;
    otherwise;
  end;
end;

if remaining_avg_cost > 0 and threshold >= plan_threshold then do;
  select(mrx_alt_cstshr_post_oopt);
    when('1') ;
    when('2') do;
      if drug_type = 'G' then do;
        if (remaining_avg_cost < plan_copay_cat_gen) and (remaining_avg_cost <
            (avg_price * 0.05)) then do;
          copay_oopc + remaining_avg_cost;
          total_covered_oopc + remaining_avg_cost;
        end;
        else if plan_copay_cat_gen >= (avg_price * 0.05) then do;
          copay_oopc + plan_copay_cat_gen;
          total_covered_oopc + plan_copay_cat_gen;
        end;
        else do;
          coins_oopc + (avg_price * 0.05);
          total_covered_oopc + (avg_price * 0.05);
        end;
      end;
      else if drug_type = 'B' then do;
        if (remaining_avg_cost < plan_copay_cat_brand) and (remaining_avg_cost <
              (avg_price * 0.05)) then do;
          copay_oopc + remaining_avg_cost;
          total_covered_oopc + remaining_avg_cost;
        end;
        else if plan_copay_cat_brand >= (avg_price * 0.05) then do;
          copay_oopc + plan_copay_cat_brand;
          total_covered_oopc + plan_copay_cat_brand;
        end;
        else do;
          coins_oopc + (avg_price * 0.05);
          total_covered_oopc + (avg_price * 0.05);
        end;
      end;
    end;
    when('3') do;
      select(SUBSTR(mrx_tier_cstshr_struct_type,2,1));

        when('1') do;
          coins_oopc + min(remaining_avg_cost,avg_price * plan_coins_cat);
          total_covered_oopc + min(remaining_avg_cost,avg_price * plan_coins_cat);
        end;
        when('2') do;
          copay_oopc + min(remaining_avg_cost,plan_copay_cat);
          total_covered_oopc +  min(remaining_avg_cost,plan_copay_cat);
        end;
        when('3') do;
          coins_amt = min(remaining_avg_cost,avg_price * plan_coins_cat);
          copay_amt = min(remaining_avg_cost,plan_copay_cat);
          if coins_amt > copay_amt then do;
            coins_oopc + coins_amt;
            total_covered_oopc + coins_amt;
          end;
          else do;
            copay_oopc + copay_amt;
            total_covered_oopc + copay_amt;
          end;
        end;
        when('4') do;
          coins_amt = min(remaining_avg_cost,avg_price * plan_coins_cat);
          copay_amt = min(remaining_avg_cost,plan_copay_cat);
          if coins_amt < copay_amt then do;
            coins_oopc + coins_amt;
            total_covered_oopc + coins_amt;
          end;
          else do;
            copay_oopc + copay_amt;
            total_covered_oopc + copay_amt;
          end;
        end;
        otherwise;
      end;
    end;
    otherwise;
  end;
end;

if icl < plan_icl then icl + avg_price;
threshold + avg_price;
