/******************************************************************************/
/*                                                                            */
/*                 SAS Program to Score the PIRLS 2016 Items                  */
/*                           PIRLS 2016 User Guide                            */
/*                                                                            */
/******************************************************************************/

%MACRO SCOREIT (ITEM, TYPE, RIGHT, NR, NA, OM, OTHER) ;

  * Code for multiple-choice items ;

  %IF %UPCASE(&TYPE) = "MC" %THEN %DO ;
    SCORE = 0 ;
      IF &ITEM = &RIGHT THEN SCORE = 1 ; * Item Key         ;
      IF &ITEM = &NR    THEN SCORE = 0 ; * Not Reached      ;
      IF &ITEM = &NA    THEN SCORE = . ; * Not Administered ;
      IF &ITEM = &OM    THEN SCORE = 0 ; * Omitted          ;
      IF &ITEM = &OTHER THEN SCORE = 0 ; * Other Missing    ;
    &ITEM = SCORE ;
  %END ;

  * Code for constructed-response items ;

  %IF %UPCASE(&TYPE) = "CR" %THEN %DO ;
    SCORE = 0 ;
      IF &ITEM = 3      THEN SCORE = 3 ; * 3 Points         ;
      IF &ITEM = 2      THEN SCORE = 2 ; * 2 Points         ;
      IF &ITEM = 1      THEN SCORE = 1 ; * 1 Point          ;
      IF &ITEM = 0      THEN SCORE = 0 ; * 0 Points         ;
      IF &ITEM = &NR    THEN SCORE = 0 ; * Not Reached      ;
      IF &ITEM = &NA    THEN SCORE = . ; * Not Administered ;
      IF &ITEM = &OM    THEN SCORE = 0 ; * Omitted          ;
      IF &ITEM = &OTHER THEN SCORE = 0 ; * Other Missing    ;
    &ITEM = SCORE ;
  %END ;

%MEND SCOREIT ;

%MACRO DOIT (INDIR  = ,
             OUTDIR = ,
             INFILE = ) ;

  LIBNAME INDIR  "&INDIR" ;
  LIBNAME OUTDIR "&OUTDIR" ;

  OPTIONS NONOTES FMTSEARCH=(INDIR,OUTDIR) MPRINT ;

  * Get the student achievement data ;

  DATA OUTDIR.&INFILE._SCR; * Output File ;
       SET INDIR.&INFILE;   * Input File ;

       * Score multiple-choice items with A key ;

       ARRAY ARIGHT L21B06M   L21B12M   L21E05M   L21E09M   R11F04M   R11F11M
                    R41H05M   R41H12M   R41I06M   R41I08M   R11L05M   R31M03M
                    R31M05M   R31M11M   R41O11M   R31W09M   R31W12M   R21Y06M ;
       DO OVER ARIGHT ; %SCOREIT (ARIGHT, "MC", 1, .R, .A, ., .I) ; END ;

       * Score multiple-choice items with B key ;

       ARRAY BRIGHT L21B10M   L21E04M   R11F01M   R11F13M   R41H01M   R41H10M
                    R21K06M   R21K09M   R21K11M   R11L02M   R31M01M   R31M07M
                    R31M15M   R41O06M   R41T05M   R41T15M   R41T16M   R31W08M
                    R21Y01M   R21Y04M   R21Y05M   R21Y07M   R21Y11M   ;
       DO OVER BRIGHT ; %SCOREIT (BRIGHT, "MC", 2, .R, .A, ., .I) ; END ;

       * Score multiple-choice items with C key ;

       ARRAY CRIGHT L21B03M   L21B05M   L21B11M   L21B14M   L21E06M   L21E08M
                    R11F02M   R41H07M   R41H11M   R41I05M   R41I10M   R21K03M
                    R11L01M   R11L07M   R11L09M   R31M06M   R31M12M   R31M13M
                    R41O01M   R41T09M   R41T12M   R31W03M   R31W05M   R31W06M
                    R21Y08M   ;
       DO OVER CRIGHT ; %SCOREIT (CRIGHT, "MC", 3, .R, .A, ., .I) ; END ;

       * Score multiple-choice items with D key ;

       ARRAY DRIGHT L21B02M   L21E03M   L21E11M   L21E17M   R11F03M   R11F05M
                    R41H02M   R41H09M   R41I02M   R41I12M   R21K04M   R21K08M
                    R11L11M   R31M08M   R31M14M   R41O12M   R41T01M   R41T13M
                    R31W10M   R21Y02M   ;
       DO OVER DRIGHT ; %SCOREIT (DRIGHT, "MC", 4, .R, .A, ., .I) ; END ;

       * Score Constructed-Response Items ;

       ARRAY CONSTR L21B01C   L21B04C   L21B07C   L21B08C   L21B09C   L21B13C
                    L21B15C   L21B16C   L21B17C   L21E01C   L21E02C   L21E07C
                    L21E10C   L21E12C   L21E13C   L21E14C   L21E15C   L21E16C
                    R11F06C   R11F07C   R11F08C   R11F09C   R11F10C   R11F12C
                    R41H03C   R41H04C   R41H06C   R41H08C   R41H13C   R41H14C
                    R41H15C   R41H16C   R41I01C   R41I03C   R41I04C   R41I07C
                    R41I09C   R41I11C   R41I13C   R41I14C   R41I15C   R21K01C
                    R21K02C   R21K05C   R21K07C   R21K10C   R21K12C   R11L03C
                    R11L04C   R11L06C   R11L08C   R11L10C   R11L12C   R31M02C
                    R31M04C   R31M09C   R31M10C   R31M16C   R31M17CA  R31M17CB
                    R31M17CC  R31M17CZ  R41O02C   R41O03C   R41O04C   R41O05C
                    R41O07C   R41O08C   R41O09C   R41O10C   R41O13C   R41T02C
                    R41T03C   R41T04C   R41T06C   R41T07C   R41T08C   R41T10C
                    R41T11C   R41T14C   R31W01C   R31W02C   R31W04C   R31W07CA
                    R31W07CB  R31W07CC  R31W07CZ  R31W11C   R31W13C   R21Y03C
                    R21Y09C   R21Y10C   R21Y12C   R21Y13C   R21Y14C   ;
       DO OVER CONSTR ; %SCOREIT (CONSTR, "CR",  , .R, .A, ., .I) ; END ;

  RUN ;

%MEND DOIT ;

%DOIT (INDIR  = <Specify location of input SAS data file using format "x:\xxx"> ,
       OUTDIR = <Specify location of output SAS data file using format "x:\xxx"> ,
       INFILE = <Specify name of merged SAS student achievement data file> ) ;
