<?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: add leading zero for a variable with both char and num in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/add-leading-zero-for-a-variable-with-both-char-and-num/m-p/367127#M87369</link>
    <description>You have numeric values and character values mixed in one variable, which means that the data type of the variable is char, and in sorting SAS does not see the numeric values as numbers, but sort them as strings. you can get the wanted result by splitting the variable in 2, the first a string variable with (or without) the letters, and the second a numeric variable with the numbers, and then sort by both.
there are many methods for doing that, one is the following;

data have;
input value $char8.;
datalines;
ab1
8
111
d388
9
456
23
abc9
457
ac2
;
data wrk; set have;
	length c $8 n $8;
	c = prxchange('s/(\D*)(\d*)/$1/',-1,trim(value));
	n = input(prxchange('s/(\D*)(\d*)/$2/',-1,trim(value)),8.);
run;
proc sort data=wrk out=want(drop=c n); by c n;
run;

data _null_; set want; put value;
run;

Result:
8
9
23
111
456
457
ab1
abc9
ac2
d388</description>
    <pubDate>Wed, 14 Jun 2017 19:43:41 GMT</pubDate>
    <dc:creator>ErikLund_Jensen</dc:creator>
    <dc:date>2017-06-14T19:43:41Z</dc:date>
    <item>
      <title>add leading zero for a variable with both char and num</title>
      <link>https://communities.sas.com/t5/SAS-Programming/add-leading-zero-for-a-variable-with-both-char-and-num/m-p/367114#M87364</link>
      <description>&lt;P&gt;I have saw many questions similar but after read those posts I still need to ask.&lt;/P&gt;
&lt;P&gt;My question:&lt;/P&gt;
&lt;P&gt;there is a variable has around 100 values, from 8, 9, 111, 23, 456,457, ab1, ac2, abc9, d388, q34, s789, etc&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to sort it to a way that I feel comfortable with, that is, it should sfirst start with, 8, then 9, then 23... then ab1, ac2, then d388...&lt;/P&gt;
&lt;P&gt;the rule would be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if all are numbers, the order should be from small to big, that is, 8 should be listed before 111;&lt;/P&gt;
&lt;P&gt;those with letters will be after the numbers, and in the order with alphabit order, that is, after these numbers, ab1 should be first,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but even I sort it, it still lists 111 before 8. I then realize it might be because sas considers 1 is small then 8.&lt;/P&gt;
&lt;P&gt;I thought I could add leading zero, I do not know how to do this becasue it is a mixture of numeric and alpha.&lt;/P&gt;
&lt;P&gt;Thank you in advance for your advice and time!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2017 19:15:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/add-leading-zero-for-a-variable-with-both-char-and-num/m-p/367114#M87364</guid>
      <dc:creator>Bal23</dc:creator>
      <dc:date>2017-06-14T19:15:33Z</dc:date>
    </item>
    <item>
      <title>Re: add leading zero for a variable with both char and num</title>
      <link>https://communities.sas.com/t5/SAS-Programming/add-leading-zero-for-a-variable-with-both-char-and-num/m-p/367120#M87365</link>
      <description>&lt;P&gt;You could use the ANYALPHA function to determine if the text has alphabetic characters or not. If there are no alphabetic characters, then you can concatenate zeros in front of the numeric.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2017 19:30:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/add-leading-zero-for-a-variable-with-both-char-and-num/m-p/367120#M87365</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2017-06-14T19:30:18Z</dc:date>
    </item>
    <item>
      <title>Re: add leading zero for a variable with both char and num</title>
      <link>https://communities.sas.com/t5/SAS-Programming/add-leading-zero-for-a-variable-with-both-char-and-num/m-p/367125#M87367</link>
      <description>&lt;P&gt;Proc Sort has built-in features just what you want:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards dlm=',';
input var$ @@;
cards;
 8, 9, 111, 23, 456,457, ab1, ac2, abc9, d388, q34, s789,etc
 ;

 proc sort data=have out=want sortseq=linguistic(NUMERIC_COLLATION=on) ;
 by var;
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 14 Jun 2017 19:34:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/add-leading-zero-for-a-variable-with-both-char-and-num/m-p/367125#M87367</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2017-06-14T19:34:19Z</dc:date>
    </item>
    <item>
      <title>Re: add leading zero for a variable with both char and num</title>
      <link>https://communities.sas.com/t5/SAS-Programming/add-leading-zero-for-a-variable-with-both-char-and-num/m-p/367126#M87368</link>
      <description>&lt;P&gt;If it's acceptable to add a variable just for sorting purposes, this might be easier than adding leading zeros:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if input(varname, ??8.) &amp;gt; . then sorting_var =&amp;nbsp;right(varname);&lt;/P&gt;
&lt;P&gt;else sorting_var = varname;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then sort by SORTING_VAR instead of by the original variable name.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2017 19:34:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/add-leading-zero-for-a-variable-with-both-char-and-num/m-p/367126#M87368</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-06-14T19:34:52Z</dc:date>
    </item>
    <item>
      <title>Re: add leading zero for a variable with both char and num</title>
      <link>https://communities.sas.com/t5/SAS-Programming/add-leading-zero-for-a-variable-with-both-char-and-num/m-p/367127#M87369</link>
      <description>You have numeric values and character values mixed in one variable, which means that the data type of the variable is char, and in sorting SAS does not see the numeric values as numbers, but sort them as strings. you can get the wanted result by splitting the variable in 2, the first a string variable with (or without) the letters, and the second a numeric variable with the numbers, and then sort by both.
there are many methods for doing that, one is the following;

data have;
input value $char8.;
datalines;
ab1
8
111
d388
9
456
23
abc9
457
ac2
;
data wrk; set have;
	length c $8 n $8;
	c = prxchange('s/(\D*)(\d*)/$1/',-1,trim(value));
	n = input(prxchange('s/(\D*)(\d*)/$2/',-1,trim(value)),8.);
run;
proc sort data=wrk out=want(drop=c n); by c n;
run;

data _null_; set want; put value;
run;

Result:
8
9
23
111
456
457
ab1
abc9
ac2
d388</description>
      <pubDate>Wed, 14 Jun 2017 19:43:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/add-leading-zero-for-a-variable-with-both-char-and-num/m-p/367127#M87369</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2017-06-14T19:43:41Z</dc:date>
    </item>
    <item>
      <title>Re: add leading zero for a variable with both char and num</title>
      <link>https://communities.sas.com/t5/SAS-Programming/add-leading-zero-for-a-variable-with-both-char-and-num/m-p/367128#M87370</link>
      <description>&lt;P&gt;Sorry the formatting was so bad.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; have;&lt;/P&gt;
&lt;P&gt;input value $char8.;&lt;/P&gt;
&lt;P&gt;datalines;&lt;/P&gt;
&lt;P&gt;ab1&lt;/P&gt;
&lt;P&gt;8&lt;/P&gt;
&lt;P&gt;111&lt;/P&gt;
&lt;P&gt;d388&lt;/P&gt;
&lt;P&gt;9&lt;/P&gt;
&lt;P&gt;456&lt;/P&gt;
&lt;P&gt;23&lt;/P&gt;
&lt;P&gt;abc9&lt;/P&gt;
&lt;P&gt;457&lt;/P&gt;
&lt;P&gt;ac2&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; wrk; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; length c $&lt;STRONG&gt;8&lt;/STRONG&gt; n $&lt;STRONG&gt;8&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c = prxchange('s/(\D*)(\d*)/$1/',-&lt;STRONG&gt;1&lt;/STRONG&gt;,trim(value));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; n = input(prxchange('s/(\D*)(\d*)/$2/',-&lt;STRONG&gt;1&lt;/STRONG&gt;,trim(value)),&lt;STRONG&gt;8.&lt;/STRONG&gt;);&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt; &lt;STRONG&gt;sort&lt;/STRONG&gt; data=wrk out=want(drop=c n); by c n;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; _null_; set want; put value;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;8&lt;BR /&gt;9&lt;BR /&gt;23&lt;BR /&gt;111&lt;BR /&gt;456&lt;BR /&gt;457&lt;BR /&gt;ab1&lt;BR /&gt;abc9&lt;BR /&gt;ac2&lt;BR /&gt;d388&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2017 19:46:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/add-leading-zero-for-a-variable-with-both-char-and-num/m-p/367128#M87370</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2017-06-14T19:46:05Z</dc:date>
    </item>
    <item>
      <title>Re: add leading zero for a variable with both char and num</title>
      <link>https://communities.sas.com/t5/SAS-Programming/add-leading-zero-for-a-variable-with-both-char-and-num/m-p/367130#M87371</link>
      <description>&lt;P&gt;Look up the PROC Sort options SORTSEQ with LINGUISTIC options. Learning that proc sort has these options may save you many problems in the future so I'm not providing an example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that AFAIK only Proc Sort will create the order properly (though if you have mixed case letters there are still issues). Other procedures that produce output may or maynot maintain your desired order.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2017 19:50:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/add-leading-zero-for-a-variable-with-both-char-and-num/m-p/367130#M87371</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-06-14T19:50:18Z</dc:date>
    </item>
  </channel>
</rss>

