* PROGRAM: BUILD_LOOKUP.SAS;

LIBNAME FORM    "&FORMULARY";
LIBNAME INPUT   "&INPUTDIR";
LIBNAME PBP     "&PBPDIR";

options msglevel=i;

* first subset the formulary file for the formularies for the planfile.txt;

* fix type of rxcui variable to numeric 8 for druglist_rxcui;
data druglist_rxcui;
*length rxcui 8;
set INPUT.druglist_rxcui/*(rename=rxcui=old_rxcui)*/;
*rxcui = input(old_rxcui,8.);
run;

DATA PLAN_FORM;
LENGTH CONTPLN $8;
SET FORM.PLAN_FORMULARY;
CONTPLN = CONTRACT_ID||PLAN_ID;
IF PUT(CONTPLN,$CONTPLNF.) NE 'NO';
RUN;

proc sort data=plan_form out=pforms(keep=formulary_id) nodupkey;
by formulary_id;
run;

proc sort data=form.formulary out=formulary;
by formulary_id;
run;

data formulary_subset;
merge pforms(in=inplan) formulary(in=inform);
by formulary_id;
if inplan and inform;
run;

* merge formulary data with druglist by rxcui, select minimum tier level value;

proc sql;
  create table form_rxcui as
   (select f.formulary_id, f.tier_level_value, d.drugname, d.drug_type, d.avg_price
   from formulary_subset f, druglist_rxcui d
   where f.rxcui = d.rxcui
   )
  ;
;

proc sort data=form_rxcui;
by formulary_id drugname tier_level_value drug_type;
run;

data form_rxcui_sub;
set form_rxcui;
by formulary_id drugname tier_level_value;
retain tlvtemp;
if first.drugname then do;
  tlvtemp=tier_level_value;
end;
if tier_level_value=tlvtemp then output;
run;

data low_tiers_drugname (keep=formulary_id drugname tier_level_value brand_flag generic_flag
  avg_price);
  set form_rxcui_sub;
  by formulary_id drugname tier_level_value drug_type;
  brand_flag='0';
  generic_flag='0';
  select(drug_type);
    when ('B')  do;
      brand_flag='1';
    end;
    when ('G') do;
      generic_flag='1';
    end;
    otherwise;
  end;
run;

proc summary data=low_tiers_drugname nway missing;
  class formulary_id drugname tier_level_value generic_flag brand_flag;
  var avg_price;
  output out=low_tiers_median(drop=_type_ _freq_) median=;
run;

data low_tiers (drop=gf bf avg_price);
  set low_tiers_median (rename=(generic_flag=gf brand_flag=bf));
  by formulary_id drugname tier_level_value;
  retain generic_flag brand_flag;
  if first.tier_level_value then do;
    brand_flag='0';
    generic_flag='0';
    brand_price=.;
    generic_price=.;
  end;
  if bf='1' then do;
    brand_flag='1';
    brand_price=avg_price;
  end;
  if gf='1' then do;
    generic_flag='1';
    generic_price=avg_price;
  end;
  if last.tier_level_value;
run;

proc summary data=druglist_rxcui nway missing;
class drugname drug_type;
var avg_price;
output out=median_avg_drugs (drop=_type_ _freq_) median=;
run;

data druglist_rollup (keep=drugname avg_brand avg_generic avg_unknown);
set median_avg_drugs;
by drugname;
array price(3)  avg_brand avg_generic avg_unknown;
retain avg_brand avg_generic avg_unknown;
if first.drugname then do i=1 to 3;
  price(i)=.;
end;
select(drug_type);
  when ('B')  price(1)=avg_price;
  when ('G')  price(2)=avg_price;
  when (' ')  price(3)=avg_price;
  otherwise;
end;
if last.drugname;
run;

* create shell of all formulary id/drugname combinations;

data _null_;
  set druglist_rollup end = last;
  if last then call symput('drugcnt',left(_n_));
run;

data formlist;
  set formulary_subset;
  by formulary_id;
  if first.formulary_id;
  keep formulary_id;
run;

data shell;
  set formlist end=last;
  do i=1 to &drugcnt.;
    set druglist_rollup (keep=drugname) point=i;
    output;
  end;
  if last then stop;
  keep formulary_id drugname;
run;

* merge tier data onto shell;

data shell_low;
  merge shell (in=a) low_tiers (in=b);
  by formulary_id drugname;
  if a;
  low_tier_flag=b;
run;

proc sort data=shell_low;
  by drugname;
run;

data shell_all;
  merge shell_low (in=a) druglist_rollup (in=b);
  by drugname;
  if a;
  rollup_flag=b;
  if drug_type=' ' then do;
    if avg_generic^=. then drug_type='G';
    else if avg_brand^=. then drug_type='B';
    else if avg_unknown^=. then drug_type='U';
  end;
run;

data drugname_formulary_tiers;
  set shell_all;
  select(tier_level_value);
    when (.)    do;
                  if drug_type='B' then avg_price=avg_brand;
                  else if drug_type='G' then avg_price=avg_generic;
                  else if drug_type='U' then avg_price=avg_unknown;
                end;
    otherwise   do;
                  if drug_type='B' then avg_price=brand_price;
                  else if drug_type='G' then avg_price=generic_price;
                  else if drug_type='U' then avg_price=avg_unknown;
                end;
  end;
  if drug_type='U' then drug_type='B';
  keep formulary_id drugname avg_price tier_level_value drug_type;
run;

proc sql;
  create table ricpme_sample_cohort as
    (select f.formulary_id, f.tier_level_value, f.avg_price, f.drug_type,
            b.baseid, b.rx_script_order, b.drugname, b.drugname_code, b.year
     from IN1.bene_script b, drugname_formulary_tiers f
     where b.drugname = f.drugname
    );

data ricpme_sample_cohort_comp_&year1 ricpme_sample_cohort_comp_&year2;
  set ricpme_sample_cohort;
  length avg_price 6 rx_script_order tier_level_value 3;
  drop drugname;
  if year = &year1 then output ricpme_sample_cohort_comp_&year1;
  else if year = &year2 then output ricpme_sample_cohort_comp_&year2;
run;

proc datasets memtype=data lib=work nolist;
delete ricpme_sample_cohort;
run;
