/*----------------------------------------------------------------- Imputation SAS code for Exemplar 6 detailed questions This program uses the IVEWARE macros that can be installed from http://www.isr.umich.edu/src/smp/ive/ ALSO to run the IVEWARE software you need to close all advanced editor windows and use only the old program editor CHANGE the libname to shere your data sets are stored proc contents data=ex6.ex6det short position;run; ---------------------------------------------------------------------------------*/libname ex6 'C:\Documents and Settings\gillian raab\My Documents\aprojects\peaslaptop\ex6datafiles\data' ; /*------------------------------------------------------------------ Now the analysis of detailed questions using the IVEWARE macros. File ex6.ex6det has detailed questions (102) and gender details of what the commands mean are in the user guide on the IVE site. Here all variables are treated as counts and the upper limit of the count is set to 7. --------------------------------------------------------------------*/ title 'Imputation of detailed questions with IVEWARE'; libname ex6 'C:\Documents and Settings\gillian raab\My Documents\aprojects\peaslaptop\ex6datafiles\data' ; %impute(name=ivesetupdet, dir=C:\Documents and Settings\gillian raab\My Documents\aprojects\peaslaptop\ex6datafiles\program_code ,setup=new); title Multiple imputation prevalence only; datain ex6.ex6det; dataout impout all ; default count; bounds YAYARS02(<=7) YAYBOP02(<=7)YAYBUS02(<=7) YAYCBK02(<=7) YAYGRF02(<=7) YAYHBK02(<=7) YAYHIT02(<=7) YAYHOM02(<=7) YAYJRD02(<=7) YAYROB02(<=7) YAYSCL02(<=7) YAYSHP02(<=7) YAYSKV02(<=7) YAYVND02(<=7) YAYWEP02(<=7) YBYARS02(<=7) YBYBOP02(<=7) YBYBUS02(<=7) YBYCBK02(<=7) YBYGRF02(<=7) YBYHBK02(<=7) YBYHIT02(<=7) YBYHOM02(<=7) YBYJRD02(<=7) YBYPET02(<=7) YBYROB02(<=7) YBYSCL02(<=7) YBYSHP02(<=7) YBYSKV02(<=7) YBYVND02(<=7) YBYWEP02(<=7) YCYARS02(<=7) YCYBOP02(<=7) YCYBUS02(<=7) YCYCBK02(<=7) YCYDRG02(<=7) YCYGRF02(<=7) YCYHBK02(<=7) YCYHIT02(<=7) YCYHOM02(<=7) YCYJRD02(<=7) YCYPET02(<=7) YCYRAB02(<=7) YCYROB02(<=7) YCYSCL02(<=7) YCYSHP02(<=7) YCYSKV02(<=7) YCYVND02(<=7) YCYWEP02(<=7) YDYARS02(<=7) YDYBOP02(<=7) YDYBUS02(<=7) YDYCBK02(<=7) YDYDRG02(<=7) YDYGRF02(<=7) YDYHBK02(<=7) YDYHIT02(<=7) YDYHOM02(<=7) YDYJRD02(<=7) YDYPET02(<=7) YDYRAB02(<=7) YDYROB02(<=7) YDYSCL02(<=7) YDYSHP02(<=7) YDYSKV02(<=7) YDYVND02(<=7) YDYWEP02(<=7) YEYARS02(<=7) YEYBOP02(<=7) YEYBUS02(<=7) YEYCBK02(<=7) YEYDRG02(<=7) YEYGRF02(<=7) YEYHBK02(<=7) YEYHIT02(<=7) YEYJRD02(<=7) YEYPET02(<=7) YEYRAB02(<=7) YEYROB02(<=7) YEYRST02(<=7) YEYSHP02(<=7) YEYSKV02(<=7) YEYVND02(<=7) YEYWEP02(<=7) YFYARS02(<=7) YFYBFT02(<=7) YFYBOP02(<=7) YFYCBK02(<=7) YFYDRG02(<=7) YFYFRD02(<=7) YFYHBK02(<=7) YFYHIT02(<=7) YFYJRD02(<=7) YFYPET02(<=7) YFYRAB02(<=7) YFYROB02(<=7) YFYRST02(<=7) YFYRST22(<=7) YFYSHP02(<=7) YFYSKV02(<=7) YFYVND02(<=7) YFYWEP02(<=7); iterations 5; multiples 10; transfer caseid ; run ; %include "C:\Documents and Settings\gillian raab\My Documents\aprojects\peaslaptop\ex6datafiles\program_code\checkimp_macro.sas"; %checkimp(ex6.detailimp,ex6.ex6det); /*---------------------------- save it after all that trouble--------------------*/ data ex6.detailimp; set impout; run; /*---------------------------- now calculate prevalence etc from the imputed data----------*/ data ex6.ivedet (keep=dprev: dvol: dvar: caseid _mult_); set ex6.detailimp; /*---------------------------- sweep 1---------------------------------------*/ *Variety of delinquency at sweep one (n=15); array offv1 yaybus02 yaybop02 yayshp02 yayjrd02 yayscl02 yaywep02 yaygrf02 yayrob02 yayvnd02 yayhbk02 yayhom02 yaycbk02 yayars02 yayhit02 yayskv02; dprev1=0; dvar1=0; dvol1=0; do i=1 to 15; if offv1[i]>0 then do dprev1=1; dvar1=dvar1+1; end; if offv1[i]=7 then offv1[i]=11; * code to match SPSS; dvol1=offv1[i]+dvol1; ; end; /*---------------------------- sweep 2---------------------------------------*/ *Variety of delinquency at sweep two (n=16); array offv2 ybybus02 ybybop02 ybyshp02 ybyjrd02 ybyscl02 ybywep02 ybygrf02 ybyrob02 ybyvnd02 ybyhbk02 ybyhom02 ybycbk02 ybyars02 ybyhit02 ybyskv02 ybypet02 ; dprev2=0; dvar2=0; dvol2=0; do i=1 to 16; if offv2[i]>0 then do dprev2=1; dvar2=dvar2+1; end; if offv2[i]=7 then offv2[i]=11; * code to match SPSS; dvol2=offv2[i]+dvol2; end; /*---------------------------- sweep 3---------------------------------------*/ *Variety of delinquency at sweep three (n=18); array offv3 ycybus02 ycybop02 ycyshp02 ycyjrd02 ycyscl02 ycywep02 ycygrf02 ycyrob02 ycyvnd02 ycyhbk02 ycyhom02 ycycbk02 ycyars02 ycyhit02 ycyskv02 ycypet02 ycydrg02 ycyrab02 ; dprev3=0; dvar3=0; dvol3=0; do i=1 to 18; if offv3[i]>0 then do dprev3=1; dvar3=dvar3+1; end; if offv3[i]=7 then offv3[i]=11; * code to match SPSS; dvol3=offv3[i]+dvol3; end; /*---------------------------- sweep 4---------------------------------------*/ *Variety of delinquency at sweep three (n=18); array offv4 ydybus02 ydybop02 ydyshp02 ydyjrd02 ydyscl02 ydywep02 ydygrf02 ydyrob02 ydyvnd02 ydyhbk02 ydyhom02 ydycbk02 ydyars02 ydyhit02 ydyskv02 ydypet02 ydydrg02 ydyrab02; dprev4=0; dvar4=0; dvol4=0; do i=1 to 18; if offv4[i]>0 then do dprev4=1; dvar4=dvar4+1; end; if offv4[i]=7 then offv4[i]=11; * code to match SPSS; dvol4=offv4[i]+dvol4; end; /*---------------------------- sweep 5---------------------------------------*/ *Variety of delinquency at sweep five (n=17); array offv5 yeybus02 yeybop02 yeyshp02 yeyjrd02 yeywep02 yeygrf02 yeyrob02 yeyvnd02 yeyhbk02 yeycbk02 yeyars02 yeyhit02 yeyskv02 yeypet02 yeydrg02 yeyrab02 yeyrst02; dprev5=0; dvar5=0; dvol5=0; do i=1 to 17; if offv5[i]>0 then do dprev5=1; dvar5=dvar5+1; end; if offv5[i]=7 then offv5[i]=11; * code to match SPSS; dvol5=offv5[i]+dvol5; end; /*---------------------------- sweep 6---------------------------------------*/ *Variety of delinquency at sweep six (n=18); array offv6 yfybop02 yfyshp02 yfyjrd02 yfywep02 yfyrob02 yfyvnd02 yfyhbk02 yfycbk02 yfyars02 yfyhit02 yfypet02 yfyskv02 yfydrg02 yfyrab02 yfyrst02 yfyrst22 yfybft02 yfyfrd02; dprev6=0; dvar6=0; dvol6=0; do i=1 to 18; if offv6[i]>0 then do dprev6=1; dvar6=dvar6+1; end; if offv6[i]=7 then offv6[i]=11; * code to match SPSS; dvol6=offv6[i]+dvol6; end; run; /*----now merge this with some variables from the original file-----*/ /*----------- must run ex6formats.sas before this--------------------*/ data temp1; set ex6.ex6; keep caseid sector szindep gender; run; proc sort data=ex6.ivedet; by caseid;run; proc sort data=temp1; by caseid;run; data ex6.ivedet; merge ex6.ivedet temp1; by caseid; rename _Mult_ =_imputation_; * to use proc MIanalyze; run; /*----------------------------------------------------------------------------------- POST IMPUTATION PROCEDURES The imputation variable needs to be called _imputation_ renamed above These are always 2 stage procedures -------------------------------------------------------------------------------------*/ proc sort data=ex6.IVEdet; by _imputation_; run; /*---------------------------- get means using corr--------------------------------*/ PROC CORR DATA=ex6.IVEdet COV OUT=OUTCOV(TYPE=COV) NOCORR noprint ; title 'IVE WARE BOYS'; where gender=1; VAR dprev1-dprev6 dvol1-dvol6 dvar1-dvar6 ; BY _IMPUTATION_; run; PROC MIANALYZE data=outcov ; VAR dprev1-dprev6 dvol1-dvol6 dvar1-dvar6 ; RUN; PROC CORR DATA=ex6.IVEdet COV OUT=OUTCOV(TYPE=COV) NOCORR noprint ; title 'IVE WARE girls'; where gender=2; VAR dprev1-dprev6 dvol1-dvol6 dvar1-dvar6 ; BY _IMPUTATION_; run; PROC MIANALYZE data=outcov ; VAR dprev1-dprev6 dvol1-dvol6 dvar1-dvar6 ; RUN; /*------------------- logistic regression and MIANALYZE---------------------------*/ proc logistic data=ex6.IVEsc outest=outreg covout descending noprint; by _imputation_; class gender szindep sector/param=ref; model dprev6= gender szindep sector ; run; proc contents data= outreg short;run;* use this to find names of contrasts; PROC MIANALYZE data=outreg edf=4325 ; * these are the residual degrees of freedom from the model; var GENDERFemale SZINDEPManual_high_depr sectorBehavioural sectorIndependent sectorSpecial ; run; /*--We can compare with the complete data analysis here---------*/ proc logistic data=ex6.ex6 descending ; class gender szindep sector; model dprev6= gender szindep sector ; run; /*---------------- now a check to compare prevalence by deprivation with original data---*/ proc means data=ex6.IVEdet; class szindep; var dprev6; run; proc means data=ex6.ex6; class szindep; var dprev6; run; /*------------------------------------------------------------ Now analyses with proc MI that assume all variables are multivariate normal. Upper bounds are set on all variables to prevent high values But lower bounds are not set because experience showed that this resulted in a downward bias that gave too few responses in the zero category Needs to be run in two halves as problem seems to be too big otherwise ---------------------------------------------------------------*/ proc contents short data=ex6.ex6detail; * this just to get list of variables; run; data temp1; set ex6.ex6detail; keep yaybus02 ybybus02 ycybus02 ydybus02 yeybus02 yayskv02 ybyskv02 ycyskv02 ydyskv02 yeyskv02 yfyskv02 yeyrst02 yfyrst02 yfyrst22 yfybft02 yfyfrd02 yayshp02 ybyshp02 ycyshp02 ydyshp02 yeyshp02 yfyshp02 yayscl02 ybyscl02 ycyscl02 ydyscl02 yayhom02 ybyhom02 ycyhom02 ydyhom02 yaycbk02 ybycbk02 ycycbk02 ydycbk02 yeycbk02 yfycbk02 yayjrd02 ybyjrd02 ycyjrd02 ydyjrd02 yeyjrd02 yfyjrd02 yayrob02 ybyrob02; run; /*------------------------------------------------ now the imputation including rounding to the nearest whole -------------------------------------------------------*/ proc mi data=temp1 OUT=imp1 nimpute=10 round(1); VAR yaybus02 ybybus02 ycybus02 ydybus02 yeybus02 yayskv02 ybyskv02 ycyskv02 ydyskv02 yeyskv02 yfyskv02 yeyrst02 yfyrst02 yfyrst22 yfybft02 yfyfrd02 yayshp02 ybyshp02 ycyshp02 ydyshp02 yeyshp02 yfyshp02 yayscl02 ybyscl02 ycyscl02 ydyscl02 yayhom02 ybyhom02 ycyhom02 ydyhom02 yaycbk02 ybycbk02 ycycbk02 ydycbk02 yeycbk02 yfycbk02 yayjrd02 ybyjrd02 ycyjrd02 ydyjrd02 yeyjrd02 yfyjrd02 yayrob02 ybyrob02 gender; RUN; /*---------- run this to check for extreme values--------*/ proc means data=imp1; run; /*---- now pull values <0 or >7 into range------------*/ data imp1; set imp1; array vars caseid yaybus02 ybybus02 ycybus02 ydybus02 yeybus02 yayskv02 ybyskv02 ycyskv02 ydyskv02 yeyskv02 yfyskv02 yeyrst02 yfyrst02 yfyrst22 yfybft02 yfyfrd02 yayshp02 ybyshp02 ycyshp02 ydyshp02 yeyshp02 yfyshp02 yayscl02 ybyscl02 ycyscl02 ydyscl02 yayhom02 ybyhom02 ycyhom02 ydyhom02 yaycbk02 ybycbk02 ycycbk02 ydycbk02 yeycbk02 yfycbk02 yayjrd02 ybyjrd02 ycyjrd02 ydyjrd02 yeyjrd02 yfyjrd02 yayrob02 ybyrob02 ; do over vars; if vars<0 then vars=0; if vars>7 then vars=7; end; run; /*-------------------- now repeated for second lot of variables-------------*/ data temp2; set ex6.ex6detail; keep caseid yaywep02 ybywep02 ycywep02 ydywep02 yeywep02 yfywep02 ycydrg02 ydydrg02 yeydrg02 yfydrg02 yayhit02 ybyhit02 ycyhit02 ydyhit02 yeyhit02 yfyhit02 yayvnd02 ybyvnd02 ycyvnd02 ydyvnd02 yeyvnd02 yfyvnd02 yaygrf02 ybygrf02 ycygrf02 ydygrf02 yeygrf02 gender yayars02 ybyars02 ycyars02 ydyars02 yeyars02 yfyars02 ycyrab02 ydyrab02 yeyrab02 yfyrab02 yayhbk02 ybyhbk02 ycyhbk02 ydyhbk02 yeyhbk02 yfyhbk02 ycyrob02 ydyrob02 yeyrob02 yfyrob02 ybypet02 ycypet02 ydypet02 yeypet02 yfypet02 yaybop02 ybybop02 ycybop02 ydybop02 yeybop02 yfybop02 gender; run; proc mi data=temp2 OUT=imp2 nimpute=10 round=1; title 'Imputatuion with PROC MI'; VAR yaywep02 ybywep02 ycywep02 ydywep02 yeywep02 yfywep02 ycydrg02 ydydrg02 yeydrg02 yfydrg02 yayhit02 ybyhit02 ycyhit02 ydyhit02 yeyhit02 yfyhit02 yayvnd02 ybyvnd02 ycyvnd02 ydyvnd02 yeyvnd02 yfyvnd02 yaygrf02 ybygrf02 ycygrf02 ydygrf02 yeygrf02 gender yayars02 ybyars02 ycyars02 ydyars02 yeyars02 yfyars02 ycyrab02 ydyrab02 yeyrab02 yfyrab02 yayhbk02 ybyhbk02 ycyhbk02 ydyhbk02 yeyhbk02 yfyhbk02 ycyrob02 ydyrob02 yeyrob02 yfyrob02 ybypet02 ycypet02 ydypet02 yeypet02 yfypet02 yaybop02 ybybop02 ycybop02 ydybop02 yeybop02 yfybop02 gender ; MCMC timeplot(mean(yfybop02)) timeplot(mean(ydyhbk02)) timeplot(cov(yeyhbk02*yfyhbk02)); RUN; proc means data=imp2; run; /*---- now pull values <0 or >7 into range------------*/ data imp2; set imp2; array vars yaywep02 ybywep02 ycywep02 ydywep02 yeywep02 yfywep02 ycydrg02 ydydrg02 yeydrg02 yfydrg02 yayhit02 ybyhit02 ycyhit02 ydyhit02 yeyhit02 yfyhit02 yayvnd02 ybyvnd02 ycyvnd02 ydyvnd02 yeyvnd02 yfyvnd02 yaygrf02 ybygrf02 ycygrf02 ydygrf02 yeygrf02 gender yayars02 ybyars02 ycyars02 ydyars02 yeyars02 yfyars02 ycyrab02 ydyrab02 yeyrab02 yfyrab02 yayhbk02 ybyhbk02 ycyhbk02 ydyhbk02 yeyhbk02 yfyhbk02 ycyrob02 ydyrob02 yeyrob02 yfyrob02 ybypet02 ycypet02 ydypet02 yeypet02 yfypet02 yaybop02 ybybop02 ycybop02 ydybop02 yeybop02 yfybop02; do over vars; if vars<0 then vars=0; if vars>7 then vars=7; end; run; proc freq data=imp2; * checking this one since it had some very high imputed values; table _imputation_ *yeybop02/nopercent nocol; run; proc freq data=ex6.ex6detail; table yeybop02; run; /*------- now merge two imputed data sets togther--------*/ data ex6.detailimpmi; merge imp1 imp2; run; /*-------------- include the macro code-----------------------*/ %include "C:\Documents and Settings\gillian raab\My Documents\aprojects\peaslaptop\ex6datafiles\program_code\checkimp_macro.sas"; %checkimp(ex6.detailimpmi,ex6.ex6det); /*---------------------------- now calculate prevalence etc from the imputed data----------*/ data ex6.NORMdet (keep=dprev: dvol: dvar: caseid gender _imputation_); set ex6.detailimpmi; /*---------------------------- sweep 1---------------------------------------*/ *Variety of delinquency at sweep one (n=15); array offv1 yaybus02 yaybop02 yayshp02 yayjrd02 yayscl02 yaywep02 yaygrf02 yayrob02 yayvnd02 yayhbk02 yayhom02 yaycbk02 yayars02 yayhit02 yayskv02; dprev1=0; dvar1=0; dvol1=0; do i=1 to 15; if offv1[i]>0 then do dprev1=1; dvar1=dvar1+1; end; if offv1[i]=7 then offv1[i]=11; * code to match SPSS; dvol1=offv1[i]+dvol1; ; end; /*---------------------------- sweep 2---------------------------------------*/ *Variety of delinquency at sweep two (n=16); array offv2 ybybus02 ybybop02 ybyshp02 ybyjrd02 ybyscl02 ybywep02 ybygrf02 ybyrob02 ybyvnd02 ybyhbk02 ybyhom02 ybycbk02 ybyars02 ybyhit02 ybyskv02 ybypet02 ; dprev2=0; dvar2=0; dvol2=0; do i=1 to 16; if offv2[i]>0 then do dprev2=1; dvar2=dvar2+1; end; if offv2[i]=7 then offv2[i]=11; * code to match SPSS; dvol2=offv2[i]+dvol2; end; /*---------------------------- sweep 3---------------------------------------*/ *Variety of delinquency at sweep three (n=18); array offv3 ycybus02 ycybop02 ycyshp02 ycyjrd02 ycyscl02 ycywep02 ycygrf02 ycyrob02 ycyvnd02 ycyhbk02 ycyhom02 ycycbk02 ycyars02 ycyhit02 ycyskv02 ycypet02 ycydrg02 ycyrab02 ; dprev3=0; dvar3=0; dvol3=0; do i=1 to 18; if offv3[i]>0 then do dprev3=1; dvar3=dvar3+1; end; if offv3[i]=7 then offv3[i]=11; * code to match SPSS; dvol3=offv3[i]+dvol3; end; /*---------------------------- sweep 4---------------------------------------*/ *Variety of delinquency at sweep three (n=18); array offv4 ydybus02 ydybop02 ydyshp02 ydyjrd02 ydyscl02 ydywep02 ydygrf02 ydyrob02 ydyvnd02 ydyhbk02 ydyhom02 ydycbk02 ydyars02 ydyhit02 ydyskv02 ydypet02 ydydrg02 ydyrab02; dprev4=0; dvar4=0; dvol4=0; do i=1 to 18; if offv4[i]>0 then do dprev4=1; dvar4=dvar4+1; end; if offv4[i]=7 then offv4[i]=11; * code to match SPSS; dvol4=offv4[i]+dvol4; end; /*---------------------------- sweep 5---------------------------------------*/ *Variety of delinquency at sweep five (n=17); array offv5 yeybus02 yeybop02 yeyshp02 yeyjrd02 yeywep02 yeygrf02 yeyrob02 yeyvnd02 yeyhbk02 yeycbk02 yeyars02 yeyhit02 yeyskv02 yeypet02 yeydrg02 yeyrab02 yeyrst02; dprev5=0; dvar5=0; dvol5=0; do i=1 to 17; if offv5[i]>0 then do dprev5=1; dvar5=dvar5+1; end; if offv5[i]=7 then offv5[i]=11; * code to match SPSS; dvol5=offv5[i]+dvol5; end; /*---------------------------- sweep 6---------------------------------------*/ *Variety of delinquency at sweep six (n=18); array offv6 yfybop02 yfyshp02 yfyjrd02 yfywep02 yfyrob02 yfyvnd02 yfyhbk02 yfycbk02 yfyars02 yfyhit02 yfypet02 yfyskv02 yfydrg02 yfyrab02 yfyrst02 yfyrst22 yfybft02 yfyfrd02; dprev6=0; dvar6=0; dvol6=0; do i=1 to 18; if offv6[i]>0 then do dprev6=1; dvar6=dvar6+1; end; if offv6[i]=7 then offv6[i]=11; * code to match SPSS; dvol6=offv6[i]+dvol6; end; run; /*----now merge this with some variables from the original file-----*/ data temp1; set ex6.ex6; keep caseid sector szindep; run; proc sort data=ex6.NORMdet ; by caseid;run; proc sort data=temp1; by caseid;run; data ex6.NORMdet; merge ex6.NORMdet temp1; by caseid; run; /*----------------------------------------------------------------------------------- POST IMPUTATION PROCEDURES The imputation variable needs to be called _imputation_ renamed above These are always 2 stage procedures -------------------------------------------------------------------------------------*/ proc sort data=ex6.NORMdet; by _imputation_; run; /*---------------------------- get means using corr--------------------------------*/ PROC CORR DATA=ex6.NORMdet COV OUT=OUTCOV(TYPE=COV) NOCORR noprint ; VAR dprev1-dprev6 dvol1-dvol6 dvar1-dvar6 ; where gender=1; * boys only; BY _IMPUTATION_; run; PROC MIANALYZE data=outcov ; title 'MI detailed imputation boys'; VAR dprev1-dprev6 dvol1-dvol6 dvar1-dvar6 ; RUN; PROC CORR DATA=ex6.NORMdet COV OUT=OUTCOV(TYPE=COV) NOCORR noprint ; VAR dprev1-dprev6 dvol1-dvol6 dvar1-dvar6 ; where gender=2; * girls only; BY _IMPUTATION_; run; PROC MIANALYZE data=outcov ; title 'MI detailed imputation girls'; VAR dprev1-dprev6 dvol1-dvol6 dvar1-dvar6 ; RUN; /*------------------- logistic regression and MIANALYZE---------------------------*/ proc logistic data=ex6.NORMdet outest=outreg covout descending noprint; by _imputation_; class gender szindep sector; model dprev6= gender szindep sector ; run; proc contents data= outreg short;run;* use this to find names of contrasts; PROC MIANALYZE data=outreg edf=4325 ; * these are the residual degrees of freedom from the model; var GENDER1 SZINDEPManual_high_depr sectorBehavioural sectorIndependent sectorSpecial ; run; /*--We can compare with the complete data analysis here---------*/ proc logistic data=ex6.ex6 descending ; class gender szindep sector; model dprev6= gender szindep sector ; run; /*---------------- now a check to compare prevalence by deprivation with original data---*/ proc means data=ex6.fromdetail; class szindep; var dprev6; run; proc means data=ex6.ex6; class szindep; var dprev6; run;