Example 5.4 Subsetting a Table Based on the Calculated Average of a Group

Goal

Compute the average of a column for each group in a table. Select observations from each group based on the group's average.

Example Features

Featured StepPROC SQL
Featured Step Options and StatementsAVG aggregate function GROUP BY clause HAVING clause
Related TechniquePROC MEANS, CLASS, OUTPUT, and TYPES statements DATA step, match-merging by value

Input Table

Table SOYBEANS contains the soybean yield and acres of soybeans planted for 30 farms in four regions.

                 SOYBEANS


Obs    region    farmid    yield    acres 1 NW 456 33 95 2 NW 269 38 346 3 NW 295 31 59 4 NW 689 22 373 5 NW 080 30 289 6 NW 319 28 83 7 NW 703 29 114 8 SW 700 36 122 9 SW 178 28 214 10 SW 358 31 817 11 SW 045 ...

Get Combining and Modifying SAS® Data Sets: Examples Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.