********************************************************************;
***  CREATE SUMMARY SCALES *****************************************;
********************************************************************;

DATA sf36c(keep=id agg_phys agg_ment pf_t rp_t bp_t gh_t em_t re_t sf_t en_t
rpcs12 rmcs12 physhlt4 menthlt4 physhlt3 menthlt2 fsfm1 fsfm2
hui2);  

   set sf36a(keep=id i1-i36 age phyfun10 rolep4 pain2 sfpain genh5 
                  emot5 rolee3 socfun2 enfat4 sfgenh5);

*****************************************************************;
*** NEMC PHYSICAL AND MENTAL HEALTH COMPOSITE - SF36 ************;
*****************************************************************;

*** 1) Transform SF-36 scores (NEMC Scoring) to z-scores; ******* ;
***    1998 US general population means and SD's are used here ** ;
***    (not age/gender based) *********************************** ;

   PF_Z = (PHYFUN10 - 82.96845) / 23.83795 ;
   RP_Z = (ROLEP4   - 77.93107) / 35.34865 ;
   BP_Z = (SFPAIN   - 70.22865) / 23.35310 ;
   GH_Z = (SFGENH5  - 70.10060) / 21.35900 ;
   EM_Z = (EMOT5    - 75.21913) / 17.60698 ;
   RE_Z = (ROLEE3   - 83.10276) / 31.64149 ;
   SF_Z = (SOCFUN2  - 83.56494) / 23.02758 ;
   EN_Z = (ENFAT4   - 56.99917) / 21.12677 ;
   
*** 2) Create physical and mental health composite scores: **********;
***    Multiply z-scores by varimax-rotated factor scoring **********;
***    coefficients and sum the products ****************************;

   AGG_PHYS = (PF_Z * 0.42402) + (RP_Z * 0.35119) + (BP_Z * 0.31754) +
              (GH_Z * 0.24954) + (EM_Z * -.22069) + (RE_Z * -.19206) +
              (SF_Z * -.00753) + (EN_Z * 0.02877);
   AGG_MENT = (PF_Z * -.22999) + (RP_Z * -.12329) + (BP_Z * -.09731) +
              (GH_Z * -.01571) + (EM_Z * 0.48581) + (RE_Z * 0.43407) +
              (SF_Z * 0.26876) + (EN_Z * 0.23534);

*** 3) Transform composite and scale scores to T-scores: ****** ;

   AGG_PHYS = 50 + (AGG_PHYS * 10);
   AGG_MENT = 50 + (AGG_MENT * 10);

   label agg_phys="NEMC physical health T-score - SF36";
   label agg_ment="NEMC mental health T-score - SF36";

   PF_T = 50 + (PF_Z * 10) ;
   RP_T = 50 + (RP_Z * 10) ;
   BP_T = 50 + (BP_Z * 10) ;
   GH_T = 50 + (GH_Z * 10) ;
   EM_T = 50 + (EM_Z * 10) ;
   RE_T = 50 + (RE_Z * 10) ;
   SF_T = 50 + (SF_Z * 10) ;
   EN_T = 50 + (EN_Z * 10) ;

   label pf_t="NEMC physical functioning T-score";
   label rp_t="NEMC role limitation physical T-score";
   label bp_t="NEMC pain T-score";
   label gh_t="NEMC general health T-score";
   label em_t="NEMC emotional well-being T-score";
   label re_t="NEMC role limitation emotional T-score";
   label sf_t="NEMC social functioning T-score";
   label en_t="NEMC energy/fatigue T-score";

*****************************************************************;
*** NEMC PHYSICAL AND MENTAL HEALTH COMPOSITE SCORES - SF-12 ****;
*****************************************************************;

*** 1) Create dummy variables for the SF-12 items: **************;

IF I1=0 THEN GH1_1=1;
 ELSE IF I1>=0 THEN GH1_1=0;
IF I1=25 THEN GH1_2=1;
 ELSE IF I1>=0 THEN GH1_2=0;
IF I1=50 THEN GH1_3=1;
 ELSE IF I1>=0 THEN GH1_3=0;
IF I1=75 THEN GH1_4=1;
 ELSE IF I1>=0 THEN GH1_4=0;
IF I4=0 THEN PF02_1=1;
 ELSE IF I4>=0 THEN PF02_1=0;
IF I4=50 THEN PF02_2=1;
 ELSE IF I4>=0 THEN PF02_2=0;
IF I6=0 THEN PF04_1=1;
 ELSE IF I6>=0 THEN PF04_1=0;
IF I6=50 THEN PF04_2=1;
 ELSE IF I6>=0 THEN PF04_2=0;
IF I14=0 THEN RP2_1=1;
 ELSE IF I14>=0 THEN RP2_1=0;
IF I15=0 THEN RP3_1=1;
 ELSE IF I15>=0 THEN RP3_1=0;
IF I18=0 THEN RE2_1=1;
 ELSE IF I18>=0 THEN RE2_1=0;
IF I19=0 THEN RE3_1=1;
 ELSE IF I19>=0 THEN RE3_1=0;
IF I22=0 THEN BP2_1=1;
 ELSE IF I22>=0 THEN BP2_1=0;
IF I22=25 THEN BP2_2=1;
 ELSE IF I22>=0 THEN BP2_2=0;
IF I22=50 THEN BP2_3=1;
 ELSE IF I22>=0 THEN BP2_3=0;
IF I22=75 THEN BP2_4=1;
 ELSE IF I22>=0 THEN BP2_4=0;
IF I26=0 THEN EM3_1=1;
 ELSE IF I26>=0 THEN EM3_1=0;
IF I26=20 THEN EM3_2=1;
 ELSE IF I26>=0 THEN EM3_2=0;
IF I26=40 THEN EM3_3=1;
 ELSE IF I26>=0 THEN EM3_3=0;
IF I26=60 THEN EM3_4=1;
 ELSE IF I26>=0 THEN EM3_4=0;
IF I26=80 THEN EM3_5=1;
 ELSE IF I26>=0 THEN EM3_5=0;
IF I27=0 THEN EN2_1=1;
 ELSE IF I27>=0 THEN EN2_1=0;
IF I27=20 THEN EN2_2=1;
 ELSE IF I27>=0 THEN EN2_2=0;
IF I27=40 THEN EN2_3=1;
 ELSE IF I27>=0 THEN EN2_3=0;
IF I27=60 THEN EN2_4=1;
 ELSE IF I27>=0 THEN EN2_4=0;
IF I27=80 THEN EN2_5=1;
 ELSE IF I27>=0 THEN EN2_5=0;
IF I28=0 THEN EM4_1=1;
 ELSE IF I28>=0 THEN EM4_1=0;
IF I28=20 THEN EM4_2=1;
 ELSE IF I28>=0 THEN EM4_2=0;
IF I28=40 THEN EM4_3=1;
 ELSE IF I28>=0 THEN EM4_3=0;
IF I28=60 THEN EM4_4=1;
 ELSE IF I28>=0 THEN EM4_4=0;
IF I28=80 THEN EM4_5=1;
 ELSE IF I28>=0 THEN EM4_5=0;
IF I32=0 THEN SF2_1=1;
 ELSE IF I32>=0 THEN SF2_1=0;
IF I32=25 THEN SF2_2=1;
 ELSE IF I32>=0 THEN SF2_2=0;
IF I32=50 THEN SF2_3=1;
 ELSE IF I32>=0 THEN SF2_3=0;
IF I32=75 THEN SF2_4=1;
 ELSE IF I32>=0 THEN SF2_4=0;

*** 2) Create SF-12 aggregates: *************************;

   RAWPCS12 = (PF02_1 *  -7.23216) + (PF02_2 * -3.45555) +
              (PF04_1 *  -6.24397) + (PF04_2 * -2.73557) +
              (RP2_1  *  -4.61617) + (RP3_1  * -5.51747) +
              (BP2_1  * -11.25544) + (BP2_2  * -8.38063) +
              (BP2_3  *  -6.50522) + (BP2_4  * -3.80130) +
              (GH1_1  *  -8.37399) + (GH1_2  * -5.56461) +
              (GH1_3  *  -3.02396) + (GH1_4  * -1.31872) +
              (EN2_1  *  -2.44706) + (EN2_2  * -2.02168) +
              (EN2_3  *  -1.61850) + (EN2_4  * -1.14387) +
              (EN2_5  *  -0.42251) + (SF2_1  * -0.33682) +
              (SF2_2  *  -0.94342) + (SF2_3  * -0.18043) +
              (SF2_4  *   0.11038) + (RE2_1  *  3.04365) +
              (RE3_1  *   2.32091) + (EM3_1  *  3.46638) +
              (EM3_2  *   2.90426) + (EM3_3  *  2.37241) +
              (EM3_4  *   1.36689) + (EM3_5  *  0.66514) +
              (EM4_1  *   4.61446) + (EM4_2  *  3.41593) +
              (EM4_3  *   2.34247) + (EM4_4  *  1.28044) +
              (EM4_5  *   0.41188);
   RAWMCS12 = (PF02_1 *   3.93115) + (PF02_2 *   1.86840) +
              (PF04_1 *   2.68282) + (PF04_2 *   1.43103) +
              (RP2_1  *   1.44060) + (RP3_1  *   1.66968) +
              (BP2_1  *   1.48619) + (BP2_2  *   1.76691) +
              (BP2_3  *   1.49384) + (BP2_4  *   0.90384) +
              (GH1_1  *  -1.71175) + (GH1_2  *  -0.16891) +
              (GH1_3  *   0.03482) + (GH1_4  *  -0.06064) +
              (EN2_1  *  -6.02409) + (EN2_2  *  -4.88962) +
              (EN2_3  *  -3.29805) + (EN2_4  *  -1.65178) +
              (EN2_5  *  -0.92057) + (SF2_1  *  -6.29724) +
              (SF2_2  *  -8.26066) + (SF2_3  *  -5.63286) +
              (SF2_4  *  -3.13896) + (RE2_1  *  -6.82672) +
              (RE3_1  *  -5.69921) + (EM3_1  * -10.19085) +
              (EM3_2  *  -7.92717) + (EM3_3  *  -6.31121) +
              (EM3_4  *  -4.09842) + (EM3_5  *  -1.94949) +
              (EM4_1  * -16.15395) + (EM4_2  * -10.77911) +
              (EM4_3  *  -8.09914) + (EM4_4  *  -4.59055) +
              (EM4_5  *  -1.95934);

*** 3) Transform the aggregates: ********************************;

   RPCS12 = RAWPCS12 + 56.57706 ;
   RMCS12 = RAWMCS12 + 60.75781 ;

   label rpcs12="NEMC physical health T-score - SF12";
   label rmcs12="NEMC mental health T-score - SF12";

*****************************************************************;
*** PHYSICAL AND MENTAL HEALTH SCALES - DERIVED FROM MEAN OF ****;
*** SELECTED INDIVIDUAL NEMC T-SCORES ***************************;
*****************************************************************;

   PHYSHLT4=mean(PF_T, RP_T, BP_T, GH_T);
   MENTHLT4=mean(EM_T, RE_T, SF_T, EN_T);
   PHYSHLT3=mean(PF_T, RP_T, BP_T);
   MENTHLT2=mean(EM_T, RE_T);

   label physhlt4="physical T-score: mean(PF_T,RP_T,BP_T,GH_T)";
   label menthlt4="mental T-score: mean(EM_T,RE_T,SF_T,EN_T)";
   label physhlt3="physical T-score: mean(PF_T,RP_T,BP_T)";
   label menthlt2="mental T-score: mean(EM_T,RE_T)";

*****************************************************************;
*** PHYSICAL and MENTAL HEALTH FACTORS **************************;
*****************************************************************;

*** 1) Derive z-scores using age/gender adjusted US general *****; 
***    population means and SD's, then multiply each z-score by *;
***    oblique factor scoring coefficient and sum the products **;

  FFM1=   0.35089 * (PHYFUN10 - 79.9348918)/ 22.709038 
        + 0.30827 * (ROLEP4  - 76.2860947)/ 34.945958
        + 0.28347 * (SFPAIN  - 72.6616442)/ 23.807217
        + 0.21645 * (SFGENH5 - 69.3384676)/ 20.141323
        - 0.10780 * (EMOT5  - 74.6684030)/ 18.209059
        - 0.05148 * (ROLEE3  - 79.9677043)/ 34.229373
        + 0.11250 * (SOCFUN2 - 82.1785891)/ 23.568127
        + 0.09241 * (ENFAT4  - 59.5636224)/ 21.334831;

  FFM2= -0.13426 * (PHYFUN10 - 79.9348918)/ 22.709038 
        -0.02762 * (ROLEP4  - 76.2860947)/ 34.945958
        -0.00386 * (SFPAIN  - 72.6616442)/ 23.807217
        +0.08775 * (SFGENH5 - 69.3384676)/ 20.141323
        +0.44386 * (EMOT5  - 74.6684030)/ 18.209059
        +0.36103 * (ROLEE3  - 79.9677043)/ 34.229373
        +0.19318 * (SOCFUN2 - 82.1785891)/ 23.568127 
        +0.25868 * (ENFAT4  - 59.5636224)/ 21.334831 ;
  
*** 2) Transform composite scores to t-scores *******************;

  FSFM1 = (FFM1 * 10) +50;
  FSFM2 = (FFM2 * 10) +50;

  label fsfm1="Physical health factor T-score";
  label fsfm2="Mental health factor T-score";
  
%include 'hui2.sas' /source2;  
  
run;
proc sort data=sf36c; by id; run;