/******************************************************************************/
/*                                                                            */
/*       SAS Program to Join SAS Data Files into a Single SAS Data File       */
/*                         Specific for 1995 Database                         */
/*                       TIMSS Advanced 2008 User Guide                       */
/*                                                                            */
/******************************************************************************/

OPTIONS NONOTES ;

* Specify the file type ;
* <type> must be edited ;
* MCG/PCG - MSA/PSA - MSC/PSC - MSG/PSG - MSR/PSR ;

%LET TYPE = <type> ;

* Specify the path for the SAS data files following the format x:\xxx\ ;
* <path> must be edited ;

LIBNAME LIBDAT "<path>" ;

%MACRO DOIT ;

   * Specify the list of countries to be processed ;

   %IF %UPCASE(%SUBSTR(&TYPE,1,1)) = M %THEN %DO ; * 4th grade countries ;

      %LET COUNTRY = AUS AUT CAN CYP CZE FRA DEU DNK GRC ISR
                     ITA LTU RUS SVN SWE CHE USA ;

   %END ;

   %IF %UPCASE(%SUBSTR(&TYPE,1,1)) = P %THEN %DO ; * 8th grade countries ;

      %LET COUNTRY = AUS AUT CAN CYP CZE FRA DEU DNK GRC ISR
                     LVA NOR RUS SVN SWE CHE USA ;

   %END;

   DATA &TYPE.ALLM1 ;
        SET %LET I = 1 ;
            %DO %WHILE(%LENGTH(%SCAN(&COUNTRY,&I))) ;
               %LET CTRY = %SCAN(&COUNTRY,&I) ;
                  LIBDAT.&TYPE&CTRY.M1
               %LET I = %EVAL(&I + 1) ;
            %END ; ;
   RUN ;

   * Specify the sorting variables ;
   * Dependent of the file type ;

   %LET SORTVARS = IDCNTRY IDSTUD ;

   %IF %UPCASE(%SUBSTR(&TYPE,2,1)) = C %THEN %DO ;
      %LET SORTVARS = IDCNTRY IDSCHOOL ; %END ;

   %IF %UPCASE(%SUBSTR(&TYPE,2,1)) = T %THEN %DO ;
      %LET SORTVARS = IDCNTRY IDTEACH IDLINK ; %END ;

   %IF %UPCASE(%SUBSTR(&TYPE,3,1)) = T %THEN %DO ;
      %LET SORTVARS = IDCNTRY IDSTUD IDTEACH IDLINK ; %END ;

   PROC SORT DATA=&TYPE.ALLM1 OUT=LIBDAT.&TYPE.ALLM1 ;
        BY &SORTVARS ;
   RUN ;

%MEND DOIT ;

%DOIT ;
