This is a following post after my previous post about SAS/SQL.
SAS’s SQL procedure has a basic SQL syntax. I found that the most challenging work is to use PROC SQL to solve the TOP N (or TOP N by Group) questions. Comparing with other modern database systems, PROC SQL is lack of -
- The ranking functions such as
SELECT TOPclause such as
select TOP 3 * from class ;
partition byclause such as
select sex, name, weight from (select sex, name, max(weight) over(partition by sex) max_weight from class) where weight = max_weight ;
However, there are always some alternative solutions in SAS. I list a few question from an ascending difficulty below to explore the possibilities.
Prepare the data
First a SASHELP.CLASS dataset is used as a demo (availabe for every SAS copy). It is a small weight and height dataset from a faked class of 19 children. Now I only keep the
weightvariable as target column.
data class; set sashelp.class; keep name sex weight; run; proc sort; by descending weight; run;
1. Select highest value
It is straightforward to use the
outobsoption at the begining to single out the highest weight.
title "Select highest weight overall"; proc sql outobs = 1; select name, weight from class order by weight desc ;quit;
2. Select second highest value
How about the second highest weight? The logic is simple — if we remove the highest weight first, then the second highest weight will take the first row.
title "Select second highest weight overall"; proc sql outobs = 1; select name, weight from class where weight not in (select max(weight) from class) order by weight desc ;quit;
3. Select Nth highest value
Now it comes to the hard part. How about the Nth highest value, say, the fourth highest weight? Now we have to do a self-joining to let the distinct value point to 3. Since there are two children with the weight 112.5, the query returns the two tied names.
title "Select Nth highest weight"; %let n = 4; proc sql; select distinct a.name, a.weight from class as a where (select count(distinct b.weight) from class as b where b.weight > a.weight ) = &n - 1; quit;
4. Select highest values by group
There are two groups Male and Female in the class, and the easiest way to find the highest weight for each category is
select max for female union select max for male. However, a more scalable solution is to use the
group byclause that fits more than two groups.
title "Select highest weights by group"; proc sql; select sex, name, weight from class group by sex having weight = max(weight) ;quit;
5. Rank all values
The ultimate solution to solve all the question above is to derive a
rankcolumn for the target. There are two solutions: the first one use a subquery in the
selectclause, while the second one utilizes a subquery in the
The subquery in the first solution is independent to the main query, which uses less codes and is easier to recall in practice. The second one is actually a self-joining that is faster than the first solution.
/* Solution I */ proc sql; select name, weight, (select count(distinct b.weight) from class as b where b.weight >= a.weight) as Rank from class as a order by rank ;quit; /* Solution II */ proc sql; select a.name, a.weight, count(b.weight) as rank from class as a, (select distinct weight from class ) as b where a.weight <= b.weight group by a.name, a.weight order by a.weight desc ;quit;
6. Select top N values by group
Once with the
rankcolumn at hand, many perplexing problems could be easily solved. For example, we can use it to find the top 3 heaviest people for each category of male and female. And it is also scalable to more than two groups.
title "Select Top N weights by group"; proc sql; select a.sex, a.name, a.weight, (select count(distinct b.weight) from class as b where b.weight >= a.weight and a.sex = b.sex ) as rank from class as a where calculated rank <= 3 order by sex, rank ;quit;