sas8879
sas8879

Reputation: 11

Selecting top 10 observations for each data type (SAS)

I am trying to select the top 10 exposures for each class of business out of a large data set.

Below is an example of the dataset.

dataset example

If I were to need the top 10 exposures then I would simply sort by exposure descending (as I have done) and use the (obs = 10) command.

However I require the top 10 for each LOB.

Do you know how I could do this in SAS?

Thanks!

Upvotes: 1

Views: 1157

Answers (1)

Therkel
Therkel

Reputation: 1438

I would create a counting dummy variable, counting the number of exposures per lines of business and then delete any observation for which the dummy variable exceeds 10.

This can be done in a single datastep (given that the data is properly sorted) by (ab-)using that SAS code runs top to bottom.

proc sort data = have out=temp; by lob descending exposure; run;

data want(drop=countlob);
    retain countlob;
    set temp;
    by lob;

    countlob = countlob + 1;
    if first.lob then countlob = 1;

    if countlob > 10 then delete;
run;

Upvotes: 1

Related Questions