/*-------------------------------------------------------------------------------------*\
|  PROGRAM NAME:                                                                        |
|     scdm_data_qa_review-patientsurvey.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=prm_code surveyid survey_name questionid questionverid question_dt) ;
    run;
    %let qa_table = t_view ;
  %end ;
  %else %do;
    %let qa_table = qadata.&&&module.table (keep=prm_code surveyid survey_name questionid questionverid question_dt) ;
  %end;
%mend;
%get_qa_table ;
proc sql noprint;
  create table dplocal.&module._temp as 
  select prm_code
       , surveyid
       , survey_name
       , questionid
       , questionverid
       , question_dt
       , count(*) as n
  from &qa_table
  group by 1,2,3,4,5  
  ;
quit;

proc sql noprint; 
  create table dplocal.&module._temp2 as
  select t.prm_code
       , s.surveyid
       , s.survey_name
       , s.questionid 
       , s.questionverid 
       , s.question_dt
       , t.prm_type
       , t.prm_subtype
  from dplocal.&module._temp s
  left join infolder.lkp_prm_type t
  on s.prm_code = t.prm_code
  order by prm_code
  ;
quit;
%remove_labels(dplocal,&module._temp)

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

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

proc sql noprint;
  create table msoc.all_l3_survey_dist as
    select prm_code
         , survey_name
         , count(unique surveyid) as n_surveyid format=15.
  from dplocal.&module._temp2
  group by 1,2
  ;
quit;

proc sql noprint;
  create table msoc.prs_l3_prm_code_type_subtype as
    select prm_code
         , prm_type
         , prm_subtype
         , count(unique questionverid) as nquestionverid
    from dplocal.&module._temp2
    group by 1,2,3
  ; 
quit;

proc sql noprint;
  create table msoc.prs_l3_surveyid_ver_ym (drop=n) as
    select surveyid
         , survey_name
         , questionverid
         , put(question_dt,yymmd.) as yearmonth length=7
         , count(*) as n
  from dplocal.&module._temp2
  where not missing(surveyID)
  group by 1,2,3,4
  ;

  drop table dplocal.&module._temp
           , dplocal.&module._temp2;
  ;
quit;

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