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

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

/* create additional temp procedure 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 px_codetype providerid) ;
    run;
    %let qa_table = t_view ;
  %end ;
  %else %do;
    %let qa_table = qadata.&&&module.table (keep=patid adate encounterid enctype px_codetype providerid) ;
  %end;
%mend;
%get_qa_table ;
    proc sql noprint;
      create table temp_223 as
      select a.patid
           , a.adate
           , a.encounterid
           , a.enctype
           , a.px_codetype
           , 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
      ;
    quit;

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

proc sql noprint;

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

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

quit;

/* 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 dplocal.ddate_223 as
  select b.ddate
       , a.encounterid
       , a.enctype
       , a.px_codetype
       , 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
  ;
quit;

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

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

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

proc sql noprint;
  drop table dplocal.&module._pxtype_adate
  ;
  drop table dplocal.&module._pxtype_ddate
  ;
  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
  ;
  /* ym, enc, px - for YearMonth values, use ddate when EncType=IP/IS, use adate when EncType ne IP/IS */
  create table msoc.&module._l3_enctype_pxtype_ym as
  select YearMonth
       , enctype label=' '
       , px_codetype label=' '
       , sum(n) as count format=comma15.
  from (select put(adate,yymmd.) as YearMonth
             , enctype
             , px_codetype
             , n
        from dplocal.&module._temp (keep=adate enctype px_codetype n)
        where enctype not in ("IP","IS")
        outer union corr 
        select put(ddate,yymmd.) as YearMonth
             , enctype
             , px_codetype
             , n
        from dplocal.ddate_223 (keep=ddate enctype px_codetype n)
        where not missing (ddate))
  group by 1,2,3
  ;
  drop table dplocal.&module._temp 
  ;
  drop table dplocal.ddate_223
  ;
quit;

/* patid*encounterid (number of procedures per patient-encounter - unique by px_codetype and px) */
%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;
  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 procedures per encounter visit: overall >;
%l2_procmeans_sum(libin=dplocal,dsin=&module._n_patid_encid,
                  libout=msoc,  dsout=&module._l3_px_per_enc_stats,
                  keepvars=, vars=n, classvars=,
                  names= n=enc sum=pxs 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._n_patid_encid;
quit;

proc datasets library=msoc nolist nodetails nowarn;
  modify &module._l3_px_per_enc_stats;
  format enc pxs comma15. mean std 10.2 median 10.1;
  informat mean std 10.2 median 10.1;
run;
quit;

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