options pagesize=400 linesize=122 nodate nonumber center nofmterr; *****************************************************************; * Program Documentation Information ; %LET prg=ptrz03p07.sas ; TITLE1 "SOURCE: &prg on 3/20/03 by Ruitao Zhang" ; * ; * Description: ; * Test program for imputation for creating score variables ; * using data from Little and Rubin (1987) p31 ; * Run macro imputation and get average of scores ; * (based on ptrz03p03.sas) ; * Run manual imputation and get average of scores ; * (based on ptrz03p01.sas) ; * compare the results ; * Input: ; *****************************************************************; ODS HTML CLOSE; ***************************************************************************************; *** BEGIN MACRO IMPUTE ****************************************************************; ***************************************************************************************; %MACRO impute ( /* Description (Macro Var)*/ /* Name of data set */ datain= , /* Subject variable */ subject= , /* # of items in the scale */ nitems= , /* Initial Item Var Names (blanks)*/ items= , /* New Item Var Names (blanks) */ newitems= , /* Var name for new score */ newscale= , /* Output dataset with new score */ dataout1= , /* Output dataset with new items */ dataout2= ); PROC SORT DATA=&datain; BY &subject; DATA &datain; SET &datain; ARRAY orig(&nitems) &items; DO i=1 TO &nitems; IF orig(i) IN (-7, -8) THEN orig(i)=.; END; RUN; PROC TRANSPOSE DATA=&datain OUT=d2 PREFIX=resp; BY &subject; VAR &items; *Note that variable for item number is called _name_; RUN; *************************************************************************; *** fit main effects model and create predicted values ; *************************************************************************; PROC MIXED DATA=d2; CLASS _NAME_ &subject; MODEL resp1=_NAME_ &subject/OUTPREDM=p1; RUN; *************************************************************************; *** substitute imputed values in data set ; *************************************************************************; DATA p2 (KEEP=&subject _NAME_ resp2); SET p1; resp2=resp1; IF resp1=. THEN resp2=pred; RUN; PROC MEANS NOPRINT DATA=p2; BY &subject; VAR resp2; OUTPUT OUT=p2s MEAN=&newscale; DATA &dataout1; SET p2s (KEEP=&subject &newscale); *************************************************************************; *** re-arrange data to create imputed item variables *; *************************************************************************; PROC TRANSPOSE DATA=p2 OUT=p3 PREFIX=new; BY &subject; VAR resp2; RUN; **************************************************; *** Rename imputed items *; **************************************************; DATA &dataout2 (KEEP=&subject &newitems); SET p3; ARRAY new(&nitems); ARRAY imp(&nitems) &newitems; DO i=1 to &nitems; imp{i}=new{i}; END; %mend impute; ***************************************************************************************; *** END MACRO IMPUTE ******************************************************************; ***************************************************************************************; ***************************************************************************; *** Read in data and run macro ; ***************************************************************************; DATA d; INPUT Item subject response; CARDS; 1 1 . 1 2 8 1 3 7.93 2 1 8.14 2 2 8.15 2 3 7.87 3 1 7.76 3 2 . 3 3 7.74 4 1 7.17 4 2 7.57 4 3 7.80 5 1 7.46 5 2 7.68 5 3 7.21 ; PROC PRINT DATA=d noobs; TITLE2 "Table 1. List of data from Little and Rubin (1987, p31)"; PROC SORT DATA=d; BY subject item; PROC TRANSPOSE DATA=d OUT=d1 PREFIX=r; BY subject; ID item; VAR response; PROC PRINT DATA=d1; TITLE2 "Table 2. List of transposed data"; RUN; %impute ( /* Description (Macro Var)*/ /* Name of data set */ datain= d1, /* Subject variable */ subject= subject, /* # of items in the scale */ nitems= 5, /* Initial Item Var Names (blanks)*/ items= r1 r2 r3 r4 r5, /* New Item Var Names (blanks) */ newitems= r1_imp r2_imp r3_imp r4_imp r5_imp, /* Var name for new score */ newscale= score, /* Output dataset with new score */ dataout1= f2, /* Output dataset with new items */ dataout2=f3 ); PROC MEANS DATA=f2; VAR score; TITLE2 "Table 1. Mean of new Imputed Scale based on MACRO"; RUN; PROC PRINT DATA=f2; VAR subject score; TITLE2 "Table 1b. List of scores for subjects"; RUN; ******************************************************************; *** Impute values and create the scores manually *; ******************************************************************; PROC SORT DATA=d1; BY subject; PROC TRANSPOSE DATA=d1 OUT=d2 PREFIX=resp; BY subject; VAR r1 r2 r3 r4 r5; *Note that variable for item number is called _name_; RUN; *************************************************************************; *** fit main effects model and create predicted values ; *************************************************************************; PROC MIXED DATA=d2; CLASS _NAME_ subject; MODEL resp1=_NAME_ subject/OUTPREDM=p1; TITLE2 "Table 2. Results of mixed models"; RUN; *************************************************************************; *** substitute imputed values in data set ; *************************************************************************; DATA p2 (KEEP=subject _NAME_ resp2); SET p1; resp2=resp1; IF resp1=. THEN resp2=pred; RUN; PROC MEANS NOPRINT DATA=p2; BY subject; VAR resp2; OUTPUT OUT=p2s MEAN=score; DATA c2; SET p2s (KEEP=subject score); *************************************************************************; *** re-arrange data to create imputed item variables *; *************************************************************************; PROC TRANSPOSE DATA=p2 OUT=p3 PREFIX=new; BY subject; VAR resp2; RUN; **************************************************; *** Rename imputed items *; **************************************************; DATA c3 (KEEP=subject g1a_imp g1b_imp g1c_imp g1d_imp g1e_imp); SET p3; ARRAY new(5); ARRAY imp(5) g1a_imp g1b_imp g1c_imp g1d_imp g1e_imp; DO i=1 to 5; imp{i}=new{i}; END; PROC MEANS DATA=c2; VAR score; TITLE2 "Table 3. Mean of new Imputed Scale based on manual program"; RUN;