data answerSets; infile cards dlm='|' truncover; input answerSetId description $100.; cards; 1 | Single answer on a scale of 1 to 7 2 | Single answer Yes or No 3 | Single answer on a scale of 1 to 7, flowery run; data answers; infile cards dlm='|'; length answerSetId index 8 answerText $40; input answerSetId index answerText; cards; 1 | 1 | Terrible 1 | 2 | Awful 1 | 3 | Could be better 1 | 4 | Average 1 | 5 | Great 1 | 6 | Suprisingly Good 1 | 7 | Awesome 2 | 1 | No 2 | 2 | Yes 3 | 1 | Rotten 3 | 2 | Stunk 3 | 3 | Putrid 3 | 4 | Bleh 3 | 5 | Worthy 3 | 6 | Fragrant 3 | 7 | Swoony run; data questions; infile cards dlm='|'; length questionId $4 answerSetId 8 questionText $40; input questionId answerSetId questionText; cards; Q1 | 2 | Did you plan your trip using a Travel Agent Q2 | 1 | Describe the quality of your flight Q3 | 1 | Rate your car rental service Q4 | 1 | Rate the demeanor of the receptionist Q5 | 1 | Please rate the room Q6 | 3 | My reaction to the survey is run; data SurveyResults(label='Results delivered from external customer'); length personId 8; array A1 Q1_1-Q1_2; array A2 Q2_1-Q2_7; array A3 Q3_1-Q3_7; array A4 Q4_1-Q4_7; array A5 Q5_1-Q5_7; keep personId -- Q5_7; do i = 1 to 2; A1[i] = 0; end; do i = 1 to 7; A2[i] = 0; A3[i] = 0; A4[i] = 0; A5[i] = 0; end; do personId = 1 to 1350; i1 = 1 + 2*ranuni(1234); i2 = 1 + 7*ranuni(1234); i3 = 1 + 7*ranuni(1234); i4 = 1 + 7*ranuni(1234); i5 = 1 + 7*ranuni(1234); A1 [ i1 ] = 1; A2 [ i2 ] = 1; A3 [ i3 ] = 1; A4 [ i4 ] = 1; A5 [ i5 ] = 1; output; A1 [ i1 ] = 0; A2 [ i2 ] = 0; A3 [ i3 ] = 0; A4 [ i4 ] = 0; A5 [ i5 ] = 0; end; stop; run; proc transpose data=SurveyResults out=vector; by personId; var Q1_1 -- Q5_7; run; data vector2; set vector; questionId = scan(_name_,1,"_"); answerIndex = input(scan(_name_,2,"_"),best12.); drop _name_; run; proc sql; create table vector3 as select vector2.*, questions.answerSetId from vector2, questions where vector2.questionId = questions.questionId ; quit; %macro decoratedTabulation; proc sql noprint; select distinct answerSetId into :answerSets separated by ' ' from questions where questionId in (select questionId from vector2) ; quit; %do i = 1 %to &SQLOBS; ods html text='
'; %let answerSetId = %scan(&answerSets,&i); data fmt; set answers; where answerSetId = &answerSetId; retain fmtname "answer"; rename index=start answertext=label; run; proc format cntlin=fmt; run; title "Decorated answers"; proc tabulate data=vector3; class questionId; class answerIndex; table questionId,answerIndex*N all='Number of Survey Takers'*N=''; where col1=1 and answerSetId=&answerSetId; format answerIndex answer.; run; data fmt; set questions; retain fmtname '$question'; rename questionId=start questionText=label; run; proc format cntlin=fmt; run; title "Decorated questions and answers"; proc tabulate data=vector3; class questionId; class answerIndex; table questionId,answerIndex*N all='Number of Survey Takers'*N=''; where col1=1 and answerSetId=&answerSetId; format answerIndex answer.; format questionId $question.; run; %end; %mend; ods html file="%sysfunc(pathname(WORK))\tables.html" style=meadow; title "Plain Raw"; proc tabulate data=vector3; class questionId; class answerIndex; class col1; table questionId,answerIndex*(col1 all)*N ; run; title "Undecorated"; proc tabulate data=vector3; class questionId; class answerIndex; table questionId,answerIndex*N all='Number of Survey Takers'*N=''; where col1=1; run; options mprint; %decoratedTabulation; ods html close;