Sentry Page Protection
Data Manipulation [5-18]
Flagging Extreme Values II
So how does the flagging technique work?
Let's examine the code with the EXAM data set one more time.
Example
Let's examine the code with the EXAM data set one more time.
Example
Two Steps Flagging Extreme Values
The special technique involves two steps:
Step 1
Sort the data set so that the exam results are in ascending order within each subject.
The exam results will be in ascending order for each subject:
Step 2
Flag the first (or last) observation within each subject.
This will flag the first observation:
Since the results are in ascending order, the first observation happens to be the lowest result for the particular subject.
As a result, the lowest result is flagged as "1".
Two Important Notes
1. Flagging the last observation is just the same.
Use the (last.subject) in the IF statement:
This will flag the last observation (i.e. highest result) instead:
2. The BY statement in the data step is needed for this technique to work.
Without the BY statement, the flagging wouldn't work.
Exercise
Locate the CARS data set from the SASHelp library.
Identify the most and least powerful cars (HORSEPOWER) from each car maker (MAKE).
Keep only the observations that are flagged and remove the rest of the observations.
Save the data set in the WORK library.
Locate the CARS data set from the SASHelp library.
Identify the most and least powerful cars (HORSEPOWER) from each car maker (MAKE).
Keep only the observations that are flagged and remove the rest of the observations.
Save the data set in the WORK library.
Need some help?
HINT:
Add an additional IF statement to subset the data set.
SOLUTION:
Proc Sort Data=SASHelp.Cars Out=Cars;
By Make Horsepower;
Run;
Data Cars2;
Set Cars;
By Make Horsepower;
if first.make then i=1;
else if last.make then i=2;
if i in (1, 2);
Run;
Fill out my online form.