/*-------------------------------------------------------------------------------------*\
|  PROGRAM NAME:                                                                        |
|     scdm_data_qa_review-prescribing.sas                                               |
|                                                                                       |
|---------------------------------------------------------------------------------------|
|  PURPOSE:                                                                             |
|     The purpose of this program is to perform data quality checks on the              |
|     Prescribing 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 level=2;

%macro l2_tables (varlist=);
  %local l selectlist var ct;
  %let selectlist=%sysfunc(tranwrd(&varlist,%str( ),%str(,) ));
  %let ct=%sysfunc(countw(&varlist));  

  %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 = &varlist.) ;
      run;
      %let qa_table = t_view ;
    %end ;
    %else %do;
      %let qa_table = qadata.&&&module.table (keep = &varlist.) ;
    %end;
  %mend;
  %get_qa_table ;

  proc sql noprint;
    create table dplocal.&module._temp as
    select &selectlist.
         , count(*) as n
    from &qa_table
    group by &selectlist.
    ;
  quit;
  %remove_labels(dplocal, &module._temp);

  %do l=1 %to &ct.;
    %let var=%scan(&varlist,&l);

    proc sql noprint;
      create table msoc.&module._l2_&var. as
      select &var.
           , sum(n) as count format=comma15.
      from dplocal.&module._temp (keep= n &var.)
      group by 1
      ;
    quit;
  %end;

  proc sql noprint;
    drop table dplocal.&module._temp;
  quit;

%mend l2_tables;

%l2_tables (varlist=RxSource RxRoute RxDoseQuantity RxDoseUnit RxDoseForm);
%l2_tables (varlist=RxFreqQuantity RxFreqUnit RxRefills RxPrnFlag RxDAW);

%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= orderdate rx_codetype rx rxsup rxamt providerid) ;
    run;
    %let qa_table = t_view ;
  %end ;
  %else %do;
    %let qa_table = qadata.&&&module.table (keep= orderdate rx_codetype rx rxsup rxamt providerid) ;
  %end;
%mend;
%get_qa_table ;
/* create temp table, used to source creation of remaining level 2 prescribing tables */
proc sql noprint;
    create table dplocal.&module._temp as
        select   put(a.orderdate,yymmd.) as YearMonth
               , a.rx_codetype
               , a.rx
               , a.rxsup
               , a.rxamt
	           , b.specialty
               , count(*) as n 
        from &qa_table as a
             left join 
             qadata.&PVDTABLE. (keep = providerid specialty ) as b
	    on a.providerid = b.providerid
        group by 1,2,3,4,5,6;

quit; 
%remove_labels(dplocal, &module._temp);


/* frequency of Rx by Rx_CodeType */
proc sql noprint;
    create table msoc.&module._l2_rx_rxtype_spec as
        select   rx_codetype
               , rx
               , specialty
               , sum(n) as count format=comma15. 
        from dplocal.&module._temp (keep= n rx rx_codetype specialty)
        group by 1,2,3 ;
quit;

/* frequency of OrderDate */
proc sql noprint;
    create table msoc.&module._l2_orderdate_ym as
        select   yearmonth
               , sum(n) as count format=comma15. 
        from dplocal.&module._temp (keep= n yearmonth)
        group by 1;
quit;


/*-------------------------------------------------------------------------------------*/
/*  START Level 2 Flags                                                                */
/*-------------------------------------------------------------------------------------*/
%level2;

/*-------------------------------------------------------------------------------------*/
/*  START Level 3                                                                      */
/*-------------------------------------------------------------------------------------*/
%let level=3;

/* frequency of RxAmt by RxSup */
proc sql noprint;
  create table msoc.&module._l3_rxamt_rxsup as
  select rxamt
       , rxsup
       , sum(n) as count format=comma15. 
  from dplocal.&module._temp (keep= n rxamt rxsup)
  group by 1,2
  ;
  drop table dplocal.&module._temp
  ;
quit;

%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=orderdate) ;
    run;
    %let qa_table = t_view ;
  %end ;
  %else %do;
    %let qa_table = qadata.&&&module.table (keep=orderdate) ;
  %end;
%mend;
%get_qa_table ;

proc sql noprint;
  create table dplocal.&module._temp_dates as
  select orderdate 
       , count(*) as n
  from &qa_table
  group by 1
  ;
quit;
%remove_labels(dplocal, &module._temp_dates);
%date_percentiles (libin=dplocal, dsin=&module._temp_dates
                , libout=dplocal, dsout=date_dist_&module., vars=orderdate );

proc sql noprint;
  drop table dplocal.&module._temp_dates
  ;
quit;

/* Temporary PatID and OrderDate for L3 Stats tables  */
/* Note: by patid by orderdate, not using EncounterID, as it is allowed to be missing */
%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 orderdate) ;
    run;
    %let qa_table = t_view ;
  %end ;
  %else %do;
    %let qa_table = qadata.&&&module.table (keep=patid orderdate) ;
  %end;
%mend;
%get_qa_table ;
proc sql noprint;
  create table dplocal.&module._temp as
  select patid
       , orderdate
       , put(orderdate,yymmd.) as YearMonth
       , count(*) as n
  from &qa_table
  group by 1,2,3
  ;
quit;
%remove_labels(dplocal,&module._temp);

/* number of ordered prescriptions per patient per day */
proc sql noprint;
  create table dplocal.&module._n_patid_preid as
  select *
  from dplocal.&module._temp (drop=yearmonth)
  ;
quit;

*level 3 <statistics on number prescriptions per date visit: overall >;
%l2_procmeans_sum(libin=dplocal,dsin=&module._n_patid_preid,
                  libout=msoc,  dsout=&module._l3_pt_orderdate_stats,
                  keepvars=, vars=n, classvars=, 
                  names= n=pre sum=rxs
                  mean=mean std=std min=min p1=p1 p5=p5 p25=p25 median=median
                  p75=p75 p95=p95 p99=p99 max=max, numobs=max);
                  
proc datasets library=msoc nolist nodetails nowarn;
  modify &module._l3_pt_orderdate_stats;
  format pre rxs comma15. mean std 10.2 median 10.1;
  informat mean std 10.2 median 10.1;
run;quit;

/*< number of prescriptions per patid per year-month > */
proc sql noprint;
  drop table dplocal.&module._n_patid_preid
  ;
  create table dplocal.&module._pat_y as
  select patid
       , substr(yearmonth,1,4) as Year
       , sum(n) as n
  from dplocal.&module._temp (drop=orderdate)
  group by 1,2
  ;
  drop table dplocal.&module._temp
  ;
quit;

/*< Distribution of number of prescriptions per patid per year > */
proc means nolabels nonobs data=dplocal.&module._pat_y 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 library=dplocal nolist nodetails nowarn;
  delete &module._pat_y;
run;quit;

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