Sample 24595: Counting the number of observations in a BY-Group
Determine how many observations
there are in each BY-Group by using BY-Group processing.
Note:
PROC FREQ can also be used to count the frequency of values in each BY-Group. A sort is not necessary when using PROC FREQ.
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 old;
input state $ accttot;
datalines;
ca 7000
ca 6500
ca 5800
nc 4800
nc 3640
sc 3520
va 4490
va 8700
va 2850
va 1111
;
proc sort data=old;
by state;
run;
/* To get the number of observations in each group of states, start */
/* a counter on the first observation of each BY-Group. The last */
/* observation in the BY-Group contains the total number of */
/* observations and is output to the data set. */
data new;
set old (drop= accttot);
by state;
if first.state then count=0;
count+1;
if last.state then output;
run;
proc print;
run;
/* Alternative approach using PROC FREQ to generate the same output */
proc freq;
tables state / out=new(drop=percent);
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 state COUNT
1 ca 3
2 nc 2
3 sc 1
4 va 4
Determine how many observations
there are in each BY-Group by using BY-Group processing.
Type: | Sample |
Topic: | SAS Reference ==> DATA Step Data Management ==> Manipulation and Transformation ==> BY-group processing
|
Date Modified: | 2013-09-03 13:42:31 |
Date Created: | 2004-09-30 14:08:57 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |