user1094779
user1094779

Reputation: 141

Using SAS I want to print the 5 rows with the highest value in a certain column

I have a dataset with several columns. I can get the extreme observations for a column or set of columns like this ...

PROC Univariate data       = Work.tempVal
            nextrobs   = 5 ;
 ods select ExtremeObs ;
 ods output ExtremeObs = ExtremeObs;
            var          B C;

run;

What I would like to do is print out the dataset row for each one of the extreme observations. So I am getting the column that I am targeting for extremity but I want the rest of the columns as well.

Upvotes: 1

Views: 1358

Answers (2)

user1094779
user1094779

Reputation: 141

Turns out the id keyword includes other columns

So ...

 PROC Univariate data       = Work.tempVal
        nextrobs   = 5 ;
 ods select ExtremeObs ;
 ods output ExtremeObs = ExtremeObs;
        var          B;
        id           A C D;
 run;

will return columns A, B, C, and D where B is an extreme observation.

Upvotes: 1

Chris J
Chris J

Reputation: 7769

You can use proc sql with the outobs= option, and the appropriate sort order.

For example, to get the rows with the top 5 maximum values :

proc sql outobs=5 ;
  create table top5 as
  select *
  from mydata 
  order by targetvar descending ;
quit ;

Obviously, if you've got multiple rows with the same maximum value, you may want to use a different approach.

Upvotes: 0

Related Questions