<?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: sorting char id in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sorting-char-id/m-p/851446#M336540</link>
    <description>&lt;P&gt;With a character var sorting will use the string using one character after the next starting from the left side. If you have two strings like "1&lt;STRONG&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt;1" and "1&lt;STRONG&gt;1&lt;/STRONG&gt;737" then the second character is "1" and "2" respectively and though "11737" will sort before "121".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you read the strings into a numerical variable then they should sort numerical and as you want them to. If that's not the case then either something with your conversion to numeric goes wrong or you don't sort the right table.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can take below code as your "template". The line in comment using a compress() function removes any non-digit character from your source string. That's a bit brute force but should help you to identify rows where you've got more than digits only in your source.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover;
  input c_var $32.;
  n_var=input(c_var,best32.);
/*  n_var2=input(compress(c_var,,'kd'),best32.);*/
datalines;
956257
957515
121
11737
;

proc sort data=have out=want;
  by n_var;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1672275534092.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78913i79F5B707D7D8482C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1672275534092.png" alt="Patrick_0-1672275534092.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 29 Dec 2022 01:00:21 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2022-12-29T01:00:21Z</dc:date>
    <item>
      <title>sorting char id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sorting-char-id/m-p/851408#M336521</link>
      <description>&lt;P&gt;I have a char (32) type id column where I would like to sort but the sorting is not done properly so I created a new numeric_id column with Num (8) type. However, the sorting is still not working. To some point, it is correct but then I have a wrong order. For example:&lt;BR /&gt;&lt;SPAN&gt;....&lt;BR /&gt;956257&lt;BR /&gt;957515&lt;BR /&gt;121&lt;BR /&gt;11737&lt;BR /&gt;....&lt;BR /&gt;&lt;BR /&gt;Why do 121 is coming after than&amp;nbsp;957515 in an ascending sorting and how do I make a proper sorting for this char type id?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Dec 2022 21:27:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sorting-char-id/m-p/851408#M336521</guid>
      <dc:creator>znhnm</dc:creator>
      <dc:date>2022-12-28T21:27:40Z</dc:date>
    </item>
    <item>
      <title>Re: sorting char id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sorting-char-id/m-p/851411#M336523</link>
      <description>&lt;P&gt;Please run this code on your table and post the results. If your data is sorted by ID then it will report this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data = MyTable;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Dec 2022 21:35:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sorting-char-id/m-p/851411#M336523</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2022-12-28T21:35:18Z</dc:date>
    </item>
    <item>
      <title>Re: sorting char id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sorting-char-id/m-p/851423#M336527</link>
      <description>Thank you. &lt;BR /&gt;I have: 	&lt;BR /&gt;Sorted	NO  &lt;BR /&gt;&lt;BR /&gt;as part of the output. How can I understand what is wrong with the sorting?</description>
      <pubDate>Wed, 28 Dec 2022 22:28:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sorting-char-id/m-p/851423#M336527</guid>
      <dc:creator>znhnm</dc:creator>
      <dc:date>2022-12-28T22:28:30Z</dc:date>
    </item>
    <item>
      <title>Re: sorting char id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sorting-char-id/m-p/851429#M336531</link>
      <description>&lt;P&gt;It means your sort didn't work. Try rerunning your PROC SORT step then check your SAS log for any errors. If there are no errors, run the PROC CONTENTS again to confirm the sort order is correct.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Dec 2022 22:37:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sorting-char-id/m-p/851429#M336531</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2022-12-28T22:37:27Z</dc:date>
    </item>
    <item>
      <title>Re: sorting char id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sorting-char-id/m-p/851430#M336532</link>
      <description>&lt;P&gt;One thing to check for is characters in your data that you can't see.&amp;nbsp; The simplest case, for example, would be leading blanks, which would affect the sorted order.&amp;nbsp; A quick check:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data leading_blank;
   set have;
   if varname ne left(varname);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;A second possibility might be other "leading" characters such as a tab character.&amp;nbsp; That's a little harder to check for, but not much:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data something_else;
   set have;
   if varname ne compress(varname,, 'kd');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In this DATA step, the COMPRESS function keeps all digits and removes all other characters.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Dec 2022 22:40:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sorting-char-id/m-p/851430#M336532</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2022-12-28T22:40:19Z</dc:date>
    </item>
    <item>
      <title>Re: sorting char id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sorting-char-id/m-p/851439#M336537</link>
      <description>Hi, thank you so much for the suggestion. I tries it both and I have zero observations in the output dataset. I think it means I don't have characters in my data that I can't see or a leading character, right? Please let me know if you'd have any other suggestions.</description>
      <pubDate>Wed, 28 Dec 2022 23:22:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sorting-char-id/m-p/851439#M336537</guid>
      <dc:creator>znhnm</dc:creator>
      <dc:date>2022-12-28T23:22:22Z</dc:date>
    </item>
    <item>
      <title>Re: sorting char id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sorting-char-id/m-p/851441#M336538</link>
      <description>&lt;P&gt;See my last post regarding rerunning PROC SORT. Please post your complete SAS log including any notes and errors. If there is no evidence we would be just guessing what is going wrong.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Dec 2022 23:48:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sorting-char-id/m-p/851441#M336538</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2022-12-28T23:48:10Z</dc:date>
    </item>
    <item>
      <title>Re: sorting char id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sorting-char-id/m-p/851446#M336540</link>
      <description>&lt;P&gt;With a character var sorting will use the string using one character after the next starting from the left side. If you have two strings like "1&lt;STRONG&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt;1" and "1&lt;STRONG&gt;1&lt;/STRONG&gt;737" then the second character is "1" and "2" respectively and though "11737" will sort before "121".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you read the strings into a numerical variable then they should sort numerical and as you want them to. If that's not the case then either something with your conversion to numeric goes wrong or you don't sort the right table.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can take below code as your "template". The line in comment using a compress() function removes any non-digit character from your source string. That's a bit brute force but should help you to identify rows where you've got more than digits only in your source.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover;
  input c_var $32.;
  n_var=input(c_var,best32.);
/*  n_var2=input(compress(c_var,,'kd'),best32.);*/
datalines;
956257
957515
121
11737
;

proc sort data=have out=want;
  by n_var;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1672275534092.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78913i79F5B707D7D8482C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1672275534092.png" alt="Patrick_0-1672275534092.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Dec 2022 01:00:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sorting-char-id/m-p/851446#M336540</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-12-29T01:00:21Z</dc:date>
    </item>
    <item>
      <title>Re: sorting char id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sorting-char-id/m-p/851510#M336563</link>
      <description>&lt;P&gt;You can do sorting using sortseq option in Proc Sort;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=have out=want sortseq=linguistic(Numeric_Collation=ON);&lt;BR /&gt;by c_var ;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Dec 2022 11:00:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sorting-char-id/m-p/851510#M336563</guid>
      <dc:creator>Aku</dc:creator>
      <dc:date>2022-12-29T11:00:16Z</dc:date>
    </item>
    <item>
      <title>Re: sorting char id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sorting-char-id/m-p/851562#M336585</link>
      <description>&lt;P&gt;Please post the&amp;nbsp;&lt;STRONG&gt;complete(!!!)&lt;/STRONG&gt; log of both the step where you create the numeric variable and the PROC SORT. Use this button to pist the log text:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bildschirmfoto 2020-04-07 um 08.32.59.jpg" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/54552i914D97BE1B0F21E5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Bildschirmfoto 2020-04-07 um 08.32.59.jpg" alt="Bildschirmfoto 2020-04-07 um 08.32.59.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Dec 2022 23:55:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sorting-char-id/m-p/851562#M336585</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-12-29T23:55:58Z</dc:date>
    </item>
  </channel>
</rss>

