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.
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;
@Tom How do I print the sorted dataset?
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.
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.