Effective questionnaire processing with Arrays
%******************************************************************************; %** http://www.info-net.nl **; %******************************************************************************; %* Topic : Data manipulation *; %* Program : Effective questionnaire processing with Arrays *; %* Author : Raymond Ebben *; %* Location : http://www.info-net.nl/index.sas-programs/data-manipulation*; %* Date : March 2006 *; %* Version : 1.0 *; %* Description : This program illustrates how to effectivly process the *; %* result of a questionnaire using several examples *; %******************************************************************************; %* In datasets : None *; %* In macrovars : None *; %* In files : None *; %* Out datasets : Work.results *; %* Out macrovars : None *; %* Out files : None *; %******************************************************************************; %** Version control **; %******************************************************************************; %* Mod * Ver. * Date * Description *; %******************************************************************************; %******************************************************************************; %******************************************************************************; %* Create a dataset containing the results of a questionnaire per subject *; %******************************************************************************; options validvarname=v7; data questionnaire; length question1-question16 $1; input subject Question1-Question16 $; cards; 1 a b b a c c d d d d d e e a a a 2 b . a b b b a a d a a a a a a b 3 d e a a d e a a a d c c a a b a 4 a a b b a a b b c d e a a a a a 5 c c c a c c d d d d d e e a a a 6 a b b a c c . . . c c c c a a a 7 a d b a c c d a c d d e e a a a 8 a b d a c c d a d c d e e a a a 9 a b f a c c d d d c d e e a a a 10 d d d d d c d e a a d e e a a a ; run; %******************************************************************************; %* The following example is a large dataset performing several types of *; %* processing questionnaire results, each step is separated in comment blocks *; %******************************************************************************; data results(drop=i); set questionnaire; %****************************************************************************; %* The following array statement initialises the question array *; %****************************************************************************; array questions question1-question16; %****************************************************************************; %* Example 1 *; %* This example assumes that the answers of the questionnaire are ranked. *; %* Valid answers are A through F and are raked as: a=1, b=2 c=3 etc. *; %* *; %* First a new array called answers is created in which to store the ranked *; %* answers. Then the example processes the questions array. *; %* *; %* The ranked answer is derived by using the rank function on the original *; %* question answer (capital A returns 65, capital B returns 66 etc.) *; %* If the result form the rank function - 64 (capital A returns 65-64=1, *; %* capital B returns 66-64=2 etc.) is not greater than or equal to 1 and *; %* lesser than or equal to 6 (ie. A throug F) the ranked answer is set to *; %* missing, and a log warning is generated. *; %****************************************************************************; array rankedanswers 8 rankedanswer1-rankedanswer16; do i=1 to dim(questions); rankedanswers{i}=rank(upcase(questions{i}))-64; if not(1<=rankedanswers{i}<=6) then do; put "WAR" "NING: answer could not be derived " subject= questions{i}=; rankedanswers{i}=.; end; end; %****************************************************************************; %* Example 2 *; %* This example assumes the ranked answers of the previous example are *; %* rated using the following table: *; %* *; %* Rating table: *; %* Type 2 rank *; %* 1 2 3 4 5 6 *; %* Type 1 (A) (B) (C) (D) (E) (F) *; %* rank |----------------------------- *; %* 1(A) | 1 1 1 1 1 2 *; %* 2(B) | 2 2 2 2 3 3 *; %* 3(C) | 3 3 3 4 4 4 *; %* 4(D) | 4 4 5 5 5 5 *; %* 5(E) | 5 6 6 6 6 6 *; %* 6(F) | 7 7 7 7 7 7 *; %* *; %* This means that if a subject answered C to question 1 and D to question 2*; %* the rated result would be 5. The rated answer can only be derived when *; %* Both answers are not missing. *; %* This example will rate the following answers as specified *; %* *; %* Variable | Type 1 Type 2 *; %* ---------+------------------------------------ *; %* rating1 | rankedanswer1 rankedanswer2 *; %* rating2 | rankedanswer4 rankedanswer3 *; %* rating3 | rankedanswer9 rankedanswer11 *; %* rating4 | rankedanswer10 rankedanswer15 *; %* *; %* First a temproray multidimensional array will be created represeting the *; %* rating table as specified above. The the different rating values are *; %* derived by using the rankedanswers with the temporary array *; %****************************************************************************; array ratingtable (6,6) 8 _temporary_ (1,1,1,1,1,2, 2,2,2,2,3,3, 3,3,3,4,4,4, 4,4,5,5,5,5, 5,6,6,6,6,6, 7,7,7,7,7,7); if not missing(rankedanswer1) and not missing(rankedanswer2) then rating1=ratingtable(rankedanswer1,rankedanswer2); if not missing(rankedanswer4) and not missing(rankedanswer3) then rating2=ratingtable(rankedanswer4,rankedanswer3); if not missing(rankedanswer9) and not missing(rankedanswer11) then rating3=ratingtable(rankedanswer9,rankedanswer11); if not missing(rankedanswer10) and not missing(rankedanswer15) then rating4=ratingtable(rankedanswer10,rankedanswer15); %****************************************************************************; %* Example 3 *; %* This final example assumes that overall results are calculated using both*; %* the ranking and the rating answers derived in the previous steps. *; %* *; %* The following will be calculated : *; %* rankmean (mean of all non missing rankings questions 1-16 *; %* Only to be calculated if there is not more than one missing *; %* ranked answer) *; %* ratingsum (sum of all non missing ratings 1-4) *; %* *; %* First an array is created for the ranking variables as it did not exist *; %* yet. Then the overal answers are derived. *; %****************************************************************************; array ratings rating:; if 0<nmiss(of rankedanswers{*})<1 then rankmean=mean(of rankedanswers{*}); ratingsum=sum(of ratings{*}); run;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
© 2012 Raymond Ebben, expert SAS consultant and programmer, Netherlands All Rights Reserved.