Sample 24649: Generate a cumulative total per BY-group using DATA step code
The sample code on the Full Code tab illustrates how to use BY processing to sum a variable to produce a total in each BY-group.
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 sample data */
data one;
input x y;
datalines;
1 3
1 6
2 7
2 8
2 9
2 5
3 3
3 5
4 10
4 4
4 9
4 15
4 3
;
run;
/* Initialize the TOTAL variable back to zero at the beginning of each */
/* BY-Group. Sum the variable while the BY-Group remains the same */
/* and produce a total for the BY-Group on the last observation of the */
/* BY-Group. Output the last observation for each BY-Group. */
data two;
set one;
by x;
if first.x then total=0;
total + y;
if last.x then output;
drop y;
run;
proc print;
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 x total
1 1 9
2 2 29
3 3 8
4 4 41
Type: | Sample |
Topic: | SAS Reference ==> DATA Step Data Management ==> Manipulation and Transformation ==> BY-group processing
|
Date Modified: | 2005-12-08 11:34:16 |
Date Created: | 2004-09-30 14:09:00 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |