<?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: PROC SORT with categorical variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451219#M113723</link>
    <description>&lt;P&gt;Can you not do this in the first place?&lt;/P&gt;
&lt;P&gt;Replace the categorical variables with a number that would sort accordingly and have a&amp;nbsp;format that shows the names instead.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm not aware of another method to do this in any language tbh.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 04 Apr 2018 17:42:55 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-04-04T17:42:55Z</dc:date>
    <item>
      <title>PROC SORT with categorical variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451178#M113705</link>
      <description>&lt;P&gt;I would like to sort a dataset using PROC SORT based on a character column.&amp;nbsp; Something equivalent to:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
  SELECT *
  FROM foo
  ORDER BY
    CASE some_text_column
    WHEN 'category a' THEN 1
    WHEN 'category b' THEN 2
    WHEN 'category c' THEN 3
  END;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Apr 2018 16:03:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451178#M113705</guid>
      <dc:creator>tomcmacdonald</dc:creator>
      <dc:date>2018-04-04T16:03:42Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SORT with categorical variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451188#M113707</link>
      <description>&lt;P&gt;Since you mention PROC SORT, have you tried it? It should get the job done very easily. Does it work? Why or why not?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is the real question not about sorting but assigning values 1 2 3??&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 16:22:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451188#M113707</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-04-04T16:22:41Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SORT with categorical variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451189#M113708</link>
      <description>&lt;P&gt;I'm just using dummy values.&amp;nbsp; The values won't be alphabetical.&amp;nbsp; I have tried it and it sorts alphabetically which is incorrect.&amp;nbsp; I'm guessing I'm going to have make a temporary column in a previous DATA STEP, sort it, then remove the column in another DATA STEP.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 16:24:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451189#M113708</guid>
      <dc:creator>tomcmacdonald</dc:creator>
      <dc:date>2018-04-04T16:24:51Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SORT with categorical variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451195#M113710</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/142145"&gt;@tomcmacdonald&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I'm just using dummy values.&amp;nbsp; The values won't be alphabetical.&amp;nbsp; I have tried it and it sorts alphabetically which is incorrect.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;There is not enough information here for me to understand what is not correct, and what result you want.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 16:35:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451195#M113710</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-04-04T16:35:34Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SORT with categorical variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451201#M113713</link>
      <description>&lt;P&gt;This will take at least two steps (just can't be done in one step).&amp;nbsp; To get a grasp on the problem ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you know what 1, 2, and 3 are, or does the program have to figure out positionally what the names of the fields are?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is FOO the name of the data set that contains both the data to be sorted as well as SOME_TEXT_COLUMN?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Wherever it comes from, is SOME_TEXT_COLUMN constant across records (or is there only one record to begin with)?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;*********** EDITED:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your example makes things much clearer.&amp;nbsp; You could combine a couple of steps:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=_1 out=foo (keep=a b);&lt;/P&gt;
&lt;P&gt;by dummy;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the DATA step approach will likely take longer to run than the original.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 16:58:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451201#M113713</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-04-04T16:58:54Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SORT with categorical variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451203#M113714</link>
      <description>&lt;P&gt;Just seeing if there's a less wordy way of doing the equivalent PROC SQL operation.&amp;nbsp; I think I have a solution.&amp;nbsp; This is the test dataset:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data foo;
	input a b $;
	datalines;
1 apple
2 banana
3 pear
4 peach
5 orange
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the PROC SQL sort solution:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	select *
	from foo 
	order by
		case b
		when 'peach' then 1
		when 'orange' then 2
		when 'apple' then 3
		when 'pear' then 4
		when 'banana' then 5;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the wordy DATA STEP and PROC SORT solution:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _1;
	set foo;
	if b = 'peach' then dummy = 1;
	if b = 'orange' then dummy = 2;
	if b = 'apple' then dummy = 3;
	if b = 'pear' then dummy = 4;
	if b = 'banana' then dummy = 5;
run;

proc sort data=_1; by dummy; run;

data foo(keep=a b);
	set _1;
run;

proc delete data=_1; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Apr 2018 16:52:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451203#M113714</guid>
      <dc:creator>tomcmacdonald</dc:creator>
      <dc:date>2018-04-04T16:52:37Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SORT with categorical variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451204#M113715</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/142145"&gt;@tomcmacdonald&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I'm just using dummy values.&amp;nbsp; The values won't be alphabetical.&amp;nbsp; I have tried it and it sorts alphabetically which is incorrect.&amp;nbsp; I'm guessing I'm going to have make a temporary column in a previous DATA STEP, sort it, then remove the column in another DATA STEP.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Show the code you used that resulted in an incorrect sort order for numeric variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also indicate what format the numeric variable(s) that sort incorrectly may have. Note that if you have a format permanently applied to&amp;nbsp;the variables the displayed value may not match the underlying numeric value.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 16:54:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451204#M113715</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-04-04T16:54:58Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SORT with categorical variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451206#M113716</link>
      <description>&lt;P&gt;may be a hash data step equivalent?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data foo;
	input a b $;
	datalines;
1 apple
2 banana
3 pear
4 peach
5 orange
;
run;

data _null_;
if _n_=1 then do;
  dcl hash H (ordered: "A") ;
   h.definekey  ("dummy") ;
   h.definedata ("a", "b") ;
   h.definedone () ;
end;
set foo end=last;
if b = 'peach' then dummy = 1;
else if b = 'orange' then dummy = 2;
else if b = 'apple' then dummy = 3;
else if b = 'pear' then dummy = 4;
else if b = 'banana' then dummy = 5;
h.replace();
if last then h.output(dataset:'want');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Apr 2018 16:59:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451206#M113716</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-04-04T16:59:28Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SORT with categorical variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451208#M113717</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=_1 out=foo(drop=dummy); by dummy; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Apr 2018 17:02:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451208#M113717</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-04-04T17:02:49Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SORT with categorical variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451219#M113723</link>
      <description>&lt;P&gt;Can you not do this in the first place?&lt;/P&gt;
&lt;P&gt;Replace the categorical variables with a number that would sort accordingly and have a&amp;nbsp;format that shows the names instead.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm not aware of another method to do this in any language tbh.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 17:42:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451219#M113723</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-04-04T17:42:55Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SORT with categorical variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451227#M113725</link>
      <description>Wouldn't that be more complicated because now you need to go into PROC FORMAT and define a format that will only be used once?</description>
      <pubDate>Wed, 04 Apr 2018 17:57:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451227#M113725</guid>
      <dc:creator>tomcmacdonald</dc:creator>
      <dc:date>2018-04-04T17:57:38Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SORT with categorical variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451228#M113726</link>
      <description>&lt;P&gt;It speaks to the structure of your data in the first place.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Doing it from the start means your data is smaller and easier to manage.&lt;/P&gt;
&lt;P&gt;It means when I'm coding, I'm typing less characters.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC FORMAT can be driven from a data set so the code is centralized.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In many ways, it's just better programming approach overall.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC FORMAT is also very fast for lookups and I have more control over my reports.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Obviously my opinion, but I work with SAS/R/Python and this is one of the best approaches in SAS, IMO&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 17:59:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451228#M113726</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-04-04T17:59:52Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SORT with categorical variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451348#M113761</link>
      <description>&lt;P&gt;In addition to &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;'s format comments it may be one of the easier ways to reorder data depending on what you are actually using the data for. Note that reporting procedures Report and Tabulate explicitly have order options for variables and include Formatted as one of the options.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 23:17:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451348#M113761</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-04-04T23:17:40Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SORT with categorical variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451478#M113834</link>
      <description>&lt;P&gt;Since ORDER BY is executed at last.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe you should try .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;PROC&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;SQL&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  &lt;SPAN class="token statement"&gt;SELECT&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;*,&lt;BR /&gt;CASE some_text_column WHEN &lt;SPAN class="token string"&gt;'category a'&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;THEN&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;1&lt;/SPAN&gt; WHEN &lt;SPAN class="token string"&gt;'category b'&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;THEN&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;2&lt;/SPAN&gt; WHEN &lt;SPAN class="token string"&gt;'category c'&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;THEN&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;3&lt;/SPAN&gt; END AS XXXX&lt;/SPAN&gt;
  &lt;SPAN class="token keyword"&gt;FROM&lt;/SPAN&gt; foo
  ORDER &lt;SPAN class="token statement"&gt;BY&lt;/SPAN&gt;
  XXXX  &lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;QUIT&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Apr 2018 12:18:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SORT-with-categorical-variables/m-p/451478#M113834</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-04-05T12:18:10Z</dc:date>
    </item>
  </channel>
</rss>

