Selecting 5 max observations per category


/ Published in: SAS
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /* original data set */
  2. data firm; input category size @@;
  3. datalines;
  4. 1 93458934 1 8467489 1 8679 2 56757 3 57850
  5. 1 456794 2 697058 2 59876 3 687957 3 698879
  6. 3 598679 1 8756356 1 356396 1 65365 2 56385
  7. 2 4686 2 469674967 2 49679674 3 4686 3 47696
  8. 3 76946794 3 64868
  9. ;
  10.  
  11. /* sort by category and then by size within each category in descending order */
  12. proc sort data=firm;
  13. by category DESCENDING size;
  14. run;
  15.  
  16. /* create rank variable within each category */
  17. data firm1; set firm;
  18. by category;
  19. rank+1;
  20. if first.category then rank=1;
  21. run;
  22.  
  23. /* data set that contains 5 biggest firms within each category */
  24. data firm2; set firm1(where=(rank<6));
  25. run;

URL: http://support.sas.com/forums/message.jspa?messageID=21083#21083

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.