BookmarkSubscribeRSS Feed
gtjoeckel
Fluorite | Level 6

Hello,

 

Is there an easy way to sort levels in a categorical variable manually?

 

So if I have 4 seasons - Fall, Winter, Spring, and Summer.  I want to view results of analysis in this order, not the alphabetical order of Fall, Spring, Summer, Winter.

 

Thanks,

Garrett

2 REPLIES 2
ballardw
Super User

There are two basic approaches:

1) Create values that sort as desired. Sometimes you can accomplish this by inserting leading spaces. Then the order typically is kept if done right. Example: "     Winter", with 5 leading spaces will sort before "    Spring" with 4 leading spaces. Comparisons for sorting are done from left to right and spaces come before letters. I used multiple spaces your use of Fall is going to require several as well. Generally most of the output will default to left justifying text and you won't see the spaces in output. Which has caused some issues with debugging  results from output without actual data.

 

2) create a numeric variable and assign a custom format to display the text desired. Depending on the procedure then either the numeric order would be used by default or you sort the data and use an Order=data option. Sort of depends to some extent exactly which procedure is creating output what options are available where. So you might have to provide a bit more of an example.

 

For some reporting procedures you also have the option of creating a format with a specific order and using an option such as Preloadfmt. But that has other requirements involved.

 

The numeric value with format is often the most reliable if you have more than a small  number of values.

ghosh
Barite | Level 11

SQL approach

 

data mydata;
input var1 var2 Season $;
cards;
831 3 Spring
442 5 Summer
664 8 Fall 
245 1 Winter
;
 proc sql;
  create table newdata as  
    select var1, var2, Season
       from (select var1, var2, Season,
               case
                  when Season = 'Fall' then 1
                  when Season = 'Winter' then 2
                  when Season = 'Spring' then 3
                  when Season = 'Summer' then 4
                  else .
               end as Sorder
               from mydata)
       order by Sorder;
quit;
proc print;
var season;
run;

sort.png

 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 2626 views
  • 1 like
  • 3 in conversation