Using PROC REPORT to Cross-Tabulate Multiple Response Items

Patrick Thornton
SRI International

Example of a Multiple Response Item
◦ Variable coding and example data


A Cross Tabulation using Proc REPORT
Examine the Proc REPORT Syntax for:
◦
◦
◦
◦
◦
Grouping and counting observations
Creating a summary row
Adding a ‘Total’ label to the summary row
Creating a count and percent
Summing columns
2
 Respondents may leave blank those service categories
they did not receive
 Respondents may not provide a response to any of the
service categories
3
Total:
Referral One:

 Children
Number of
receiving
childrenatreceiving
least oneatservice
least one service

 Children
Number of
and
children
percentreceiving
of children
each
receiving
service (e.g.
each CHI)
service
 Number
Percent of
receiving each service (e.g. HV)

of children
total services
 Number of total services
4
proc report data=md
 nowd missing;
 col referral n;
 define referral /group center;
 define n /’Children’ center;
 where CHI ne . or HV ne . or TS ne . ;
run;



DEFINE
COL determines
prevents
thethe
the
/GROUP
order
option
of columns,
window
is used
REFERRAL
and
to
obtain
MISSING
and
the N
 NOWD
Nwith
is used
to interactive
label
and
center
the
column

WHERE
removes
OBS
missing
on
all
service
variables
includes
distinct
where Nvalues
is
OBS
a key
missing
ofword
REFERRAL
REFERRAL
to obtain the count of observations
5
proc report data=work.multir
missing nowd;
col referral n;
define referral /group;
define n /’Children’ center;
 rbreak after / summarize;
Run;
 Creates total number of children (OBS)
6
proc report data=work.multir
missing nowd out=d;
col referral n;
define referral /group;
define n /’Children’ center;
rbreak after / summarize;
run;
Proc print data=d noobs;
run;
7
col referral  newcol n;
define referral /group  noprint;
define n /’Children’ center;
 define newcol/computed ‘Referral’;
 compute newcol /char length=15;
 if _BREAK_="_RBREAK_" then newcol = 'Total';
else newcol = put(referral,myrows.);
endcomp;
rbreak after / summarize;
 Test

Adds_BREAK_
Defines
NEWCOL
ablock,
computed
as
COMPUTED
variable
not
in the

Hides
the
REFERRAL
variable

Start
aNEWCOL,
compute
NEWCOL
as aset
15 NEWCOL
length

for
summary
row
and
data
set variable
character
to
“Total,”
otherwise set to formatted value of
REFERRAL.
8
col referral newcol n  CHI  PCHI;
define referral /group noprint;
define n / ‘Children’ center;
 define CHI / analysis ‘CHI’ sum format=5.0 center;
 define PCHI /computed ‘%CHI’ format=percent8.1 center ;
 compute PCHI;
PCHI = CHI.sum/n;
endcomp;

isaacolumn
columnand
and
definedas

 PCHI
CHIisisCOMPUTED
defined
SUM

PCHI
as
CHI.SUM
/as
naCOMPUTED
Repeat for each Service
9
col referral newcol n  CHI PCHI HV PHV TS PTS total;
 define total/computed "Services" width=10 center;
compute total;

total = sum(chi.sum, hv.sum,ts.sum);
endcomp;


 Total
As shown
is sum
defined
on of
previous
the
as COMPUTED
sums
slide,
of each
service
service
sumsvariable
and
computed percents. Total column is added.
10
proc
compute
report newcol
data=work.multir
/char length=15;
missing nowd ;
col IF
referral
_BREAK_='_RBREAK_'
newcol n CHI p1THEN
HV p2
newcol
TS p3=total;
'Total';
define
elsereferral
newcol/group
= put(referral,myrows.);
left width=15 noprint;
define
endcomp;
newcol /computed "Referral" ;
compute
define n/format=8.0
p1;
"Children" center;
define
p1 =
CHI
CHI.sum
/ analysis
/n; sum "CHI" format=5.0 center;
endcomp;
define HV / analysis sum "HV" format=5.0 center;
compute
define TSp2;
/ analysis sum "TS" format=5.0 center;
define
p2 =
p1HV.sum
/computed
/n; "%" format=percent8.1 center ;
endcomp;
define p2 /computed "%" format=percent8.1 center ;
compute
define p3p3;
/computed "%" format=percent8.1 center ;
define
p3 =
total/computed
TS.sum /n;
"Services" width=10 center;
endcomp;
compute total;
total = sum(CHI.sum , HV.sum , TS.sum);
endcomp;
rbreak after /summarize;
where CHI ne . or HV ne . or TS ne . ;
run;
11
proc report data=work.multir missing nowd;
col referral newcol n CHI P1 total;
define referral /group noprint;
define n / ‘Children’ center;
define newcol/computed ‘Referral’;
define CHI / analysis ‘CHI’ sum;
define P1 /computed ‘%’;
compute newcol /char length=15;
if _BREAK_=‘_RBREAK_’ then newcol= 'Total';
else newcol = put(referral,myrows.);
endcomp;
compute P1; P1 = CHI.sum /n; endcomp;
define total/computed ‘Services’ width=10;
compute total;
total = sum(chi.sum);
endcomp;
rbreak after / summarize;
where CHI ne .;
run;
Example
•Libref
and Data SetMacro
Values
•Row
variable, label Variables
and format
•Observation
Label &Lib
Work
•Total Label
Multir for the multiple
&d
•Variables
referralitem
&row
response
•Labels
for each variable
Referral
&rowlabel
Myrows
&rowformat
Children
&obslabel
Services
&totallabel
CHI HV TS
&series
CHI^HV^TS
&labels
12
%macro chooseall(lib,d,row,rowlabel,rowformat,obslabel,totallabel,series,labels);
proc report data=&lib..&d missing nowd;
col &row newcol n [1 SERIES AND PERCENT] total; [e.g. CHI p1 HV p2 TS p3]
define &row /group left width=15 noprint;
define newcol /computed “ &rowlabel “;
define n/format=8.0 “ &obslabel " center;
[2 DEFINE EACH SERIES VARIABLE ] [e.g. define CHI / analysis sum "CHI";]
[3 DEFINE PERCENT FOR EACH] [e.g. define p1 /computed "%“ ;]
define total/computed “ &totallabel " width=10 center;
compute newcol /char length=15;
IF _BREAK_='_RBREAK_' THEN newcol = 'Total';
else newcol = put( &row ,” &rowformat.. “);
endcomp;
[4 COMPUTE EACH PERCENT] [e.g. compute p1; p1=CHI.sum /n; endcomp;]
compute total;
[5 COMPUTE TOTAL] [e.g. total = sum(CHI.sum , HV.sum , TS.sum);]
endcomp;
rbreak after /summarize;
[6 CREATE A WHERE STATEMENT] [e.g. where CHI ne . or HV ne . or TS ne . ;]
run;
%mend;
13
%macro
chooseall(lib,d,row,rowlabel,rowformat,obslabel,totallabel,series,labels);
%let vargroup =; %*[1 COLUMN SERIES AND PERCENTS] ;
%let define =; %*[2 DEFINE EACH SERIES VARIABLE];
%let definep =; %*[3 DEFINE PERCENT FOR EACH];
%let computedp =; %*[4 COMPUTE EACH PERCENT] ;
%let total=; %* [5 COMPUTE TOTAL];
%let where=; %* [6 CREATE A WHERE STATEMENT];
%*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(^));
%*
[5
COMPUTE
TOTAL];
%*[2
%*[3
[6DEFINE
CREATEEACH
PERCENT
A
WHERE
SERIES
FOR
STATEMENT];
VARIABLE
EACH]; ]; ;
%*[1
COLUMN
SERIES
AND
PERCENTS]
%letwhere
total===&define
&total
&var..sum
%str(,);
%let
%let
definep
define
=&where
&definep
&var
define
define
ne
&var
%str(.)
p&i
/ /computed
analysis
or;
sum
"%"
"&label"
format=percent8.1
format=5.0 center%str(;);
center %str(;);
vargroup
=
&vargroup
&var
p&i;
%let i = %eval(&i + 1);
%end;
%*Remove the comma and or;
%let total = %substr(&total,1,%eval(%length(&total) - 1));
%let where = %substr(&where,1,%eval(%length(&where) - 2));
%*[PROC REPORT FROM PREVIOUS SLIDE];
%mend;
14



PROC REPORT can Generate Client-Quality
Tabulations of Multiple Response Items (MRI)
A Relatively Concise Macro can be Used to
Generate the Extended PROC REPORT Syntax
Limitations
◦ The MRI variables must be coded 1=Yes, 0=No
◦ Many MRI variables requires table breaks
15
Your comments and questions are valued and
encouraged.
Patrick Thornton Ph.D.
Sr. Scientific Programmer/Analyst
SRI International, Center for Education and
Human Services
Phone: 650 859-5583
Fax: 650 859-2861
Email: [email protected]
Web: www.sri.com
16