i have the following data, the sort results different from the SAS order by sort. Looks like '-' variable sort first in the SAS code - Please help.
Code:
proc sql;
Create table lib.fruits as
select Name, calories,Fat,carb,protein from lib.fruits_data
order by upper(Name) asc;
quit;
result:
fruits | Calories | Fat | Carbs | Protein |
AH-LP Banana | 69 | 0.4 | 17.6 | 0.8 |
AH Banana | 69 | 0.4 | 17.6 | 0.8 |
Cantalope | 27 | 0.2 | 6.5 | 0.7 |
CANTALOPE/GREEN | 27 | 0.2 | 6.5 | 0.7 |
MA-MAS Cherry | 42 | 0.6 | 9.7 | 0.7 |
MAC-Cherry | 42 | 0.6 | 9.7 | 0.7 |
Peach | 36 | 0.1 | 9.4 | 0.6 |
Strawbery | 24 | 0.3 | 5.8 | 0.5 |
Watermelon | 24 | 0.3 | 5.5 | 0.5 |
Expected sort same as excel :
fruits | Calories | Fat | Carbs | Protein |
AH Banana | 69 | 0.4 | 17.6 | 0.8 |
AH-LP Banana | 69 | 0.4 | 17.6 | 0.8 |
CANTALOPE/GREEN | 27 | 0.2 | 6.5 | 0.7 |
Cantalope | 27 | 0.2 | 6.5 | 0.7 |
MAC-Cherry | 42 | 0.6 | 9.7 | 0.7 |
MA-MAS Cherry | 42 | 0.6 | 9.7 | 0.7 |
Peach | 36 | 0.1 | 9.4 | 0.6 |
Strawbery | 24 | 0.3 | 5.8 | 0.5 |
Watermelon | 24 | 0.3 | 5.5 | 0.5 |
Why would Excel sort hyphen before space? Perhaps it is using some strange multi-step logic for sorting?
You can find the ASCII sort order with this code:
data junk; do i= 32 to 255; x= byte(i); output; end; run;
Characters with position less than 32 are generally not used for printed output.
The actual behavior you see might involve a higher order value impersonating a space that appears as a blank in your data.
Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.