Coefficient Alpha and Item Analysis

 /****************************************************************/
 /*          S A S   S A M P L E   L I B R A R Y                 */
 /*                                                              */
 /*    NAME: ALPHA2                                              */
 /*   TITLE: Coefficient Alpha and Item Analysis                 */
 /* PRODUCT: IML                                                 */
 /*    KEYS: SSTAT MATRIX                                        */
 /*   PROCS: IML PRINT                                           */
 /*    DATA:                                                     */
 /*                                                              */
 /* SUPPORT: LWB                         UPDATE:                 */
 /*     REF:                                                     */
 /*                                                              */
 /****************************************************************/

DATA;
   DROP N C;
   ARRAY X X1-X5;
   DO N=1 TO 10;
      C=INT(RANNOR(12345)*2)+5;
      DO OVER X;
         X=C+INT(RANNOR(12345));
      END;
      OUTPUT;
   END;
RUN;
PROC PRINT;
RUN;

PROC IML;
  RESET AUTONAME ;

  *---------------------------------------------------------+
  | IML CODE TO COMPUTE COEFFICIENT ALPHA AND VARIOUS       |
  | OTHER DESCRIPTIVE STATISTICS, MODELED AFTER THE SPSS    |
  | RELIABILITY SUBPROGRAM.  SEE "SPSS STATISTICAL          |
  | ALGORITHMS"                                             |
  +---------------------------------------------------------;

  START DOSTATS;
     T1= TEMP[,:];
     T3=(TEMP-T1)[,##]/( NCOL(TEMP)-1);
     T2= SQRT(T3<>0);
     T4= TEMP[,><];
     T5= TEMP[,<>];
     T6=T5-T4;
     T7=T5/(T4<>1E-12);
     STATSTAT=STATSTAT//(T1||T2||T3||T4||T5||T6||T7);
  FINISH;

  USE DATA1 (KEEP=X1-X5);
  READ ALL INTO X [COLNAME=ITEMNAME];
  NITEM= NCOL(X);
  NOBS= NROW(X);
  X=X|| X[,+];   * ADJOIN COLUMN OF TOTALS TO DATA MATRIX;
  TEMP='TOTAL';
  ITEMNAME=ITEMNAME||TEMP;
  NTOTAL=NITEM+1;
  MEAN= mean(X);   * BASIC DESCRIPTIVE STATISTICS;
  VARIANCE=var(X);
  STD= SQRT(VARIANCE<>0);
  MINIMUM= X[><,];
  MAXIMUM= X[<>,];
  RANGE=MAXIMUM-MINIMUM;
  RATIO=MAXIMUM/(MINIMUM<>1E-12);
  STATS=MEAN//STD//VARIANCE//MINIMUM//MAXIMUM//RANGE//RATIO;

  STATSTAT= J(1,7,0);      * INITIALIZE BEFORE INVOKING DOSTAT;
  DO K=1 TO 7;             * GET STATISTICS FOR STATISTICS;
     TEMP= STATS[K,1:NITEM];
     RUN DOSTATS;
     END;
  STATS=STATS`;
  STATNAME={'MEAN' 'STD DEV' 'VARIANCE'  'MINIMUM' 'MAXIMUM'
            'RANGE' 'RATIO' 'COVAR' 'CORREL'};
  PRINT STATS[ROWNAME=ITEMNAME COLNAME=STATNAME
              L="STATISTICS FOR ITEMS AND SCALE TOTAL"];
  FREE STATS MINIMUM MAXIMUM RANGE RATIO;
  COV=cov(X);                            * COVARIANCE MATRIX;
  PRINT COV[ROWNAME=ITEMNAME COLNAME=ITEMNAME L="COVARIANCE MATRIX"];
  ITCOV= COV[NTOTAL,1:NITEM];            * SAVE ITEM-TOTAL COVARIANCES;
  TEMP= J(1,(NITEM*(NITEM-1))/2,0);      * ALL ITEM COVARIANCES;
  M=0;                                   * INTO ONE LONG VECTOR ;
  DO K=2 TO NITEM;
     DO L=1 TO K-1;
        M=M+1;
        TEMP[1,M]= COV[K,L];
     END;
  END;

  RUN DOSTATS;
  TEMP= DIAG(1/STD);           * CORRELATIONS;
  CORR=TEMP*COV*TEMP;
  FREE COV;
  PRINT CORR[ROWNAME=ITEMNAME COLNAME=ITEMNAME L="CORRELATION MATRIX"];
  TEMP= J(1,(NITEM*(NITEM-1))/2,0);
  M=0;
  DO K=2 TO NITEM;
     DO L=1 TO K-1;
        M=M+1;
        TEMP[1,M]= CORR[K,L];
        END;
     END;
  MEANCORR= TEMP[,:];

  RUN DOSTATS;
* SQUARED MULT CORR;
  SMC=1-(1/ VECDIAG( INV( CORR[1:NITEM,1:NITEM])))`;
  FREE CORR;
  STATSTAT= STATSTAT[2: NROW(STATSTAT),];
  PRINT STATSTAT[ROWNAME=STATNAME COLNAME=STATNAME
                 L="STATISTICS FOR ITEM STATISTICS"];
  FREE STATSTAT STATNAME;
  ALPHA=NITEM/(NITEM-1)*  (1- VARIANCE[1,1:NITEM][1,+]/
  VARIANCE[1,NTOTAL]);
  PRINT ALPHA[L="COEFFICIENT ALPHA"];
  ALPHA=(NITEM*MEANCORR)/(1+(NITEM-1)*MEANCORR);
  PRINT ALPHA[L="STANDARDIZED ITEM ALPHA"];

                                * ITEM-DELETED STATISTICS;
  IDMEAN= MEAN[,NTOTAL]- MEAN[,1:NITEM];
  IDVAR=( VARIANCE[,NTOTAL]+ VARIANCE[,1:NITEM])-2*
     ITCOV[,1:NITEM];
  IDSTD= SQRT(IDVAR<>0);
  IDALPHA=(NITEM-1)/(NITEM-2)@  (1-( VARIANCE[,1:NITEM][,+]-
  VARIANCE[,1:NITEM])/IDVAR);
  IDCORR=(ITCOV- VARIANCE[,1:NITEM])/( STD[,1:NITEM]#IDSTD);
  IDSTATS=IDMEAN//IDSTD//IDVAR//IDCORR//SMC//IDALPHA;
  IDSTATS=IDSTATS`;
  STATNAME={'MEAN' 'STD DEV' 'VARIANCE' 'CORREL' 'SMC' 'ALPHA'};
  PRINT IDSTATS[ROWNAME=ITEMNAME COLNAME=STATNAME
                L="SCALE STATISTICS WITH ITEM DELETED"];