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

/* summarize diagnosis table at highest level to get base counts for other required summarization */
%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=adate enctype dx_codetype dx pdx padmit providerid) ;
    run;
    %let qa_table = t_view ;
  %end ;
  %else %do;
    %let qa_table = qadata.&&&module.table (keep=adate enctype dx_codetype dx pdx padmit providerid) ;
  %end;
%mend ;
%get_qa_table ;;
    proc sql noprint;   
      create table dplocal.&module._temp as
      select a.adate
           , a.enctype
           , a.dx_codetype
           , a.dx
           , a.pdx
           , a.padmit
           , 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,7;
    quit;
    %remove_labels(dplocal, &module._temp);

/* create additional temp diagnosis table for use in 223 check with patid and encounterid vars to merge w/ encounter */
%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 adate encounterid enctype dx_codetype pdx providerid) ;
    run;
    %let qa_table = t_view ;
  %end ;
  %else %do;
    %let qa_table = qadata.&&&module.table (keep=patid adate encounterid enctype dx_codetype pdx providerid) ;
  %end;
%mend ;
%get_qa_table ;;
    proc sql noprint;
      create table temp_223 as
      select a.patid
           , a.adate
           , a.encounterid 
           , a.enctype
           , a.dx_codetype
           , a.pdx
           , b.specialty
      from &qa_table as a
           left join
           qadata.&PVDTABLE (keep=providerid specialty) as b
      on a.providerid=b.providerid
      where a.enctype in ("IP","IS")
      order by 1,2,3,4,5,6,7
      ;
    quit;

/*-------------------------------------------------------------------------------------*/
/*  START Level 2 Data                                                                 */
/*-------------------------------------------------------------------------------------*/

/* join temp_223 dataset to encounter table to get ddate values and counts */
%macro get_qa_table ;
  %if &do_partitions and not %eval(&module in &tabid_exc) %then %do ;
    data t_view /view=t_view ;
      set qadata.&ENCTABLE.: (keep=patid adate encounterid enctype ddate where=(enctype in ("IP", "IS"))) ;
    run;
    %let qa_table = t_view ;
  %end ;
  %else %do;
    %let qa_table = qadata.&ENCTABLE (keep=patid adate encounterid enctype ddate where=(enctype in ("IP", "IS"))) ;
  %end;
%mend ;
%get_qa_table ;;
proc sql noprint;
  create table ddate_223 as
  select b.ddate
       , a.encounterid
       , a.enctype
       , a.dx_codetype
       , a.pdx
       , a.specialty
       , count(*) as n
  from temp_223 as a 
       left join 
       &qa_table as b
  on a.patid=b.patid and a.adate=b.adate and a.encounterid=b.encounterid
  group by 1,2,3,4,5,6
  ;
quit;

/* create temp dataset for checkid 223 - dx_codetype by ddate where EncType=IP/IS */
proc sql noprint;
  create table dplocal.&module._dxtype_ddate as
  select dx_codetype
       , ddate
       , sum(n) as count format=comma15.
  from ddate_223 (keep=ddate dx_codetype n enctype) 
  where dx_codetype in ("09","10") and not missing(ddate)
  group by dx_codetype, ddate
  ;
quit;

proc sql noprint;

/* dx, code type, provider specialty  */
    create table msoc.&module._l2_dx_dxtype_spec as
    select   dx_codetype
           , dx
           , specialty
           , sum(n) as count format=comma15.
    from dplocal.&module._temp (keep=dx dx_codetype specialty n)
    group by 1,2,3
    ;

/* ym, enc, dx, pds - for YearMonth values, use ddate when EncType=IP/IS, use adate when EncType ne IP/IS */
  create table msoc.&module._l2_enc_dxtype_pdx_ym as
  select YearMonth
       , enctype
       , dx_codetype
       , pdx
       , sum(n) as count format=comma15.
  from (select put(adate,yymmd.) as YearMonth
             , enctype
             , dx_codetype
             , pdx
             , n
        from dplocal.&module._temp (keep=adate enctype dx_codetype pdx n)
        where enctype not in ("IP","IS")
        outer union corr 
        select put(ddate,yymmd.) as YearMonth
             , enctype
             , dx_codetype
             , pdx
             , n
        from ddate_223 (keep=ddate enctype dx_codetype pdx n)
        where not missing (ddate))
  group by 1,2,3,4
  ;

/* temp dataset for checkid 223 - dx_codetype by adate where EncType ne IP/IS */
  create table dplocal.&module._dxtype_adate as
  select dx_codetype
       , adate
       , sum(n) as count format=comma15.
  from dplocal.&module._temp (keep=adate dx_codetype n enctype where=(dx_codetype in ("09","10") and enctype not in ("IP","IS")))
  group by 1,2
  ;
quit;


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

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

proc sql noprint;
  create table dplocal.&module._temp_dates 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_dates, libout=dplocal, dsout=date_dist_&module., vars=adate);

proc sql noprint;

  create table dplocal.&module._adate_ym_count as
  select put(adate,yymmd.) as YearMonth
       , sum(n) as count format=comma15.
  from dplocal.&module._temp_dates
  group by YearMonth;

  drop table dplocal.&module._temp_dates
  ; 
  drop table dplocal.&module._dxtype_adate
  ;
  drop table dplocal.&module._dxtype_ddate
  ;
/* padmit only: list of padmit with # of records */
  create table msoc.&module._l3_padmit as
  select padmit 
       , sum(n) as count format=comma15.
  from dplocal.&module._temp (keep=padmit n)
  group by 1
  ;
  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) ;
    run;
    %let qa_table = t_view ;
  %end ;
  %else %do;
    %let qa_table = qadata.&&&module.table (keep=patid encounterid) ;
  %end;
%mend ;
%get_qa_table ;;
proc sql noprint;
/* patid*encounterid (number of diagnosis per patient-encounter - not necessarily unique) */
  create table dplocal.&module._n_patid_encid as
  select patid, encounterid, count(*) as n
  from &qa_table
  group by 1,2
  ;
quit;

*level 3 < statistics on number of diagnosis per encounter visit: overall >;
%l2_procmeans_sum(libin=dplocal,dsin=&module._n_patid_encid,
                  libout=msoc,  dsout=&module._l3_dx_per_enc_stats,
                  keepvars=, vars=n, classvars=,
                  names= n=enc sum=dxs 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_dx_per_enc_stats;
  format enc dxs comma15. mean std 10.2  median 10.1;
  informat mean std 10.2  median 10.1;
run;
quit;

proc datasets lib=dplocal nowarn nolist nodetails;
  delete &module._n_patid_encid;
quit;

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