/*-------------------------------------------------------------------------------------*\
|  PROGRAM NAME:                                                                        |
|     scdm-data-qa-review-dispensing.sas                                                |
|                                                                                       |
|---------------------------------------------------------------------------------------|
|  PURPOSE:                                                                             |
|     The purpose of this program is to perform data quality checks on the Dispensing   |
|     table.                                                                            |
|---------------------------------------------------------------------------------------|
|  PROGRAM INPUT:                                                                       |
|     see 00.0_scdm_data_qa_review_master_file.sas                                      |
|                                                                                       |
|  PROGRAM OUTPUT:                                                                      |
|     see Workplan PDF                                                                  |
|---------------------------------------------------------------------------------------|
|  CONTACT:                                                                             |
|     Sentinel Coordinating Center                                                      |
|     info@sentinelsystem.org                                                           |
\*-------------------------------------------------------------------------------------*/

*-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-;
*  PLEASE DO NOT EDIT BELOW WITHOUT CONTACTING THE SENTINEL OPERATIONS CENTER           ;
*-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-;

/*-------------------------------------------------------------------------------------*/
/*  START Level 2                                                                      */
/*-------------------------------------------------------------------------------------*/
%let qa_table= ;
%macro get_qa_table ;
  %if &do_partitions and not %eval(&module in &tabid_exc) %then %do ;
    data t_view /view=t_view ;
      set qadata.&&&module.table.: (keep=rxdate rxsup rxamt) ;
    run;
    %let qa_table = t_view ;
  %end ;
  %else %do;
    %let qa_table = qadata.&&&module.table (keep=rxdate rxsup rxamt) ;
  %end;
%mend;
%get_qa_table ;
proc sql noprint;
  create table dplocal.&module._temp as
  select rxdate 
       , rxsup
       , rxamt
       , count(*) as n
  from &qa_table
  group by 1,2,3
  ;
quit;
%remove_labels(dplocal, &module._temp);

proc sql noprint;
  create table msoc.&module._l2_rxamt_rxsup as 
  select rxsup 
       , rxamt label=' '
       , sum(n) as count format=comma15.
  from dplocal.&module._temp (keep=n rxsup rxamt)
  group by 1,2
  ;
quit;

* list of rx by rx_codetype and provider specialty (exclude record count);
%macro get_qa_table ;
  %if &do_partitions and not %eval(&module in &tabid_exc) %then %do ;
    data t_view /view=t_view ;
      set qadata.&&&module.table.: (keep= rx_codetype rx providerid) ;
    run;
    %let qa_table = t_view ;
  %end ;
  %else %do;
    %let qa_table = qadata.&&&module.table (keep= rx_codetype rx providerid) ;
  %end;
%mend;
%get_qa_table ;
proc sql noprint;
  create table dplocal.&module._l2_rx_rxtype_spec (drop= n) as
  select distinct a.rx_codetype label=' '
                , a.rx label=' '
                , b.specialty
                , count(*) as n
  from &qa_table a
  left join qadata.&PVDTABLE.(keep = providerid specialty) as b
  on a.providerid = b.providerid
  group by 1,2,3
  order by n desc 
  ;
quit;

proc sql noprint;
  create table msoc.&module._l2_rx_rxtype_spec as
    select monotonic() as row
           ,*
  from dplocal.&module._l2_rx_rxtype_spec
  ;
quit;
/*-------------------------------------------------------------------------------------*/
/*  START Level 2 Flags                                                                */
/*-------------------------------------------------------------------------------------*/;
%level2;

/*-------------------------------------------------------------------------------------*/
/*  START Level 3                                                                      */
/*-------------------------------------------------------------------------------------*/
%let level=3;
proc sql noprint;
  create table dplocal.&module._temp_dates as
  select rxdate
       , sum(n) as n
  from dplocal.&module._temp (keep=rxdate n)
  group by 1
  ;
  drop table dplocal.&module._temp
  ;
  drop table dplocal.&module._l2_rx_rxtype_spec
  ; 
quit;

%date_percentiles (libin=dplocal, dsin=&module._temp_dates, libout=dplocal, dsout=date_dist_&module., vars=rxdate);

%macro get_qa_table ;
  %if &do_partitions and not %eval(&module in &tabid_exc) %then %do ;
    data t_view /view=t_view ;
      set qadata.&&&module.table.: (keep=patid rxdate) ;
    run;
    %let qa_table = t_view ;
  %end ;
  %else %do;
    %let qa_table = qadata.&&&module.table (keep=patid rxdate) ;
  %end;
%mend;
%get_qa_table ;
proc sql noprint;
  drop table dplocal.&module._temp_dates
  ;
* list of patids-rxdate with # of records;
  create table dplocal.&module._temp as
  select patid
       , put(rxdate,yymmd.) as YearMonth
       , count(*) as n
  from &qa_table
  group by 1,2
  ;
quit;
%remove_labels(dplocal, &module._temp);

proc sql noprint;
  /* < distribution of dispensings per year-month > */
  create table msoc.&module._l3_rxdate_ym as
  select yearmonth
       , sum(n) as count format=comma15.
  from dplocal.&module._temp (keep=yearmonth n)
  group by 1
  ;
/* < number of dispensings per patid per year > */
  create table dplocal.&module._temp1 as
  select patid
       , substr(Yearmonth,1,4) as Year
       , sum(n) as n
  from dplocal.&module._temp
  group by 1,2
  ;
  drop table dplocal.&module._temp
  ;
quit;

/*< Distribution of number of dispensings per patid per year > */
proc means nolabels nonobs data=dplocal.&module._temp1 missing StackODSOutput n sum mean std min p1 P5 P25 median P75 P95 p99 max; 
  var n;
  class year;
  ods output summary=msoc.&module._l3_rx_pt_y_stats (drop=_: variable rename=(n=n_ptyr sum=n_rx stddev=Std));
run;

proc datasets library=msoc nolist nodetails nowarn;
  modify &module._l3_rx_pt_y_stats;
  format n_: comma15. mean std 10.2 p: min max median 10.1;
  informat mean std 10.2 p: min max median 10.1;
quit;

proc datasets lib=dplocal memtype=data nolist nodetails nowarn;
  delete &module._temp1;
quit;

*-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-;
*  END scdm-data-qa-review-dispensing.sas                                               ; 
*-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-;