Sample 63064: Add a row with percentages to PROC REPORT output
You can create multiple summary rows with the REPORT procedure, with one row containing totals and another row containing percentages based on those totals. A BREAK statement is needed to generate the first summary row, and an RBREAK statement is needed to generate the second summary row. Within a COMPUTE block, the percentages are calculated and an appropriate format is applied. The following steps are necessary for creating the desired summary rows:
- Add a dummy variable in the input data set for each additional summary row that you want in the report.
- Add the new dummy variables to the beginning of the COLUMN statement.
- For each dummy variable, add a DEFINE statement with either a GROUP or ORDER variable and a NOPRINT option.
- For each dummy variable, write a BREAK AFTER variable-name statement.
- Use a COMPUTE AFTER location block to calculate the needed percentages.
- Use a CALL DEFINE statement to apply a format.
See the sample code on the Full Code tab for an illustration.
For an example using multiple GROUP and ORDER variables, see SAS Note 43975, "Create multiple summary rows with PROC REPORT when more than one GROUP/ORDER variable is requested."
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.
/*Create a dummy variable that has the same value for all observations
in data set*/
data shoes;
set sashelp.shoes;
dummy=1;
run;
proc report data=shoes;
where region in ('Africa' 'Asia' 'Canada');
column dummy region product sales inventory;
define dummy / group noprint;
define region / group;
define product / group;
/*Generate two summary rows
Initially these rows will have identical values*/
break after dummy / summarize;
rbreak after / summarize;
/*Add explanatory text to first summary row*/
compute after dummy;
region = 'Totals';
endcomp;
/*Modify second summary row
-add explanatory text
-calculate percentages
-assign appropriate format*/
compute after;
region = 'Percent of Total';
/*calculate percentage sales of overall inventory*/
sales.sum = sales.sum / inventory.sum;
/*assign appropriate format to percent value*/
call define('sales.sum','format','percent8.2');
inventory.sum = inventory.sum / inventory.sum;
call define('inventory.sum','format','percent8.2');
endcomp;
run;
/********
the format can also be changed in a compute block for the analysis variable
this method is required when sending to the Listing destination
*********/
proc report data=shoes;
where region in ('Africa' 'Asia' 'Canada');
column dummy region product sales inventory;
define dummy / group noprint;
define region / group;
define product / group;
/*Generate two summary rows
Initially these rows will have identical values*/
break after dummy / summarize;
rbreak after / summarize;
/*Add explanatory text to first summary row*/
compute after dummy;
region = 'Totals';
endcomp;
/*Assign appropriate format*/
compute inventory;
if _break_ = '_RBREAK_' then do;
call define('sales.sum','format','percent8.2');
call define('inventory.sum','format','percent8.2');
end;
endcomp;
/*Modify second summary row
-add explanatory text
-calculate percentages*/
compute after;
region = 'Percent of Total';
/*calculate percentage sales of overall inventory*/
sales.sum = sales.sum / inventory.sum;
inventory.sum = inventory.sum / inventory.sum;
endcomp;
run;
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.
SAS Output
Africa |
Boot |
$119,835 |
$611,229 |
|
Men's Casual |
$562,794 |
$1,448,363 |
|
Men's Dress |
$318,500 |
$845,729 |
|
Sandal |
$190,409 |
$655,733 |
|
Slipper |
$337,076 |
$1,223,585 |
|
Sport Shoe |
$22,150 |
$145,803 |
|
Women's Casual |
$417,516 |
$1,091,582 |
|
Women's Dress |
$374,308 |
$1,079,049 |
Asia |
Boot |
$62,708 |
$170,165 |
|
Men's Casual |
$11,754 |
$2,176 |
|
Men's Dress |
$119,366 |
$272,634 |
|
Sandal |
$8,208 |
$36,570 |
|
Slipper |
$152,032 |
$485,082 |
|
Sport Shoe |
$2,092 |
$16,057 |
|
Women's Casual |
$25,837 |
$52,827 |
|
Women's Dress |
$78,234 |
$140,628 |
Canada |
Boot |
$385,613 |
$1,362,772 |
|
Men's Casual |
$441,903 |
$909,052 |
|
Men's Dress |
$920,101 |
$2,327,082 |
|
Sandal |
$14,798 |
$99,035 |
|
Slipper |
$952,751 |
$3,459,849 |
|
Sport Shoe |
$140,389 |
$765,695 |
|
Women's Casual |
$410,807 |
$895,434 |
|
Women's Dress |
$989,350 |
$3,291,790 |
Totals |
|
$7,058,531 |
$21,387,921 |
Percent of Total |
|
33.00% |
100.0% |
With PROC REPORT, you can have a summary row that contains totals, as well as a row of percentages that are calculated based on those total values.
Date Modified: | 2021-11-12 12:35:07 |
Date Created: | 2018-10-12 11:51:52 |
Operating System and Release Information
SAS System | Base SAS | Microsoft Windows Server 2003 for x64 | | |
Microsoft Windows Server 2003 Standard Edition | | |
Microsoft Windows Server 2003 Enterprise Edition | | |
Microsoft Windows NT Workstation | | |
Microsoft Windows Server 2003 Datacenter Edition | | |
Microsoft Windows 2000 Professional | | |
Microsoft Windows 2000 Server | | |
Microsoft Windows 2000 Advanced Server | | |
Microsoft Windows 95/98 | | |
Microsoft Windows 8.1 Pro x64 | | |
Microsoft Windows 8.1 Pro 32-bit | | |
Microsoft Windows 8.1 Enterprise x64 | | |
Microsoft Windows 8 Pro x64 | | |
Microsoft Windows 8.1 Enterprise 32-bit | | |
Microsoft Windows 8 Pro 32-bit | | |
Microsoft Windows 8 Enterprise 32-bit | | |
Microsoft Windows 8 Enterprise x64 | | |
OS/2 | | |
Microsoft® Windows® for x64 | | |
Microsoft Windows Server 2003 Enterprise 64-bit Edition | | |
Microsoft Windows Server 2003 Datacenter 64-bit Edition | | |
Microsoft® Windows® for 64-Bit Itanium-based Systems | | |
OpenVMS VAX | | |
Macintosh | | |
z/OS 64-bit | | |
z/OS | | |
Microsoft Windows 2000 Datacenter Server | | |
Microsoft Windows 10 | | |
Microsoft Windows XP 64-bit Edition | | |
Microsoft Windows Server 2008 | | |
Microsoft Windows Server 2008 R2 | | |
Microsoft Windows Server 2008 for x64 | | |
Microsoft Windows Server 2012 Datacenter | | |
Microsoft Windows Server 2012 R2 Datacenter | | |
Microsoft Windows Server 2012 R2 Std | | |
Microsoft Windows Server 2012 Std | | |
Microsoft Windows Server 2016 | | |
Microsoft Windows XP Professional | | |
Windows 7 Enterprise 32 bit | | |
Windows 7 Enterprise x64 | | |
Windows 7 Home Premium 32 bit | | |
Windows 7 Home Premium x64 | | |
Windows 7 Professional 32 bit | | |
Windows 7 Professional x64 | | |
Windows 7 Ultimate 32 bit | | |
Windows 7 Ultimate x64 | | |
Windows Millennium Edition (Me) | | |
Windows Vista | | |
Windows Vista for x64 | | |
64-bit Enabled AIX | | |
64-bit Enabled HP-UX | | |
64-bit Enabled Solaris | | |
ABI+ for Intel Architecture | | |
AIX | | |
HP-UX | | |
HP-UX IPF | | |
IRIX | | |
Linux | | |
Linux for x64 | | |
Linux on Itanium | | |
OpenVMS Alpha | | |
OpenVMS on HP Integrity | | |
Solaris | | |
Solaris for x64 | | |
Tru64 UNIX | | |