As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.


Here are some tips for How to share your SAS knowledge with your professional network.


Using PROC REPORT to Cross-Tabulate Multiple Response Items

From sasCommunity
Jump to: navigation, search

Author

Patrick Thornton

Citation

Thornton, S. P. (2009). Using PROC REPORT to Cross-Tabulate Multiple Response Items. Proceedings of the Seventeenth Annual Western Users of the SAS® Software Conference, San Jose, CA..

Abstract

This paper describes for an intermediate SAS® user the use of PROC REPORT to create a cross tabulation of a multiple response item. Surveys often contain multiple response items, such as services received where a respondent may have received more than one service. In this case, an indicator variable (1=Yes, 0=No) is created for each response category. This paper shows how to include all the indicator variables and a grouping variable in a PROC REPORT in order to calculate the number of responses per category overall and by group. It also demonstrates how PROC REPORT may be used to calculate the percent of responses out of the number of observations, rather than out of the total number of responses. A macro program is also included that makes easy the generation of PROC REPORT syntax for the purpose of reporting multiple response items.


Download Paper

Media:WUSS-2009-Thornton-ReportChooseAny.pdf

Macro ChooseAll

%macro chooseall
       (lib=
       ,d=
       ,row=
       ,rowlabel=
       ,rowformat=
       ,obslabel=
       ,totallabel=
       ,series=
       ,labels=
       );
/* author: Patrick Thornton WUSS 2009
paper: 
http://www.sascommunity.org/wiki/
Using_PROC_REPORT_to_Cross-Tabulate_Multiple_Response_Items
*********/       
%let vargroup =; 
%let define =; 
%let definep =;
%let computedp =; 
%let total=; 
%let where=;
%*Iterate through each variable in series;
%let i = 1; 
%do %until(%scan(&series,&i)= | &i > 50);
    %let var = %scan(&series,&i);
    %let label = %scan(&labels,&i,%str(^));
    %*[1 COLUMN STATEMENT MULTIPLE RESPONSE VARIABLES AND PERCENTS];
    %let vargroup = &vargroup &var p&i;
    %*[2 DEFINE EACH MULTIPLE RESPONSE VARIABLE ];
    %let define = &define define &var / analysis 
                  sum "&label" format=5.0 center%str(;);
    %*[3 DEFINE EACH PERCENT];
    %let definep = &definep define p&i /computed "%" 
                   format=percent8.1 center %str(;);
    %*[4 COMPUTE EACH PERCENT];
    %let computedp = &computedp compute p&i%str(;) p&i %str(=) 
                     &var..sum /n%str(;)endcomp%str(;);
    %* [5 COMPUTE TOTAL];
    %let total= &total &var..sum %str(,);
    %* [6 CREATE A WHERE STATEMENT];
    %let where = &where &var ne %str(.) or;
    %let i = %eval(&i + 1);
    %end;
 
%*Remove the comma and the OR at the end;
%let total = %substr(&total,1,%eval(%length(&total) - 1));
%let where = %substr(&where,1,%eval(%length(&where) - 2));
 
proc report data = &lib..&d 
            missing 
            nowd;
            col    &row newcol n &vargroup total;
            define &row    / group left width=15 flow noprint;
            define newcol  / computed "&rowlabel" id;
            define n       / format=8.0 "&obslabel" center;
            &define
            &definep
            define  total  / computed "&totallabel" width=10 center;
            compute newcol / char length=15;
                    IF _BREAK_='_RBREAK_' THEN newcol = 'Total';
                    else newcol = put(&row,&rowformat..);
                    endcomp;
            &computedp
            compute total;
                    total = sum(&total);
                    endcomp;
            rbreak after /summarize;
            where &where ;
run;
%mend;

Example Program

options mprint;
*examples;
%*chooseall
       (lib=
       ,d=
       ,row=
       ,rowlabel=
       ,rowformat=
       ,obslabel=
       ,totallabel=
       ,series=
       ,labels=
       );
%chooseall(lib=work
          ,d=multir
          ,row=referral
          ,rowlabel=Referral
          ,rowformat=myrows
          ,odslabel=Children
          ,totallabel=Services
          ,series=CHI HV TS,CHI^HV^TS
          ,labels=?
          );

References