/*-------------------------------------------------------------------------------------*\
|  PROGRAM NAME:                                                                        |
|     scdm_data_qa_review-encounter.sas                                                 |
|                                                                                       |
|---------------------------------------------------------------------------------------|
|  PURPOSE:                                                                             |
|     The purpose of this program is to perform data quality checks on the              |
|              Encounter 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 Data                                                                 */
/*-------------------------------------------------------------------------------------*/
%let 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=enctype admitting_source discharge_disposition discharge_status adate ddate) ;
    run;
    %let qa_table = t_view ;
  %end ;
  %else %do;
    %let qa_table = qadata.&&&module.table (keep=enctype admitting_source discharge_disposition discharge_status adate ddate) ;
  %end;
%mend;
%get_qa_table ;
proc sql noprint;
  create table dplocal.&module._temp as
  select enctype
       , admitting_source
       , discharge_disposition
       , discharge_status
       , put(adate,yymmd.) as YearMonth 
       , ddate
       , count(*) as n
  from &qa_table
  group by 1,2,3,4,5,6
  ;
quit;
%remove_labels(dplocal, &module._temp);

/* temp dataset for CheckID 223 for enctype by ddate */
proc sql noprint; 
  create table dplocal.&module._enctype_ddate_ym as
  select enctype
       , ddate
       , sum(n) as count format=comma15.
  from dplocal.&module._temp (keep=enctype ddate n)
  group by 1,2
  ;
quit;

/* temp dataset for CheckID 244 - creates temp fields that can be linked to lookup to identify invalid combinations*/
proc sql noprint;
  create table dplocal.&module._l2_encChecks as
  select enctype
       , case when missing(discharge_disposition) then 'Null' 
              else discharge_disposition              
         end as _discharge_disposition length=4
       , case when missing(discharge_status) then 'Null' 
              else discharge_status           
         end as _discharge_status length=4
       , case when ddate = .S then 'Special missing'
              when not missing(ddate) then 'Present' 
              else 'Null'             
         end as _ddate length=15
       , sum(n) as count format=comma15.
  from dplocal.&module._temp (drop=admitting_source)
  group by 1,2,3,4
  ;
quit; 

/* temp dataset for CheckID 245 - creates temp fields that can be linked to lookup with valid value combinations */

    /* calculate rate of value combinations by encounter type */
    proc sql noprint;
     create table dplocal.&module._l2_encrate as
        select    a.*
                , b.Total
                , (a.count/b.Total)*100  as rate 
        from dplocal.&module._l2_encChecks as a
             left join 
             (select  enctype
                    , sum(count) as total
                from dplocal.&module._l2_encChecks
                group by encType) as b
            on a.encType = b.encType;
    quit;

    /* select and retain invalid records only */
    /* this dataset will be input used for check 245 */
    data dplocal.&module._l2_threshchecks;

        * Load dataset descriptors ;
        if 0 then set infolder.lkp_enc_l2;

        * Hash for valid lookup;
        declare hash existing (hashexp:16, dataset:"infolder.lkp_enc_l2");
        existing.definekey(all:'yes');
        existing.definedone();
  
        do until(done);
            set dplocal.&module._l2_encrate end=done;

            * flag non-matching recs as non-valid;
            if existing.check() ne 0 then output; 
        end;
        stop;

    run;   


/* level 2 MSOC output */
proc sql noprint;
  create table msoc.&module._l2_enctype_admit_dc as
  select enctype
       , admitting_source
       , discharge_disposition
       , discharge_status
       , sum(n) as count format=comma15.
  from dplocal.&module._temp (keep=enctype admitting_source discharge_disposition discharge_status n)
  group by 1,2,3,4
  ;
quit;

proc sql noprint;
  create table msoc.&module._l2_enctype_dc_ddate_spmiss as
  select enctype
       , discharge_disposition
       , discharge_status
       , case when ddate = .S then 'Not discharged'
              when not missing(ddate) then 'DDate present' 
              else 'DDate null' 
         end as ddate_check 
       , sum(n) as count format=comma15.
  from dplocal.&module._temp (drop=admitting_source)
  group by 1,2,3,4
  ;
quit;

proc sql noprint;
  create table msoc.&module._l2_enctype_spmiss_ddate_ym as
  select enctype
       , yearmonth
       , ddate
       , sum(n) as count format=comma15.
  from dplocal.&module._temp (keep=enctype yearmonth ddate n where=(ddate=.s))
  group by 1,2,3
  ;
quit;

proc sql noprint;
  create table msoc.&module._l2_enctype_ddate_ym as
  select enctype
       , case when ddate=.S then "S"
              when ddate=. then " "
              else put(ddate,yymmd.)
         end as YearMonth length=7
       , sum(n) as count format=comma15.
  from dplocal.&module._temp (keep=enctype ddate n)
  group by 1,2
  ;
quit;

proc sql noprint;
  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=enctype adate drg drg_type) ;
    run;
    %let qa_table = t_view ;
  %end ;
  %else %do;
    %let qa_table = qadata.&&&module.table (keep=enctype adate drg drg_type) ;
  %end;
%mend;
%get_qa_table ;
proc sql noprint;
  create table dplocal.&module._temp (rename=(_adate=adate)) as
  select enctype
       , drg
       , drg_type
       , put(adate,yymmd.) as YearMonth
       , case when adate lt '01oct2007'd then '30sep2007'd 
              when adate ge '01oct2007'd then '01oct2007'd
              else .
         end as _adate
       , count(*) as n
  from &qa_table
  group by 1,2,3,4,5
  ;
quit;
%remove_labels(dplocal, &module._temp);

/* temp dataset for CheckID 229 for drg_type by adate */
proc sql noprint;
  create table dplocal.&module._drgtype_adate as
  select drg_type
       , adate
       , count(*) as n
  from dplocal.&module._temp (keep=adate drg_type n)
  group by 1,2
  ;
quit;

proc sql noprint;
  create table msoc.&module._l2_enctype_drg_drgtype as
  select enctype
       , drg
       , drg_type
       , sum(n) as count format=comma15.
  from dplocal.&module._temp (keep=enctype drg drg_type n)
  group by 1,2,3
  ;
quit;

proc sql noprint;
  create table msoc.&module._l2_enctype_drgtype_ym as
  select enctype
       , drg_type
       , yearmonth
       , sum(n) as count format=comma15.
  from dplocal.&module._temp (keep=yearmonth enctype drg_type n)
  group by 1,2,3
  ;
quit;

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

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

/* remove temporary output from level 2 encounter checks */
proc sql noprint;
  drop table   dplocal.&module._drgtype_adate
             , dplocal.&module._enctype_ddate_ym
             , dplocal.&module._l2_threshchecks
             , dplocal.&module._l2_encChecks
             , dplocal.&module._l2_encrate;
quit;

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

%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=enctype adate ddate) ;
    run;
    %let qa_table = t_view ;
  %end ;
  %else %do;
    %let qa_table = qadata.&&&module.table (keep=enctype adate ddate) ;
  %end;
%mend;
%get_qa_table ;
proc sql noprint;
  create table dplocal.&module._temp as
  select enctype
       , adate
       , ddate
       , count(*) as n 
  from &qa_table
  group by 1,2,3
  ;
quit;
%remove_labels(dplocal, &module._temp);

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

proc sql noprint;
  drop table dplocal.&module._temp_adate
  ;
  create table dplocal.&module._temp_ddate as
  select ddate
       , sum(n) as n
  from dplocal.&module._temp (keep=ddate n)
  group by 1
  ;
quit;
%date_percentiles (libin=dplocal, dsin=&module._temp_ddate, libout=dplocal, dsout=temp_date_dist_&module._2, vars=ddate);
%set_ds (libin=dplocal, dsin_prefix=temp_date_dist_&module., libout=dplocal, dsout=date_dist_&module.);

proc sql noprint;
  drop table dplocal.&module._temp_ddate
  ;
  create table msoc.&module._l3_enctype_los_ym as
  select enctype
       , put(adate,yymmd.) as YearMonth
       , case when not missing(ddate) then ddate-adate+1
              else .
         end as LOS label="Length of Stay"
       , sum(n) as count format=comma15.
  from dplocal.&module._temp
  group by 1,2,3
  ;
  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=patid encounterid enctype adate) ;
    run;
    %let qa_table = t_view ;
  %end ;
  %else %do;
    %let qa_table = qadata.&&&module.table (keep=patid encounterid enctype adate) ;
  %end;
%mend;
%get_qa_table ;
proc sql noprint;
  create table dplocal.&module._for_enc as
  select patid
       , encounterid
       , enctype
       , put(adate,yymmd.) as YearMonth
       , count(*) as n
  from &qa_table
  group by 1,2,3,4
  ;
quit;
%remove_labels(dplocal,&module._for_enc);

/* number of encounters per patid by enctype by year-month */
proc sql noprint;
  create table dplocal.&module._enc_pat_ym as
  select patid, enctype, yearmonth, sum(n) as n
  from dplocal.&module._for_enc 
  group by 1,2,3
  ; 
  drop table dplocal.&module._for_enc
  ;
quit;

/*level 3 <number of encounters per patid by enctype by year-month (mean, median, min, max) */
%l2_procmeans_sum (libin=dplocal,
                   dsin=&module._enc_pat_ym,
                   libout=msoc,   
                   dsout=&module._l3_enctype_pt_ym_stats,
                   keepvars=,
                   vars=n,
                   classvars=enctype yearmonth,
                   names=n=n_patid sum=records mean=mean std=std min=min p1=p1 p5=p5 p25=p25 median=median p75=p75 p95=p95 p99=p99 max=max,
                   numobs=max);

 
/* number of encounters per patid by year-month */                 
proc sql noprint;
  create table dplocal.&module._pat_ym as
  select patid
       , yearmonth
       , sum(n) as n
  from dplocal.&module._enc_pat_ym
  group by 1,2
  ;
  drop table dplocal.&module._enc_pat_ym
  ;
  quit;
  
/*level 3 <number of encounters per member by year-month (mean, median, min, max)*/
%l2_procmeans_sum (libin=dplocal,
                   dsin=&module._pat_ym,
                   libout=msoc,
                   dsout=&module._l3_pt_ym_stats,
                   keepvars=,
                   vars=n,
                   classvars=yearmonth,
                   names=n=n_patid sum=records 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 lib=dplocal nowarn nolist nodetails;
        delete &module._pat_ym;
quit;

proc datasets library=msoc nolist nodetails nowarn;

  modify &module._l3_enctype_pt_ym_stats;
      format n_patid records comma15. mean std 10.2 median 10.1;
      informat mean std 10.2 median 10.1;

  modify &module._l3_pt_ym_stats;
      format n_patid records comma15. mean std 10.2 median 10.1;
      informat mean std 10.2 median 10.1;

quit;

*-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-;
*  END scdm_data_qa_review-encounter.sas                                                ;
*-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-;