<?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 I convert ordinal characters to numeric in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-convert-ordinal-characters-to-numeric/m-p/619498#M181904</link>
    <description>&lt;P&gt;This worked like a charm, thank you!&lt;/P&gt;</description>
    <pubDate>Thu, 23 Jan 2020 13:44:21 GMT</pubDate>
    <dc:creator>sms39</dc:creator>
    <dc:date>2020-01-23T13:44:21Z</dc:date>
    <item>
      <title>How do I convert ordinal characters to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-convert-ordinal-characters-to-numeric/m-p/619353#M181815</link>
      <description>&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm sure this is answered somewhere else but I can't seem to find it, so I'll ask here. I'm new to SAS and am having trouble converting a char data variable to a numeric one. The variable in question is a list of data all formatted as a mix of character: 002AB0001, 002AB0002, 002AB0003 and so on). I am hoping to change it to only the last 4 digits (ex: 0001, 0002, 0003). Any suggestions on the proper code for this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried a few commands, including the one below, but am not sure how to specify to SAS that I'm interested in the last 4.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you all&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;failed attempt:&lt;/P&gt;&lt;P&gt;data new_set;&lt;/P&gt;&lt;P&gt;old_var = "002AB0001";&lt;/P&gt;&lt;P&gt;numeric_var = input(old_var,4.);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jan 2020 22:34:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-convert-ordinal-characters-to-numeric/m-p/619353#M181815</guid>
      <dc:creator>sms39</dc:creator>
      <dc:date>2020-01-22T22:34:01Z</dc:date>
    </item>
    <item>
      <title>Re: How do I convert ordinal characters to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-convert-ordinal-characters-to-numeric/m-p/619361#M181821</link>
      <description>SUBSTR() will get the last four characters. If it's mixed length it's a bit harder, because you need to count backwards. &lt;BR /&gt;&lt;BR /&gt;partial_var = substr(old_var, 6);&lt;BR /&gt;numeric_var = input(partial_var, 4.);&lt;BR /&gt;&lt;BR /&gt;if the length of the variable does change you can use LENGTH() as well.&lt;BR /&gt;&lt;BR /&gt;partial_var = substr(old_var, length(old_var)-4);&lt;BR /&gt;&lt;BR /&gt;Leading zeros will be dropped in a numeric variable though.</description>
      <pubDate>Wed, 22 Jan 2020 23:02:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-convert-ordinal-characters-to-numeric/m-p/619361#M181821</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-01-22T23:02:32Z</dc:date>
    </item>
    <item>
      <title>Re: How do I convert ordinal characters to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-convert-ordinal-characters-to-numeric/m-p/619380#M181830</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/305943"&gt;@sms39&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm sure this is answered somewhere else but I can't seem to find it, so I'll ask here. I'm new to SAS and am having trouble converting a char data variable to a numeric one. The variable in question is a list of data all formatted as a mix of character: 002AB0001, 002AB0002, 002AB0003 and so on). I am hoping to change it to only the last 4 digits (ex: 0001, 0002, 0003). Any suggestions on the proper code for this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've tried a few commands, including the one below, but am not sure how to specify to SAS that I'm interested in the last 4.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you all&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;failed attempt:&lt;/P&gt;
&lt;P&gt;data new_set;&lt;/P&gt;
&lt;P&gt;old_var = "002AB0001";&lt;/P&gt;
&lt;P&gt;numeric_var = input(old_var,4.);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You are asking SAS to read the FIRST 4 characters as try to convert to a number.&amp;nbsp; But the letter A is not valid.&lt;/P&gt;
&lt;P&gt;To get the LAST 4 characters use SUBSTR() function first.&amp;nbsp; If you know you always want from the 5th character onwards use can just use on argument to SUBSTR().&amp;nbsp; The width you use for the INFORMAT doesn't really matter if you want to read the whole string. The maximum width that normal numeric informat will read is 32 bytes.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;numeric_var = input(substr(old_var,5),32.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the prefix length varies then count backwards from the end. Subtracting 3 from the length will get the last 4 characters.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;numeric_var = input(substr(old_var,length(old_var)-3),32.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the numeric suffix length varies then start where it begins instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;numeric_var = input(substr(old_var,findc(old_var,,'dk',-length(old_var))),32.);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Jan 2020 00:16:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-convert-ordinal-characters-to-numeric/m-p/619380#M181830</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-01-23T00:16:33Z</dc:date>
    </item>
    <item>
      <title>Re: How do I convert ordinal characters to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-convert-ordinal-characters-to-numeric/m-p/619479#M181889</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new_set;
old_var = "002AB0001";
numeric_var = input(scan(old_var,-1,,'kd'),best.);
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Jan 2020 12:49:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-convert-ordinal-characters-to-numeric/m-p/619479#M181889</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-01-23T12:49:00Z</dc:date>
    </item>
    <item>
      <title>Re: How do I convert ordinal characters to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-convert-ordinal-characters-to-numeric/m-p/619498#M181904</link>
      <description>&lt;P&gt;This worked like a charm, thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jan 2020 13:44:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-convert-ordinal-characters-to-numeric/m-p/619498#M181904</guid>
      <dc:creator>sms39</dc:creator>
      <dc:date>2020-01-23T13:44:21Z</dc:date>
    </item>
  </channel>
</rss>

