<?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 Character to numeric variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Character-to-numeric-variable/m-p/732334#M228207</link>
    <description>&lt;P&gt;Hello Community,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need some help converting a character variable to a numeric variable. I have provided below an example of the different types of values of the character variable as well as the desired numeric forms of the values for the new variable. The character variable is in the&amp;nbsp;$254. format. Please also note that some of the values of the character variable have nondigits in the form of a dash (e.g., 2-4). For these cases, I would like to write out the last digit of the character value (i.e., the digit to the right of the dash) for the new numeric variable as shown below (e.g., 2-4 --&amp;gt; 4). Any suggestions on how to do this would be much appreciated!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Old_var_character&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;New_var_numeric&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;.5&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0.5&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;5.5&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;5.5&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;10&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;10&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;10.5&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;10.5&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;100&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;100&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;300&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;300&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;1-2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;2-4&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;4&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;2-8&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;8&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;4-8&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;8&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 08 Apr 2021 19:57:27 GMT</pubDate>
    <dc:creator>wj2</dc:creator>
    <dc:date>2021-04-08T19:57:27Z</dc:date>
    <item>
      <title>Character to numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Character-to-numeric-variable/m-p/732334#M228207</link>
      <description>&lt;P&gt;Hello Community,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need some help converting a character variable to a numeric variable. I have provided below an example of the different types of values of the character variable as well as the desired numeric forms of the values for the new variable. The character variable is in the&amp;nbsp;$254. format. Please also note that some of the values of the character variable have nondigits in the form of a dash (e.g., 2-4). For these cases, I would like to write out the last digit of the character value (i.e., the digit to the right of the dash) for the new numeric variable as shown below (e.g., 2-4 --&amp;gt; 4). Any suggestions on how to do this would be much appreciated!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Old_var_character&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;New_var_numeric&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;.5&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0.5&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;5.5&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;5.5&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;10&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;10&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;10.5&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;10.5&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;100&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;100&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;300&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;300&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;1-2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;2-4&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;4&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;2-8&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;8&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;4-8&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;8&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Apr 2021 19:57:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Character-to-numeric-variable/m-p/732334#M228207</guid>
      <dc:creator>wj2</dc:creator>
      <dc:date>2021-04-08T19:57:27Z</dc:date>
    </item>
    <item>
      <title>Re: Character to numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Character-to-numeric-variable/m-p/732345#M228209</link>
      <description>&lt;P&gt;Here's one way that works those sorts of examples:&lt;/P&gt;
&lt;PRE&gt;data junk;
   input var $;
   numvar = input(var,?? 12.);
   if missing(numvar) then numvar = input(scan(var,2,'-'),?? 12.);
datalines;
4-8
;&lt;/PRE&gt;
&lt;P&gt;The input just to have something to work with.&lt;/P&gt;
&lt;P&gt;The first "numvar =" attempts to convert the value as is. The ?? tells SAS to suppress the invalid data messages you get with the dash values. If the result of the first conversion is missing (the result when converting invalid data) then use the Scan function to get the second value and input that to numeric.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have any negative values then you need to provide examples and how you expect them to be used as the - also indicates negative and if you have "-4-8" then the first dash would be a delimiter for the SCAN function and find 4 as the numeric value.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Apr 2021 20:16:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Character-to-numeric-variable/m-p/732345#M228209</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-04-08T20:16:46Z</dc:date>
    </item>
    <item>
      <title>Re: Character to numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Character-to-numeric-variable/m-p/732349#M228211</link>
      <description>&lt;P&gt;Below should do it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input Old_var_character $;

	if find(Old_var_character,"-") then
		New_var_numeric = input(substr(Old_var_character,3,3),best12.);
	else New_var_numeric = input(Old_var_character,best12.);
	datalines;
.5
1
5.5
10
10.5
100
300
1-2
2-4
2-8
4-8
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Apr 2021 20:28:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Character-to-numeric-variable/m-p/732349#M228211</guid>
      <dc:creator>qoit</dc:creator>
      <dc:date>2021-04-08T20:28:48Z</dc:date>
    </item>
  </channel>
</rss>

