/******************************************************************************/
/*                                                                            */
/*        SAS Program to Score the TIMSS Advanced 2015 Adv Math Items         */
/*                       TIMSS Advanced 2015 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 >= 20 AND &ITEM < 30 THEN SCORE = 2 ; * Full Credit ;
      IF &ITEM >= 10 AND &ITEM < 20 THEN SCORE = 1 ; * Partial Credit ;
      IF &ITEM >= 70 AND &ITEM < 80 THEN SCORE = 0 ; * No Credit ;
      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 MA13011   MA33086   MA33225A  MA33225D  MA33007   MA33182   MA23185
                    MA23064   MA23082   MA33201   MA33202   MA33118A  MA33011   MA33077
                    MA33132   MA33236C  ;
       DO OVER ARIGHT ; %SCOREIT (ARIGHT, "MC", 1, .R, .A, ., .I) ; END ;

       * Score multiple-choice items with B key ;

       ARRAY BRIGHT MA13018   MA13019   MA33225B  MA33225C  MA33225E  MA33076   MA23005
                    MA23176   MA33162   MA33198   MA33137   MA33212   MA33106   MA33126
                    MA33118C  MA33236B  MA33236D  MA33169   ;
       DO OVER BRIGHT ; %SCOREIT (BRIGHT, "MC", 2, .R, .A, ., .I) ; END ;

       * Score multiple-choice items with C key ;

       ARRAY CRIGHT MA13012   MA13015   MA13020   MA33044   MA33171   MA23076   MA23098
                    MA33046   MA23144   MA23045   MA33016   MA33079   MA33150   MA33186
                    MA33027   MA33118B  MA33115   MA33236A  MA33181   ;
       DO OVER CRIGHT ; %SCOREIT (CRIGHT, "MC", 3, .R, .A, ., .I) ; END ;

       * Score multiple-choice items with D key ;

       ARRAY DRIGHT MA13016   MA33140   MA23206   MA33050   MA33163   MA33066   MA23020
                    MA33157   MA33123   MA33075   MA33054   MA33085   ;
       DO OVER DRIGHT ; %SCOREIT (DRIGHT, "MC", 4, .R, .A, ., .I) ; END ;

       * Score multiple-choice items with E key ;

       ARRAY ERIGHT MA13013   MA13017   ;
       DO OVER ERIGHT ; %SCOREIT (DRIGHT, "MC", E, .R, .A, ., .I) ; END ;

       * Score Constructed-Response Items ;

       ARRAY CONSTR MA33225   MA33142   MA33179   MA33214   MA33039   MA33180   MA23145
                    MA23187   MA23201   MA23154   MA23166   MA23043   MA33008   MA33121
                    MA33240   MA33232   MA33178   MA23054   MA23131A  MA23131B  MA23157
                    MA23094   MA33083   MA33143   MA33227   MA33220   MA33233   MA33155
                    MA33042   MA33094   MA33067   MA33012   MA33239   MA33038   MA33091
                    MA33090   MA33118   MA33243   MA33229   MA33159   MA33190   MA33237
                    MA33218   MA33236   MA33002   MA33235   ;
       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> ) ;
