Sometimes, you might want to flag the extreme values after sorting a data set.
For example, you might want to find the least expensive product after sorting a product list by price.
Or you might want to find the most powerful car after sorting the data set by horsepower.
You can do so by using a common SAS programming technique demonstrated below.
Since the data set is small, you can easily identify the cheapest house in each region.
For example, House 3 is the cheapest house in Region A and it is selling for $430,000.
However, when you have a larger data set, say, 100 observations, identifying the lowest values wouldn't be as easy, if not impossible.
Instead, you can use a special technique that systematically identifies the lowest house price in each region.
The code systematically flags the house with the lowest price in each region.
When (i=1), the observation is the house that has the cheapest price in the region.
1. The code first sort the data by REGION and PRICE.
Within each region, the house price is sorted in ascending order.
2. It then creates a new variable "i" that flags the first observation from each region.
Within each region, the first observation is flagged.
The variable i correctly flags the cheapest house in each neighborhood.
Similarly, you can flag the house that has the highest selling price by flagging the last observation from each region.
The variable "i" again correctly flags the house with the highest selling price.
Data set must be first sorted by the grouping variable(s) and the result variable(s).
Grouping variable(s) is the variable that defines the groups in the data set.
In our example, the grouping variable is REGION.
Result variable(s) is the variable that determines the order within the grouping variable(s).
In our example, the result variable is PRICE.
​The data step must include a BY statement with the grouping variable(s) and the result variable(s).
In our example, the data step includes a BY statement with REGION and PRICE.
Note: the BY statement is crucial when using this technique. Without the BY statement, this technique wouldn't work.
​Use the IF.GROUP condition to identify the first and last observation within the grouping variable (i.e. REGION).
This tells SAS to flag the observation (i.e. i=1) if it is the very first observation from each region.
This tells SAS to flag the observation (i.e. i=2) if it is the last observation from each region.
As a result, all of the observations with extreme values (highest and lowest price) are flagged.
​Note: use this technique with caution and make sure your code meets all 3 requirements stated above.
Create a new data set that contains only the heaviest fish from each species.