<?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: Sort a string that includes both character and numeric code in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sort-a-string-that-includes-both-character-and-numeric-code/m-p/683698#M207106</link>
    <description>&lt;P&gt;Hi, Tom&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Currently, all my values are combination of 'one alphabet'+'4 digits'.&lt;/P&gt;&lt;P&gt;The above code is working, except that SAS is equalling 'J9000' and 'J9070': sometimes I get results J9000,j9070. Other times, I get results J9070,J9000.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, I believe the problem is in "CALL SORTC": it is not correctly sorting the 4 digits.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you have a possible solution?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
    <pubDate>Mon, 14 Sep 2020 15:49:04 GMT</pubDate>
    <dc:creator>weijiawang</dc:creator>
    <dc:date>2020-09-14T15:49:04Z</dc:date>
    <item>
      <title>Sort a string that includes both character and numeric code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-a-string-that-includes-both-character-and-numeric-code/m-p/683012#M206807</link>
      <description>&lt;P&gt;Hi, I have a dataset like this:&lt;/P&gt;&lt;P&gt;id regimen&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&lt;SPAN&gt;J0000, J0001, J2000&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;2 &lt;SPAN&gt;J0000, J0001, J2000, J1345&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;3 &lt;SPAN&gt;J2000, J1345, Q1111&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;4 &lt;SPAN&gt;J2000, Q1111, J1345, Q1112&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I sort each line, base on alphabet and numeric order?&lt;/P&gt;&lt;P&gt;The result be like:&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&lt;SPAN&gt;J0000, J0001, J2000&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;2 &lt;SPAN&gt;J0000, J0001, J1345, J2000&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;3 &lt;SPAN&gt;J1345, &lt;/SPAN&gt;&lt;SPAN&gt;J2000, Q1111&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;4 &lt;SPAN&gt;J1345, &lt;/SPAN&gt;&lt;SPAN&gt;J2000, Q1111, Q1112&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a code like this:&lt;/P&gt;&lt;PRE&gt;DATA XX;&lt;BR /&gt;	SET XX;&lt;BR /&gt;	LENGTH newname $100;&lt;BR /&gt;	ARRAY t(100) $ _TEMPORARY_;&lt;BR /&gt;	CALL MISSING(OF t(*));&lt;BR /&gt;	DO _N_=1 to LENGTH(STRIP(regimen));&lt;BR /&gt;		t(_N_)=CHAR(regimen,_N_);&lt;BR /&gt;	END;&lt;BR /&gt;	CALL SORTC(of t(*));&lt;BR /&gt;	newname=CATS(of t(*));&lt;BR /&gt;RUN;&lt;/PRE&gt;&lt;P&gt;But above code will combine by line all character and numeric together, while I still need the code to be ordered by X1234.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks smart people!!!&lt;/P&gt;</description>
      <pubDate>Thu, 10 Sep 2020 17:55:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-a-string-that-includes-both-character-and-numeric-code/m-p/683012#M206807</guid>
      <dc:creator>weijiawang</dc:creator>
      <dc:date>2020-09-10T17:55:59Z</dc:date>
    </item>
    <item>
      <title>Re: Sort a string that includes both character and numeric code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-a-string-that-includes-both-character-and-numeric-code/m-p/683052#M206821</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/346340"&gt;@weijiawang&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi, I have a dataset like this:&lt;/P&gt;
&lt;P&gt;id regimen&lt;/P&gt;
&lt;P&gt;1&amp;nbsp;&lt;SPAN&gt;J0000, J0001, J2000&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;2 &lt;SPAN&gt;J0000, J0001, J2000, J1345&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;3 &lt;SPAN&gt;J2000, J1345, Q1111&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;4 &lt;SPAN&gt;J2000, Q1111, J1345, Q1112&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I sort each line, base on alphabet and numeric order?&lt;/P&gt;
&lt;P&gt;The result be like:&lt;/P&gt;
&lt;P&gt;1&amp;nbsp;&lt;SPAN&gt;J0000, J0001, J2000&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;2 &lt;SPAN&gt;J0000, J0001, J1345, J2000&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;3 &lt;SPAN&gt;J1345, &lt;/SPAN&gt;&lt;SPAN&gt;J2000, Q1111&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;4 &lt;SPAN&gt;J1345, &lt;/SPAN&gt;&lt;SPAN&gt;J2000, Q1111, Q1112&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a code like this:&lt;/P&gt;
&lt;PRE&gt;DATA XX;&lt;BR /&gt;	SET XX;&lt;BR /&gt;	LENGTH newname $100;&lt;BR /&gt;	ARRAY t(100) $ _TEMPORARY_;&lt;BR /&gt;	CALL MISSING(OF t(*));&lt;BR /&gt;	DO _N_=1 to LENGTH(STRIP(regimen));&lt;BR /&gt;		t(_N_)=CHAR(regimen,_N_);&lt;BR /&gt;	END;&lt;BR /&gt;	CALL SORTC(of t(*));&lt;BR /&gt;	newname=CATS(of t(*));&lt;BR /&gt;RUN;&lt;/PRE&gt;
&lt;P&gt;But above code will combine by line all character and numeric together, while I still need the code to be ordered by X1234.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks smart people!!!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Since you do not show anything that is X1234 I do not understand how it should be "ordered by X1234".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't understand why you are using CHAR since it pulls one character at a time.&lt;/P&gt;
&lt;P&gt;I suspect that this might get closer:&lt;/P&gt;
&lt;PRE&gt;DATA XX;
	SET XX;
	LENGTH newname $100;
	ARRAY t(100) $5 _TEMPORARY_;
	CALL MISSING(OF t(*));
	DO N=1 to Countw(regimen,',');
		t(N)=Scan(regimen,_N_,',');
	END;
	CALL SORTC(of t(*));
	newname=CATS(of t(*));
       drop n;
RUN;&lt;/PRE&gt;
&lt;P&gt;Since _n_ is a&amp;nbsp; SAS automatic variable and often useful for other things I don't use it for random loop counters.&lt;/P&gt;</description>
      <pubDate>Thu, 10 Sep 2020 19:25:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-a-string-that-includes-both-character-and-numeric-code/m-p/683052#M206821</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-09-10T19:25:35Z</dc:date>
    </item>
    <item>
      <title>Re: Sort a string that includes both character and numeric code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-a-string-that-includes-both-character-and-numeric-code/m-p/683057#M206824</link>
      <description>&lt;P&gt;Transpose to a long format by using an array, then use proc sort:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
do i = 1 to countw(regimen,',');
  reg = scan(regimen,i,',');
  output;
end;
keep id reg;
run;

proc sort data=want;
by id reg;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 10 Sep 2020 19:39:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-a-string-that-includes-both-character-and-numeric-code/m-p/683057#M206824</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-09-10T19:39:31Z</dc:date>
    </item>
    <item>
      <title>Re: Sort a string that includes both character and numeric code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-a-string-that-includes-both-character-and-numeric-code/m-p/683648#M207094</link>
      <description>&lt;P&gt;Using "X1234", I meant that I need to sort on this order: first on alphabet, then on numeric after the alphabet.&lt;/P&gt;&lt;P&gt;For example: J0000 should come first, then J0001, then J0002....then Q0001, Q0002....then T0001, T0002.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much for your help!!!&lt;/P&gt;</description>
      <pubDate>Mon, 14 Sep 2020 13:22:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-a-string-that-includes-both-character-and-numeric-code/m-p/683648#M207094</guid>
      <dc:creator>weijiawang</dc:creator>
      <dc:date>2020-09-14T13:22:58Z</dc:date>
    </item>
    <item>
      <title>Re: Sort a string that includes both character and numeric code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-a-string-that-includes-both-character-and-numeric-code/m-p/683649#M207095</link>
      <description>&lt;P&gt;Sorry that I was not clear about the sorting requirement. But here it is.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using "X1234", I meant that I need to sort on this order: first on alphabet, then on numeric after the alphabet.&lt;/P&gt;&lt;P&gt;For example: J0000 should come first, then J0001, then J0002....then Q0001, Q0002....then T0001, T0002.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, I am not sure if transpose the table will help?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much for your help!!!&lt;/P&gt;</description>
      <pubDate>Mon, 14 Sep 2020 13:23:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-a-string-that-includes-both-character-and-numeric-code/m-p/683649#M207095</guid>
      <dc:creator>weijiawang</dc:creator>
      <dc:date>2020-09-14T13:23:58Z</dc:date>
    </item>
    <item>
      <title>Re: Sort a string that includes both character and numeric code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-a-string-that-includes-both-character-and-numeric-code/m-p/683653#M207096</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/346340"&gt;@weijiawang&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Sorry that I was not clear about the sorting requirement. But here it is.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using "X1234", I meant that I need to sort on this order: first on alphabet, then on numeric after the alphabet.&lt;/P&gt;
&lt;P&gt;For example: J0000 should come first, then J0001, then J0002....then Q0001, Q0002....then T0001, T0002.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, I am not sure if transpose the table will help?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you so much for your help!!!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It does not look like you need to do anything.&amp;nbsp; The character values will sort properly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The digits used in storing numbers in strings will sort lexicographical in the same order as they would as numbers.&amp;nbsp; So '0001' &amp;lt; '0002' &amp;lt; '0003'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You would only have an issue if you wanted to treat 'J59' &amp;lt; 'J100'.&amp;nbsp; As long as you have the leading zeros, like in your examples you will not have any issues with the sorting of the text strings.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Sep 2020 13:29:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-a-string-that-includes-both-character-and-numeric-code/m-p/683653#M207096</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-09-14T13:29:12Z</dc:date>
    </item>
    <item>
      <title>Re: Sort a string that includes both character and numeric code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-a-string-that-includes-both-character-and-numeric-code/m-p/683660#M207097</link>
      <description>&lt;P&gt;See Maxim 4. &lt;STRONG&gt;Try It&lt;/STRONG&gt;.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Sep 2020 13:43:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-a-string-that-includes-both-character-and-numeric-code/m-p/683660#M207097</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-09-14T13:43:48Z</dc:date>
    </item>
    <item>
      <title>Re: Sort a string that includes both character and numeric code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-a-string-that-includes-both-character-and-numeric-code/m-p/683698#M207106</link>
      <description>&lt;P&gt;Hi, Tom&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Currently, all my values are combination of 'one alphabet'+'4 digits'.&lt;/P&gt;&lt;P&gt;The above code is working, except that SAS is equalling 'J9000' and 'J9070': sometimes I get results J9000,j9070. Other times, I get results J9070,J9000.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, I believe the problem is in "CALL SORTC": it is not correctly sorting the 4 digits.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you have a possible solution?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Mon, 14 Sep 2020 15:49:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-a-string-that-includes-both-character-and-numeric-code/m-p/683698#M207106</guid>
      <dc:creator>weijiawang</dc:creator>
      <dc:date>2020-09-14T15:49:04Z</dc:date>
    </item>
    <item>
      <title>Re: Sort a string that includes both character and numeric code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-a-string-that-includes-both-character-and-numeric-code/m-p/683700#M207108</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/346340"&gt;@weijiawang&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi, Tom&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Currently, all my values are combination of 'one alphabet'+'4 digits'.&lt;/P&gt;
&lt;P&gt;The above code is working, except that SAS is equalling 'J9000' and 'J9070': sometimes I get results J9000,j9070. Other times, I get results J9070,J9000.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, I believe the problem is in "CALL SORTC": it is not correctly sorting the 4 digits.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you have a possible solution?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;CALL SORTC does not care whether the characters are letters or digits.&amp;nbsp; It will NOT treat two different strings as equal.&amp;nbsp; It will sort all lower case letters after all uppercase letters.&amp;nbsp; The order is based on the ASCII codes used to store the characters.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want the order to be case insensitive you will need to either change the way you are sorting or convert the values to use consistent case.&amp;nbsp; For example by using the UPCASE() function to modify the values stored in the variables.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Sep 2020 17:55:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-a-string-that-includes-both-character-and-numeric-code/m-p/683700#M207108</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-09-14T17:55:07Z</dc:date>
    </item>
    <item>
      <title>Re: Sort a string that includes both character and numeric code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-a-string-that-includes-both-character-and-numeric-code/m-p/683707#M207112</link>
      <description>&lt;P&gt;So few points to check.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Make sure to eliminate leading spaces when parsing the words from the list&lt;/LI&gt;
&lt;LI&gt;If you want them to ignore case then force the letters to uppercase&lt;/LI&gt;
&lt;LI&gt;Make sure to clear your temporary array, values are retained unless cleared.&amp;nbsp; Or use non-temporary variables.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile cards truncover;
  input id regimen $80.;
cards;
1 J0000, J0001, J2000
2 J0000, J0001, J2000, J1345
3 J2000, J1345, Q1111
4 J2000, Q1111, J1345, Q1112
5 J1070, j1000
;;;;

data want;
  set have;
  array words [100] $10 _temporary_;
  call missing(of words[*]);
  do index=1 to countw(regimen,',');
    words[index]=upcase(strip(scan(regimen,index,',')));
  end;
  call sortc(of words[*]);
  length regimen2 $80 ;
  regimen2=catx(', ',of words[*]);
  drop index;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;Obs    id    regimen                       regimen2

 1      1    J0000, J0001, J2000           J0000, J0001, J2000
 2      2    J0000, J0001, J2000, J1345    J0000, J0001, J1345, J2000
 3      3    J2000, J1345, Q1111           J1345, J2000, Q1111
 4      4    J2000, Q1111, J1345, Q1112    J1345, J2000, Q1111, Q1112
 5      5    J1070, j1000                  J1000, J1070
&lt;/PRE&gt;
&lt;P&gt;Without the UPCASE() the last record looks like this instead:&lt;/P&gt;
&lt;PRE&gt; 5      5    J1070, j1000                  J1070, j1000
&lt;/PRE&gt;</description>
      <pubDate>Mon, 14 Sep 2020 16:28:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-a-string-that-includes-both-character-and-numeric-code/m-p/683707#M207112</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-09-14T16:28:58Z</dc:date>
    </item>
  </channel>
</rss>

