Sample 24744: Calculating totals across a BY-Group to produce either a grand or cumulative total
Create a data set that collapses each BY-Group into a single
observation and produces grand totals for variables in each
BY-Group.
Note:
Documentation: "Combining and Modifying SAS Data Sets",
Example 5.9
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.
data scores;
input id $ game1 game2 game3;
datalines;
A 2 3 4
A 5 6 7
B 1 2 3
C 1 2 3
C 4 5 6
C 7 8 9
;
proc sort data=scores;
by id;
run;
/* This produces output with one observation per BY-Group showing grand totals; */
/* however, if you'd rather see cumulative totals per observation, remove the line */
/* of code "if last.id then output". */
data grandtot(drop=temp1 temp2 temp3);
set scores(rename= (game1=temp1 game2=temp2 game3=temp3));
by id;
if first.id then
do;
game1=0;
game2=0;
game3=0;
end;
game1 + temp1;
game2 + temp2;
game3 + temp3;
if last.id then output;
run;
proc print data=grandtot;
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.
Obs id game1 game2 game3
1 A 7 9 11
2 B 1 2 3
3 C 12 15 18
Create a data set that collapses each BY-Group into a single
observation and produces grand totals for variables in each
BY-Group.
Type: | Sample |
Topic: | SAS Reference ==> DATA Step Data Management ==> Manipulation and Transformation ==> BY-group processing
|
Date Modified: | 2005-12-08 11:34:29 |
Date Created: | 2004-09-30 14:09:09 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |