Skip to content

Instantly share code, notes, and snippets.

@statgeek
Created September 21, 2019 18:42
Show Gist options
  • Save statgeek/93b7ee3318cf026e4b4507da7d6748f4 to your computer and use it in GitHub Desktop.
Save statgeek/93b7ee3318cf026e4b4507da7d6748f4 to your computer and use it in GitHub Desktop.
SAS - Add ranks - Select Top N records
*Create ranks for variables value;
proc rank data=sashelp.class out=class_ranked
/* (where= (rank_weight < 4)) */
;
var weight;
ranks rank_weight;
run;
*display output;
title 'Unsorted but ranked';
proc print data=class_ranked;
run;
*sort for display;
proc sort data=class_ranked;
by rank_weight;
run;
title 'Sorted and ranked';
proc print data=class_ranked;
run;
*filter out the top 3 regardless of the number of ties;
data final;
set class_ranked;
by rank_weight;
if first.rank_weight then group + 1;
if group <= 3;
run;*Create ranks for variables value;
proc rank data=sashelp.class out=class_ranked
/* (where= (rank_weight < 4)) */
;
var weight;
ranks rank_weight;
run;
*display output;
title 'Unsorted but ranked';
proc print data=class_ranked;
run;
*sort for display;
proc sort data=class_ranked;
by rank_weight;
run;
title 'Sorted and ranked';
proc print data=class_ranked;
run;
*filter out the top 3 regardless of the number of ties;
data final;
set class_ranked;
by rank_weight;
if first.rank_weight then group + 1;
if group <= 3;
run;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment