Sometimes, we prefer to sort variables manually with a custom sort order rather than alphabetically or in Numerical sequence.
For example, we have a variable called ‘DelayCategory’. It contains three unique “values”: ‘ 1-10 Minutes’, ‘11+ Minutes’ and ‘No Delay’.
You want to reorder this chore list so that all the chores are grouped by DelayCategory, starting with No Delay followed by 1-10 Minutes and then 11+ Minutes. Using Proc Sort to sort by DelayCategory, a list appears in Numerical Sequence followed by alphabetical “sequence”: 1-10 Minutes, 11+ Minutes, No Delay.
See our guide on the Default Sorting Order of Characters in SAS for more information.
The first solution is to create user-defined formats that map the DelayCategory to a number representing the order in which the records appear.
Download the example dataset from here.
value $delayfmt'No Delay' = 1'1-10 Minutes' = 2'11+ Minutes' = 3;run; ```The **$delayfmt** format is created to define the custom sort order.BY variables in most SAS procedures, including PROC SORT, are sorted according to their internal values and not according to the formatted values.To use proc sort, we need an extra step to create the sort variable and use the new variable in Proc Sort By statement.```data flightdelays2;set flightdelays;neworder=put(delaycategory, delayfmt.);run;proc sort data=flightdelays2;by neworder;quit;```SAS offers alternatives to sort a dataset according to formatted values. You can use a proc SQL procedure to sort a dataset by formatted values. [PROC SQL](https://www.9to5sas.com/proc-sql-in-sas/) is similar to the data step and proc sort but is more powerful.```proc sql;select * from flightdelaysorder by put(delaycategory, $delayfmt.);quit;```Here, the **put function **is used to apply the manual sort order and **ORDER BY** is used to sort the variable. This yields sorted data according to the formatted values.## Method "2": Create a custom sort order using SQL Case when the Statement```proc sql;select * from flightdelaysorder by case when delaycategory = 'No Delay' then 1when delaycategory = '1-10 Minutes' then 2when delaycategory = '11+ Minutes' then 3 end;quit;```This solution uses an in-line case when statements are used as an ORDER BY column. The CASE expression to map the *delaycategory* variable.You can download this entire code from [here](https://github.com/subhroster/9to5sas_Codes/blob/06ad1f93b2f3e9c8119f1783599a3451a5f6f4ba/SAS%20programs/custum_sort_order.sas).So, this was our side of Creating a custom sort order in SAS. We hope that you must have found it useful.Moreover, if you have any other suggestions, suggest them in the comment section.We will take those lists in our further blog post.Thanks for reading!Please subscribe to our mailing list for weekly updates. You can also find us on [Instagram](https://www.instagram.com/9to5sas/) and [Facebook](https://www.facebook.com/9to5sas/).export const _frontmatter = {"title":"Creating a custom sort order in SAS","slug":"custom-sort-order-in-sas","date":"2021-08-29T10:37:42","modified":"2022-11-10T10:25:26","excerpt":"Sometimes, we prefer to sort variables manually with a custom sort order rather than alphabetically or Numerical sequence.The first solution is to create user-defined formats that map the DelayCategory to a number that represents the order in which the records will appear.","author":"Subhro","authorSlug":"subhroster","categories":["SAS PROGRAMS"],"tags":["custom sort in sas"],"wordpressId":10808,"wordpressLink":"https://www.9to5sas.com/custom-sort-order-in-sas/","featuredImage":10914,"type":"post"}