<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: how do you manually control sort order for levels in a categorical variable? in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/how-do-you-manually-control-sort-order-for-levels-in-a/m-p/639000#M78208</link>
    <description>&lt;P&gt;SQL approach&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sort.png" style="width: 167px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/38177i3920D40FC0C7377B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="sort.png" alt="sort.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 10 Apr 2020 16:18:20 GMT</pubDate>
    <dc:creator>ghosh</dc:creator>
    <dc:date>2020-04-10T16:18:20Z</dc:date>
    <item>
      <title>how do you manually control sort order for levels in a categorical variable?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-do-you-manually-control-sort-order-for-levels-in-a/m-p/638857#M78194</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there an easy way to sort levels in a categorical variable manually?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So if I have 4 seasons - Fall, Winter, Spring, and Summer.&amp;nbsp; I want to view results of analysis in this order, not the alphabetical order of Fall, Spring, Summer, Winter.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Garrett&lt;/P&gt;</description>
      <pubDate>Fri, 10 Apr 2020 02:49:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-do-you-manually-control-sort-order-for-levels-in-a/m-p/638857#M78194</guid>
      <dc:creator>gtjoeckel</dc:creator>
      <dc:date>2020-04-10T02:49:23Z</dc:date>
    </item>
    <item>
      <title>Re: how do you manually control sort order for levels in a categorical variable?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-do-you-manually-control-sort-order-for-levels-in-a/m-p/638861#M78195</link>
      <description>&lt;P&gt;There are two basic approaches:&lt;/P&gt;
&lt;P&gt;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: "&amp;nbsp; &amp;nbsp;&amp;nbsp; Winter", with 5 leading spaces will sort before "&amp;nbsp;&amp;nbsp;&amp;nbsp; 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&amp;nbsp; results from output without actual data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The numeric value with format is often the most reliable if you have more than a small&amp;nbsp; number of values.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Apr 2020 03:53:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-do-you-manually-control-sort-order-for-levels-in-a/m-p/638861#M78195</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-04-10T03:53:21Z</dc:date>
    </item>
    <item>
      <title>Re: how do you manually control sort order for levels in a categorical variable?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-do-you-manually-control-sort-order-for-levels-in-a/m-p/639000#M78208</link>
      <description>&lt;P&gt;SQL approach&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sort.png" style="width: 167px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/38177i3920D40FC0C7377B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="sort.png" alt="sort.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Apr 2020 16:18:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-do-you-manually-control-sort-order-for-levels-in-a/m-p/639000#M78208</guid>
      <dc:creator>ghosh</dc:creator>
      <dc:date>2020-04-10T16:18:20Z</dc:date>
    </item>
  </channel>
</rss>

