Sample 24778: Select a specified number of observations from the top of each BY-Group
Select the top three observations from the top of 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.
/*********************************************************************************/
/* The following data set contains contribution amounts for each value of GROUP. */
/* In order to subset the top three observations within each BY-Group with the */
/* three largest contribution amounts, sort the data set by the GROUP */
/* variable and descending AMOUNT. In the DATA step use the BY statement */
/* following the SET statement to create the automatic FIRST. and LAST. */
/* BY-Group variables. Create a new COUNT variable and set its value to zero */
/* on the first observation of each group. Increment the COUNT variable by */
/* adding the value of one for each observation by using the SUM statement. */
/* Output the observations where the COUNT value equals a 1, 2, or 3. */
/*********************************************************************************/
data groups;
input group $ amount date date9.;
format date date9. amount dollar6.;
datalines;
A 1000 06Mar2000
A 550 01Mar2000
A 375 15Mar2000
A 1500 01Jun2000
A 900 15Jul2000
A 800 30Jun2000
B 500 01Mar2000
B 400 15Mar2000
B 1050 01Jun2000
B 330 15Jul2000
B 575 30Jun2000
;
proc sort;
by group descending amount;
run;
data top3(drop=count);
set groups;
by group descending amount;
if first.group then count=0;
count+1;
if count le 3 then output;
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 GROUP AMOUNT DATE
1 A $1,500 01JUN2000
2 A $1,000 06MAR2000
3 A $900 15JUL2000
4 B $1,050 01JUN2000
5 B $575 30JUN2000
6 B $500 01MAR2000
Select the top three observations from the top of 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:33 |
Date Created: | 2004-09-30 14:09:13 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |