Sample 24586: Compute the number of observations and the average value of a variable within a BY-Group
Determine how many observations
there are in each BY-Group, as well as the average value of
a variable within each BY-Group, using BY-Group processing.
Note: This task can also be accomplished using PROC MEANS and alleviates the need for a sorted or indexed data set.
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 test;
input i j x;
datalines;
1 1 123
1 1 3245
1 2 23
1 2 543
1 2 87
1 3 90
2 1 88
2 1 86
;
/* When the first observation in each BY-Group is read, the variables JSUB and */
/* FREQ are initialized to zero and with each subsequent observation in the */
/* BY-Group, FREQ is incremented by one and JSUB is incremented by the value of */
/* X. When the last observation in the BY-Group is read, AVER is created by */
/* dividing JSUB by FREQ to determine the average value for the group. */
data jsubtot (keep=i j freq aver);
set test;
by i j;
retain jsub freq;
if first.j then do;
jsub=0;
freq=0;
end;
jsub + x;
freq + 1;
if last.j then do;
aver=jsub/freq;
output;
end;
run;
proc print;
run;
/* Alternate approach using PROC MEANS to generate the same output */
proc means data=test noprint;
by i j;
output out=i_j(drop=_FREQ_ _TYPE_) n=freq mean=aver;
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 i j freq aver
1 1 1 2 1684.00
2 1 2 3 217.67
3 1 3 1 90.00
4 2 1 2 87.00
Determine how many observations there are in each BY-Group, as well as the average value of a variable within each BY-Group, using BY-Group processing.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step Data Management ==> Manipulation and Transformation ==> BY-group processing
|
| Date Modified: | 2006-04-13 03:03:02 |
| Date Created: | 2004-09-30 14:08:56 |
Operating System and Release Information
| SAS System | Base SAS | All | n/a | n/a |