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 2025: Call for Content

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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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