/******************************************************************************/
/*                                                                            */
/*             SAS Program to Score the PIRLS Literacy 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 L11A03M   L11A08M   L11A13MB  L11A13MC  L11A13MD  L21B06M
                    L21B12M   L21C13M   L21E05M   L21E09M   R11F04M   R11F11M
                    L21H03M   L21H04M   L21H07M   L21H10M   L21H13M   L21L08M
                    L21L12M   L21M09M   L21M18M   L11O12M   L11O16M   L11P02M
                    L11U05M   L11U08M   ;
       DO OVER ARIGHT ; %SCOREIT (ARIGHT, "MC", 1, .R, .A, ., .I) ; END ;

       * Score multiple-choice items with B key ;

       ARRAY BRIGHT L11A05M   L11A13ME  L21B10M   L21C03M   L21C08M   L21C15M
                    L21E04M   R11F01M   R11F13M   L21H05M   L21H15M   R21K06M
                    R21K09M   R21K11M   L21L02M   L21M05M   L21M11M   L21M15M
                    L11O04M   L11O07M   L11P08M   L11P10M   L11U02M   L11U13M ;
       DO OVER BRIGHT ; %SCOREIT (BRIGHT, "MC", 2, .R, .A, ., .I) ; END ;

       * Score multiple-choice items with C key ;

       ARRAY CRIGHT L11A01M   L21B03M   L21B05M   L21B11M   L21B14M   L21C06M
                    L21E06M   L21E08M   R11F02M   L21H02M   L21H08M   L21H16M
                    R21K03M   L21L10M   L21L14M   L21M01M   L21M07M   L11O01M
                    L11O08M   L11P01M   L11P09M   L11U07M   L11U09M   ;
       DO OVER CRIGHT ; %SCOREIT (CRIGHT, "MC", 3, .R, .A, ., .I) ; END ;

       * Score multiple-choice items with D key ;

       ARRAY DRIGHT L11A14M   L21B02M   L21C02M   L21C10M   L21E03M   L21E11M
                    L21E17M   R11F03M   R11F05M   L21H09M   R21K04M   R21K08M
                    L21L01M   L21L07M   L21M02M   L21M13M   L11O03M   L11O13M
                    L11P05M   L11U03M   L11U06M   ;
       DO OVER DRIGHT ; %SCOREIT (DRIGHT, "MC", 4, .R, .A, ., .I) ; END ;

       * Score Constructed-Response Items ;

       ARRAY CONSTR L11A02C   L11A04C   L11A06C   L11A07C   L11A09C   L11A10C
                    L11A11C   L11A12CA  L11A12CB  L11A12CC  L11A12CZ  L11A13CZ
                    L21B01C   L21B04C   L21B07C   L21B08C   L21B09C   L21B13C
                    L21B15C   L21B16C   L21B17C   L21C01C   L21C04C   L21C05C
                    L21C07C   L21C09C   L21C11C   L21C12C   L21C14C   L21C16C
                    L21C17C   L21E01C   L21E02C   L21E07C   L21E10C   L21E12C
                    L21E13C   L21E14C   L21E15C   L21E16C   R11F06C   R11F07C
                    R11F08C   R11F09C   R11F10C   R11F12C   L21H01C   L21H06C
                    L21H11C   L21H12C   L21H14C   R21K01C   R21K02C   R21K05C
                    R21K07C   R21K10C   R21K12C   L21L03C   L21L04C   L21L05C
                    L21L06C   L21L09C   L21L11C   L21L13C   L21L15C   L21M03C
                    L21M04C   L21M06C   L21M08C   L21M10C   L21M12C   L21M14C
                    L21M16C   L21M17C   L11O02C   L11O05C   L11O06C   L11O09C
                    L11O10C   L11O11C   L11O14C   L11O15C   L11P03C   L11P04C
                    L11P06C   L11P07C   L11P11C   L11P12C   L11P13C   L11P14C
                    L11U01C   L11U04C   L11U10C   L11U11C   L11U12C   L11U14C ;
       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> ) ;
