/*-------------------------------------------------------------------------------------*\
|  PROGRAM NAME:                                                                        |
|     scdm_data_qa_review_inpatienttransfusion.sas                                      |
|                                                                                       |
|---------------------------------------------------------------------------------------|
|  PURPOSE:                                                                             |
|     The purpose of this program is to perform data quality checks on the Itransfusion |
|     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 Flags                                                                */
/*-------------------------------------------------------------------------------------*/;
* All TXN L2 flags are coded within the lkp_all_flags lookup table and 
  do not require additional program code;
%let level=2;
%level2;

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

%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 = tdate_start tdate_end ttime_start ttime_end
               transcode transcode_type bloodtype enctype) ;
    run;
    %let qa_table = t_view ;
  %end ;
  %else %do;
    %let qa_table = qadata.&&&module.table (keep = tdate_start tdate_end ttime_start ttime_end
               transcode transcode_type bloodtype enctype) ;
  %end;
%mend;
%get_qa_table ;
proc sql noprint;
  create table dplocal.&module._temp as 
  select tdate_start 
       , ttime_start
       , tdate_end
       , ttime_end
       , transcode 
       , transcode_type 
       , bloodtype 
       , enctype
       , case when not missing(ttime_start) then put(hour(ttime_start),z2.) 
         else '' end as Hour
       , count(*) as n
  from &qa_table
  group by 1,2,3,4,5,6,7,8,9
  ;
quit;
%remove_labels(dplocal,&module._temp)

proc sql noprint;
  create table dplocal.&module._temp_ym as 
  select enctype
       ,bloodtype
       ,transcode
       ,transcode_type
       ,case when not missing(tdate_start) then put(tdate_start,yymmd.) 
        else '' end as YearMonth
       ,tdate_start
       ,ttime_start
       ,tdate_end
       ,ttime_end
       ,count(*) as n
  from dplocal.&module._temp
  group by 1,2,3,4,5,6,7,8,9
  ;
quit;

proc sql noprint;
  create table dplocal.&module._temp_dates as
  select tdate_start
       , sum(n) as n
  from dplocal.&module._temp (keep=tdate_start n)
  group by 1
  ;
quit;
%date_percentiles (libin=dplocal, dsin=&module._temp_dates
                , libout=dplocal, dsout=temp_date_dist_&module._1, vars=tdate_start);

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

  create table dplocal.&module._temp_dates as
  select tdate_end
       , sum(n) as n
  from dplocal.&module._temp (keep=tdate_end n)
  group by 1
  ;
quit;
%date_percentiles (libin=dplocal, dsin=&module._temp_dates
                , libout=dplocal, dsout=temp_date_dist_&module._2, vars=tdate_end);
%set_ds (libin=dplocal, dsin_prefix=temp_date_dist_&module., libout=dplocal, dsout=date_dist_&module.);


* 2.2.9 Frequency of records stratified by transfusion year-month;
proc sql noprint;

  drop table dplocal.&module._temp_dates; 

  create table msoc.&module._l3_tdate_start_ym as
  select YearMonth
         , sum(n) as count format=comma15.
  from dplocal.&module._temp_ym (keep=yearmonth n)
  group by 1
  ;
quit;

* 2.2.10 Frequency of records stratified by encounter type and year-month;
proc sql noprint;
  create table msoc.&module._l3_enctype_ym as
    select YearMonth
               , enctype
               , sum(n) as count format=comma15.
  from dplocal.&module._temp_ym (keep=yearmonth enctype n)
  group by 1,2
  ;
quit;

* 2.2.11 Frequency of records stratified by bloodtype and year-month values;
proc sql noprint;
  create table msoc.&module._l3_bloodtype_ym as
  select yearmonth
         ,bloodtype
         ,sum(n) as count format=comma15.
  from dplocal.&module._temp_ym (keep=yearmonth bloodtype n)
  group by 1,2
  ;
quit;

* 2.2.12 Frequency of records stratified by bloodgroup and year-month values;
proc sql noprint;
  create table msoc.&module._l3_bloodgroup_ym as
  select yearmonth
         ,case when not missing(bloodtype) then compress(bloodtype,'+-') 
          else '' end as bloodgroup
         ,sum(count) as count format=comma15.
  from msoc.&module._l3_bloodtype_ym (keep=yearmonth bloodtype count)
  group by 1,2
  ;
quit;

* 2.2.13 Frequency of records stratified by transcode_type, transcode, 
  and year-month;
proc sql noprint;
  create table msoc.&module._l3_transcode_codetype_ym as
    select yearmonth
      ,transcode_type
      ,transcode
      ,sum(n) as count format = comma15.
  from dplocal.&module._temp_ym ds
  group by 1,2,3;
quit;

* 2.2.14  Statistical distribution of transfusion duration (in min) 
  stratified by year-month;

proc sql noprint;
  create table dplocal.&module._temp_dur (drop = _:) as
    select *
    ,case when not missing(tdate_start) AND not missing(ttime_start) then  
       dhms(tdate_start,hour(ttime_start),minute(ttime_start),second(ttime_start)) 
       else . end as _start_datetime 
    ,case when not missing(tdate_end) AND not missing(ttime_end) then  
       dhms(tdate_end,hour(ttime_end),minute(ttime_end),second(ttime_end)) 
       else . end as _end_datetime 
    ,case when not missing(calculated _end_datetime) AND not missing(calculated _start_datetime) then 
       intck('minute',calculated _start_datetime, calculated _end_datetime) 
       else . end as txnduration
    ,case when not missing(calculated txnduration) AND calculated txnduration = 0 then 1 else 0 end
       as n_zero
    ,case when not missing(calculated txnduration) AND calculated txnduration < 0 then 1 else 0 end
       as n_neg 
  from dplocal.&module._temp_ym (keep = yearmonth tdate_start ttime_start tdate_end ttime_end)
  ;

  create table dplocal.&module._temp_zer_neg as
    select yearmonth
        ,sum(n_neg) as n_neg
        ,sum(n_zero) as n_zero
    from dplocal.&module._temp_dur
    group by 1;

quit;

*level 3 number of transfusions per patid by year-month (n_neg, n_zero, mean, median, min, max);
%l2_procmeans_sum (libin=dplocal,
                   dsin=&module._temp_dur,
                   libout=dplocal,   
                   dsout=&module._temp_l3_trans_duration_stats,
                   keepvars=,
                   vars=txnduration,
                   classvars=yearmonth,
                   names= n=txn 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 sql;
  create table msoc.&module._l3_trans_duration_stats as
    select a.yearmonth
          ,a.txn
          ,coalesce(b.n_neg,0)as n_neg
          ,coalesce(b.n_zero,0) as n_zero
          ,a.mean   ,a.std  ,a.min
          ,a.p1     ,a.p5   ,a.p25
          ,a.median ,a.p75  ,a.p95
          ,a.p99    ,a.max
  from dplocal.&module._temp_l3_trans_duration_stats a
  left join dplocal.&module._temp_zer_neg b
  on a.yearmonth = b.yearmonth
  ;
quit;

proc datasets library=msoc nolist nodetails nowarn;
modify &module._l3_trans_duration_stats;
format n_neg n_zero txn: comma15. mean std p: min max median comma15.1;
informat n_neg n_zero txn: comma15. mean std p: min max median comma15.1;
quit;

proc datasets library=dplocal nolist nodetails nowarn;
  delete &module._temp:;
run;quit;

* 2.4.4 Frequency of transfusions per patient encounter stratified by year-month.;

%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.: ;
    run;
    %let qa_table = t_view ;
  %end ;
  %else %do;
    %let qa_table = qadata.&&&module.table ;
  %end;
%mend;
%get_qa_table ;
proc sql noprint; 
  create table dplocal.&module._temp_enc as
    select txn.encounterid         
          ,case when not missing(tdate_start) then put(tdate_start,yymmd.) 
             else '' end as YearMonth
          ,count(distinct txn.transid) as n
  from &qa_table txn
  group by txn.encounterid, yearMonth
  order by yearmonth
;

  create table dplocal.&module._temp_l3_trans_per_enc_stats as
    select encounterid
           ,yearmonth
           ,sum(n) as txncount
  from dplocal.&module._temp_enc
  group by 1,2
;
quit;

%l2_procmeans_sum (libin=dplocal,
                   dsin=&module._temp_l3_trans_per_enc_stats,
                   libout=msoc,   
                   dsout=&module._l3_trans_per_enc_stats,
                   keepvars=,
                   vars=txncount,
                   classvars=yearmonth,
                   names= n=enc 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_trans_per_enc_stats;
  format enc: comma15. mean std p: median 10.1 min max 10.;
  informat mean std p: median 10.1 min max 10.;
quit;

proc datasets library=dplocal nolist nodetails nowarn;
  delete &module._temp_:;
run;quit;

*-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-;
*  END scdm_data_qa_review_inpatienttransfusion.sas                                     ; 
*-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-;