/*-------------------------------------------------------------------------------------*\
|  PROGRAM NAME:                                                                        |
|     scdm_data_qa_review-patientresponse.sas                                           |
|                                                                                       |
|---------------------------------------------------------------------------------------|
|  PURPOSE:                                                                             |
|     The purpose of this program is to perform data quality checks on the Patient      |
|     reported measures / outcomes (PRM) survey table.                                  |
|---------------------------------------------------------------------------------------|
|  PROGRAM INPUT:                                                                       |
|     see 00.0_scdm_prm_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;

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

/*-------------------------------------------------------------------------------------*/
/*  START Level 3                                                                      */
/*-------------------------------------------------------------------------------------*/
%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= patid prm_code surveyid questionverid
                             response_dt response_tm) ;
    run;
    %let qa_table = t_view ;
  %end ;
  %else %do;
    %let qa_table = qadata.&&&module.table (keep= patid prm_code surveyid questionverid
                     response_dt response_tm) ;

  %end;
%mend;
%get_qa_table ;
proc sql noprint;
  create table dplocal.&module._temp as 
  select patid
       , prm_code 
       , surveyid
       , questionverid 
       , response_dt
       , response_tm
       , count(*) as n
  from &qa_table
  group by 1,2,3,4,5,6
  ;

  create table dplocal.&module._temp2 as
    select r.*
      , count(*) as n
  from dplocal.&module._temp (drop = patid response_tm n) r
  group by 1,2,3,4
  ;
quit;
%remove_labels(dplocal,&module._temp)
%remove_labels(dplocal,&module._temp2)

%date_percentiles (libin=dplocal, dsin=&module._temp2, libout=dplocal, dsout=date_dist_&module., 
                  vars=response_dt);

proc sql noprint;
  create table msoc.&module._l3_response_dt_ym as
  select put(response_dt,yymmd.) as yearmonth length=7
       , sum(n) as count format=comma15.
  from dplocal.&module._temp2
  group by yearmonth
  ;
quit;

proc sql noprint;
  create table msoc.&module._l3_response_freq as
    select prm_code
        , surveyid
        , questionverid
        , sum(n) as count format=comma15.
  from dplocal.&module._temp2
  group by 1,2,3
  ;
quit;

proc sql noprint;
  create table dplocal.&module._n_patid_ym (drop= response_:) as
    select patid
        , response_dt
        , response_tm
        , put(response_dt,yymmd.) as yearmonth 
        , count(unique surveyid) as nsurvey
    from dplocal.&module._temp
    group by 1,2,3
   ;
 
  drop table dplocal.&module._temp
           , dplocal.&module._temp2
  ;
quit;

/*< Distribution of number of surveys reported per patid per yearmonth > */
proc means nolabels nonobs data=dplocal.&module._n_patid_ym missing 
    StackODSOutput n sum mean std min p1 p5 p25 median p75 p95 p99 max; 
  var nsurvey;
  class yearmonth;
  ods output summary=msoc.&module._l3_sid_pt_ym_stats (drop=_: variable rename=(n=n_ptym sum=n_svid stddev=Std));
run;

proc datasets library=msoc nolist nodetails nowarn;
  modify &module._l3_sid_pt_ym_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._n_patid_ym;
run;quit;

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