The sample code on the Full Code tab shows how to calculate cumulative column, row, and overall percentages for variables under an ACROSS variable with PROC REPORT.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
This sample shows how to calculate cumulative column, row, and overall percentages for variables under an ACROSS variable with PROC REPORT. When calculating percents that are under ACROSS variables, the denominators must be calculated within a COMPUTE BEFORE block. The percents are then calculated in a separate COMPUTE block, using the column number, in the form _Cn_, where n is the column number.
The sample below hard-codes the column references (in the form _cn_). The column references can be built with macro code to avoid hard-coding and having to know the number of columns in advance. The example beginning on page 12 of Beyond the Basics: Advanced PROC REPORT Tips and Tricks demonstrates the syntax.
data test;
input answer question $;
datalines;
0 ques1
0 ques1
0 ques1
1 ques1
1 ques1
1 ques1
1 ques1
1 ques1
1 ques1
1 ques1
0 ques1
1 ques2
1 ques2
0 ques2
0 ques2
1 ques2
0 ques2
0 ques2
1 ques3
1 ques3
0 ques3
0 ques3
0 ques3
1 ques3
1 ques3
1 ques3
0 ques3
0 ques3
0 ques3
1 ques3
;
run;
proc format;
value myyn
0 = "No"
1 = "Yes"
;
value $ myques
"ques1" = "Question 1 Text"
"ques2" = "Question 2 Text"
"ques3" = "Question 3 Text"
;
run;
ods listing close;
ods html file="c:\percents.html" style=sasweb;
title "COLUMN PERCENTS";
proc report data=test nowd split="~" style(header)=[vjust=b];
format question $myques. answer myyn.;
column question answer,(n pct cum) all;
define question / group "Question";
define answer / across "Answer";
define pct / computed "Column~Percent" f=percent8.2;
define cum / computed "Cumulative~Column~Percent" f=percent8.2;
define all / computed "Total number~of answers";
/* Sum total number of ANSWER=0 and ANSWER=1 */
compute before;
den0 = _c2_;
den1 = _c5_;
endcomp;
/* Calculate percentage */
compute pct;
_c3_ = _c2_ / den0;
_c6_ = _c5_ / den1;
endcomp;
compute all;
all = _c2_ + _c5_;
/* Calculate cumulative percent */
temp0 + _c3_;
_c4_ = temp0;
temp1 + _c6_;
_c7_ = temp1;
endcomp;
run;
title "ROW PERCENTS";
proc report data=test nowd split="~" style(header)=[vjust=b];
format question $myques. answer myyn.;
column question answer,(n pct) all;
define question / group "Question";
define answer / across "Answer";
define pct / computed "Row~Percent" f=percent8.2;
define all / computed "Total number~of answers";
/* Sum total number of responses to question */
compute before question;
den = _c2_ + _c4_;
endcomp;
/* Calculate percentage */
compute pct;
_c3_ = _c2_ / den;
_c5_ = _c4_ / den;
endcomp;
compute all;
all = _c2_ + _c4_;
endcomp;
run;
title "OVERALL PERCENTS (PCTSUM)";
proc report data=test nowd split="~" style(header)=[vjust=b];
format question $myques. answer myyn.;
column question answer,(n pct) all;
define question /group "Question";
define answer /across "Answer";
define pct /computed "Percent~of total" f=percent8.2;
define all /computed "Total number~of answers";
/* Sum total number of responses to question */
compute before;
den = _c2_ + _c4_;
endcomp;
/* Calculate percentage */
compute pct;
_c3_ = _c2_ / den;
_c5_ = _c4_ / den;
endcomp;
compute all;
all = _c2_ + _c4_;
endcomp;
run;
ods html close;
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Type: | Sample |
Topic: | SAS Reference ==> Procedures ==> REPORT |
Date Modified: | 2011-05-06 11:50:13 |
Date Created: | 2011-04-27 14:59:39 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | Base SAS | z/OS | 9 TS M0 | |
Microsoft® Windows® for 64-Bit Itanium-based Systems | 9 TS M0 | |||
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9 TS M0 | |||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9 TS M0 | |||
Microsoft Windows 2000 Advanced Server | 9 TS M0 | |||
Microsoft Windows 2000 Datacenter Server | 9 TS M0 | |||
Microsoft Windows 2000 Server | 9 TS M0 | |||
Microsoft Windows 2000 Professional | 9 TS M0 | |||
Microsoft Windows NT Workstation | 9 TS M0 | |||
Microsoft Windows Server 2003 Datacenter Edition | 9 TS M0 | |||
Microsoft Windows Server 2003 Enterprise Edition | 9 TS M0 | |||
Microsoft Windows Server 2003 Standard Edition | 9 TS M0 | |||
Microsoft Windows XP Professional | 9 TS M0 | |||
64-bit Enabled AIX | 9 TS M0 | |||
64-bit Enabled HP-UX | 9 TS M0 | |||
64-bit Enabled Solaris | 9 TS M0 | |||
HP-UX IPF | 9 TS M0 | |||
Linux | 9 TS M0 | |||
OpenVMS Alpha | 9 TS M0 | |||
Tru64 UNIX | 9 TS M0 |