BookmarkSubscribeRSS Feed
Anonymous6
Calcite | Level 5

Hi I am working on a problem where I imported an Excel file to SAS

and I need to Sort Salaries of Baseball players and print the top ten largest salaries.

I tried to sort it but it is only sorting the first ten from my dataset from greatest to smallest and it is not getting the top ten greatest salaries from the entire dataset.

 

Here is my code

proc sort data=MLB out=salaries_sorted;
	format Salary dollar12.3;
	by descending Salary descending Year;
	proc print data=MLB (obs=10);
	run;

The results I have right now and the expected results are in pdfs and I have the excel file included too.

4 REPLIES 4
Tom
Super User Tom
Super User

Your code is printing the first 10 observations of the original dataset, not the sorted version.

proc sort data=MLB out=salaries_sorted;
  by descending Salary descending Year;
run;

proc print data=salaries_sorted(obs=10);
  format Salary dollar12.3;
run;
Anonymous6
Calcite | Level 5

@Tom How do I print the sorted dataset?

PaigeMiller
Diamond | Level 26

I recommend not using PROC SORT and instead using PROC RANK, which then lets you specify how ties are handled. If you use PROC SORT, and the tenth and eleventh item are tied, then when you print out the Top 10, you miss the fact that there was a tie and you won't see the 11th item which has the same value as the 10th item.

 

proc rank data=mlb ties=low out=mlb_ranked descending;
    var salaries;
    ranks salaries_ranked;
run;
proc print data=mlb_ranked(where=(salaries_ranked<=10));
  format Salary dollar12.3;
run;

The above is one method of handling ties, the PROC RANK documentation lists the other possible options.

 

 

--
Paige Miller
Al14
SAS Employee

To rephrase what @Tom is saying, the PROC PRINT is where the bug is.  The sort is saving the output into salaries_sort, so the print needs to be of salaries_sort.

 

In the PROC PRINT, change the DATA= option from DATA=MLB to DATA=salaries_sort

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

LIBNAME 101

Follow along as SAS technical trainer Dominique Weatherspoon expertly answers all your questions about SAS Libraries.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1549 views
  • 3 likes
  • 4 in conversation