options orientation=portrait nocenter;
ods pdf file="C:\Projects\PROMIS-29\example\score29+2.pdf";

libname library "C:\Projects\PROMIS-29\example";
**************************************************************;
** read in data output from AC and restructure to 1 record per PIN/Assessment,
** incorporate Janel Hanmer Generic MAUT code 2017_09_01.sas code for PROPr,
** merge in GLOBAL07 from raw data and create t- and z-scores for construction of 
** PROMIS-29 v2.0 Physical and Mental Health summary scores (PH29 and MH29);
**************************************************************;

/**************************************************/
* read in data from Assessment Center;
PROC IMPORT OUT= WORK.AC
DATAFILE= "C:\Projects\PROMIS-29\example\prom29plus2ex.csv" DBMS=CSV REPLACE;
     GETNAMES=NO;
     DATAROW=6;
RUN;
/**************************************************/
data ac; set ac;
rename
var1=pin
var2=assmnt
var3=inst
var5=Theta
var6=Tscore;

*rename
var1=pin
var2=assmnt
var3=inst
var4=RawScore
var5=Theta
var6=Tscore
var7=SE
var8=ScrdCnt
var9=ItmCnt;
keep var1 var2 var3 var5 var6;
run;
/*
TITLE "Initial read from AC CSV file"; run;
proc freq; tables inst; run;
*/
/**************************************************/

/****************************************************/
/* restructure data so one record per PIN/assessment -  
   output Tscores and save thetas.
*/
/****************************************************/  

data PROPrData(keep=pin assmnt pain4a_t dep4a_t phy4a_t social4a_t fatig4a_t anx4a_t slpd4a_t cog2_t
 theta_pain theta_dep theta_phys theta_sr theta_fat theta_slp theta_cog); 
 set ac; by pin assmnt;
 
if first.assmnt then do;
pain4a_t  =.;    theta_pain  =.; 
dep4a_t   =.;    theta_dep   =.; 
phy4a_t   =.;    theta_phys  =.; 
social4a_t=.;    theta_sr    =.; 
fatig4a_t =.;    theta_fat   =.; 
anx4a_t   =.;    
slpd4a_t  =.;    theta_slp   =.; 
cog2_t    =.;    theta_cog   =.; 
end;

retain pin assmnt pain4a_t dep4a_t phy4a_t social4a_t fatig4a_t anx4a_t slpd4a_t cog2_t
  theta_pain theta_dep theta_phys theta_sr theta_fat theta_slp theta_cog;

if index(inst,"Pain Interference")  > 0 then do; theta_pain   =Theta; pain4a_t  =Tscore; end;
if index(inst,"Depression/Sadness") > 0 then do; theta_dep    =Theta; dep4a_t   =Tscore; end;
if index(inst,"Physical Function")  > 0 then do; theta_phys   =Theta; phy4a_t   =Tscore; end;
if index(inst,"Ability to Participate in Social Roles/Activities") 
                                    > 0 then do; theta_sr     =Theta; social4a_t=Tscore; end;
if index(inst,"Fatigue")            > 0 then do; theta_fat    =Theta; fatig4a_t =Tscore; end;
if index(inst,"Anxiety/Fear")       > 0 then do;                      anx4a_t   =Tscore; end;
if index(inst,"Sleep Disturbance")  > 0 then do; theta_slp    =Theta; slpd4a_t  =Tscore; end;
if index(inst,"Cognitive Function") > 0 then do; theta_cog    =Theta; cog2_t    =Tscore; end;

if last.assmnt then output;
run;
/**************************************************/

TITLE "Restructured results from AC"; run;
proc print data=PROPrData noobs;
	id pin;
 var pain4a_t dep4a_t phy4a_t social4a_t fatig4a_t anx4a_t slpd4a_t cog2_t
      theta_pain theta_dep theta_phys theta_sr theta_fat theta_slp theta_cog;
run;

/***********************************************/
%include "C:\Projects\PROMIS-29\example\Generic MAUT code 2017_09_01.sas" /source2;

data PROPrData; set PROPrData;
label PROPr="PROPr (MAUT J Hanmer, Sept 2017)"; 
run;

TITLE "PROPr"; run;
proc means data=PROPrData(drop=pin assmnt) maxdec=2; 
*class assmnt;
var theta_pain theta_dep theta_phys theta_sr theta_fat theta_slp theta_cog
pain_utility depression_utility physical_utility social_utility fatigue_utility 
sleep_utility cognition_utility PROPr;
run;
/***********************************************/

/***********************************************/
* pick up single-item 0-10 pain intensity item from original dataset;

DATA pain;
 set library.prom29plus2(keep=pin assmnt global07);
run;
proc sort data=pain; by pin assmnt; run;

data all; merge PROPrData pain; by pin assmnt;
	
* create Z-score for pain_average using PROMIS Wave 1 general population mean=2.31, sd=2.34;
pain1_z   = (global07 - 2.31)/2.34;

* create Z-scores from T;
pain4a_z    = (pain4a_t   - 50)/10;
dep4a_z     = (dep4a_t    - 50)/10;
phy4a_z     = (phy4a_t    - 50)/10;
social4a_z  = (social4a_t - 50)/10;
fatig4a_z   = (fatig4a_t  - 50)/10;
anx4a_z     = (anx4a_t    - 50)/10;
slpd4a_z    = (slpd4a_t   - 50)/10;	
cog2_z      = (cog2_t     - 50)/10;	

*compute composite for pain interference and pain intensity using Z-scores;
painav_z  = mean(of pain4a_z pain1_z);

* compute composite for emotion using anxiety and depression Z-scores;
emot_z    = mean(of anx4a_z dep4a_z);

* create T-score versions;
pain1_t   = 50 + (pain1_z * 10);
painav_t  = 50 + (painav_z * 10);
emot_t    = 50 + (emot_z * 10);

label
painav_z  ="pain composite (Z)"
emot_z    ="anxiety/dep composite (Z)"
painav_t  ="pain composite (T)"
emot_t    ="anxiety/dep composite (T)"

social4a_z="Ability to Part in Soc Roles/Activ (Z)"
anx4a_z   ="Anxiety/Fear (Z)"                                     
dep4a_z   ="Depression/Sadness (Z)"                               
fatig4a_z ="Fatigue (Z)"                                          
pain4a_z  ="Pain Interference (Z)"                                
phy4a_z   ="Physical Function (Z)"                                
slpd4a_z  ="Sleep Disturbance (Z)"                                
pain1_z   ="Pain intensity (Z)"

social4a_t="Ability to Part in Soc Roles/Activ (T)"
anx4a_t   ="Anxiety/Fear (T)"                                     
dep4a_t   ="Depression/Sadness (T)"                               
fatig4a_t ="Fatigue (T)"                                          
pain4a_t  ="Pain Interference (T)"                                
phy4a_t   ="Physical Function (T)"                                
slpd4a_t  ="Sleep Disturbance (T)"                                
pain1_t   ="Pain intensity (T)"
;

/* factor scoring coefficients from "PROMIS-29 V2.0 Physical and 
   Mental Health Summary Scores" - Hays/Spritzer/Schalet/Cella 
   
ph_phyfun=    0.872 ;     mh_phyfun= -0.015 ;
ph_painav=   -0.094 ;     mh_painav= -0.154 ;
ph_social=    0.113 ;     mh_social=  0.252 ;
ph_fatig =   -0.009 ;     mh_fatig = -0.351 ;
ph_sleep =    0.002 ;     mh_sleep = -0.139 ;
ph_emot  =    0.003 ;     mh_emot  = -0.257 ;
*/

/* multiple z-scores with corresponding factor scoring coefficient */
ph29_z=(phy4a_z    *  0.872) +
       (painav_z   * -0.094) +
       (social4a_z *  0.113) +
       (fatig4a_z  * -0.009) +
       (slpd4a_z   *  0.002) +
       (emot_z     *  0.003);

mh29_z=(phy4a_z    * -0.015) +
       (painav_z   * -0.154) +
       (social4a_z *  0.252) +
       (fatig4a_z  * -0.351) +
       (slpd4a_z   * -0.139) +
       (emot_z     * -0.257);

* create T-score versions: ;
ph29_t= 50 + (ph29_z * 10);
mh29_t= 50 + (mh29_z * 10);

label 
ph29_z="PROMIS29 v2.0 PH summary (Z)"
mh29_z="PROMIS29 v2.0 MH summary (Z)"

ph29_t="PROMIS29 v2.0 PH summary (T)"
mh29_t="PROMIS29 v2.0 MH summary (T)";

run;

TITLE "Score29"; run;
proc means  data=all maxdec=2; 
var 	ph29_t mh29_t phy4a_t painav_t social4a_t fatig4a_t slpd4a_t emot_t
    anx4a_t dep4a_t pain4a_t pain1_t ;
*class assmnt;
run;
TITLE "Score29 at assessment 1 - correlations"; run;
proc corr data=all; where assmnt=1; 
var 	ph29_t mh29_t phy4a_t painav_t social4a_t fatig4a_t slpd4a_t emot_t
    anx4a_t dep4a_t pain4a_t pain1_t PROPr;
run;
*-------------------------------------------------------------;