/******************************************************************************/
/*                                                                            */
/*        SAS Program to Score the TIMSS Advanced 2015 Physics 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 PA33115   PA23038   PA33078   PA33088   PA33116   PA23039   PA23042
                    PA33083   PA33002   PA33095B  PA33066A  PA33109   PA33111B  ;
       DO OVER ARIGHT ; %SCOREIT (ARIGHT, "MC", 1, .R, .A, ., .I) ; END ;

       * Score multiple-choice items with B key ;

       ARRAY BRIGHT PA13011   PA13014   PA13019   PA33102AA PA33121   PA23071   PA23148
                    PA33120   PA23064   PA33086   PA33114   PA33065   PA33040   PA33110
                    PA33063   ;
       DO OVER BRIGHT ; %SCOREIT (BRIGHT, "MC", 2, .R, .A, ., .I) ; END ;

       * Score multiple-choice items with C key ;

       ARRAY CRIGHT PA13012   PA13013   PA13015   PA13018   PA33102AB PA33101A  PA23104
                    PA33057   PA33012   PA23048   PA23085   PA33059   PA33015   PA33028B
                    PA33054A  PA33066B  PA33099   PA33003   PA33094B  ;
       DO OVER CRIGHT ; %SCOREIT (CRIGHT, "MC", 3, .R, .A, ., .I) ; END ;

       * Score multiple-choice items with D key ;

       ARRAY DRIGHT PA13016   PA13017   PA33061A  PA33044   PA23029   PA33079A  PA33019
                    PA33009B  PA33098   PA33118   PA33029   PA33081   PA33045   ;
       DO OVER DRIGHT ; %SCOREIT (DRIGHT, "MC", 4, .R, .A, ., .I) ; END ;

       * Score multiple-choice items with E key ;

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

       * Score Constructed-Response Items ;

       ARRAY CONSTR PA33061B  PA33004   PA33075   PA33102A  PA33102B  PA33005   PA33101B
                    PA23146   PA23041   PA23053   PA23119   PA23088   PA23066   PA33058
                    PA33047   PA33079B  PA33070   PA33011   PA23035   PA23012   PA23051
                    PA23130   PA23086   PA33073   PA33035   PA33119   PA33046   PA33069
                    PA33080   PA33009A  PA33028A  PA33054B  PA33095A  PA33090   PA33064A
                    PA33064B  PA33097   PA33008   PA33008A  PA33008B  PA33008C  PA33072
                    PA33077   PA33111A  PA33094A  PA33094C  ;
       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> ) ;
