<?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: Problem with converting character to numeric in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-converting-character-to-numeric/m-p/311356#M67274</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw﻿&lt;/a&gt;&amp;nbsp;Thank you for the answer. I would prefer to read the variable as numeric in the first place, but the problem is that there are 'n.a.'s throughout the file, so when I specify the numeric informat I get all missing values. Is there any way to directly read such variables as numeric?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp﻿&lt;/a&gt;&amp;nbsp;Thank you for the answer. With the translate function I get rid of a thousand separator. Decimals points are separated with a dot in my file.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 14 Nov 2016 09:30:25 GMT</pubDate>
    <dc:creator>chris2377</dc:creator>
    <dc:date>2016-11-14T09:30:25Z</dc:date>
    <item>
      <title>Problem with converting character to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-converting-character-to-numeric/m-p/310715#M67038</link>
      <description>&lt;P&gt;Dear all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have data in a txt file (see attached).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First, I read them to SAS:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile 'mypath\sample.txt'  dlm='09'x dsd lrecl=4096 truncover firstobs=2 termstr=LF;
	input Mark Name	:$20. Country :$20.	Type :$20. Id  var1 :$20.	var2 :$20.	var3 :$20.	var4 :$20.	var5 :$20.
		var6 :$20.	var7 :$20.	var8 :$20.	var9 :$20.	var10 :$20.	var11 :$20.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now, I want to convert var11 to numeric:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	new=compress(translate(var11,"", '"n.a.", ')); /* remove all unnecessary characters*/
	new1=input(new, 12.); /* convert it to numeric*/
	new2=new*1; /* alternative way*/
	keep var11 new:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The problem is that it does not convert the variable. However, when I use var2 instead of var11, everything is fine&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want1;
	set have;
	new=compress(translate(var2,"", '"n.a.", ')); /* remove all unnecessary characters, here I use var2*/
	new1=input(new, 12.); /* convert it to numeric*/
	new2=new*1; /* alternative way*/
	keep var2 new:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Why converting var11 does not work?&lt;/P&gt;</description>
      <pubDate>Thu, 10 Nov 2016 16:44:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-converting-character-to-numeric/m-p/310715#M67038</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2016-11-10T16:44:59Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with converting character to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-converting-character-to-numeric/m-p/310739#M67044</link>
      <description>&lt;P&gt;Once a variable is created is has a data type, numeric of character and will not change.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I am reading a file then I would make sure that the resultant data type when first read is the desired type. instead&amp;nbsp; of Var11 :$20.&lt;/P&gt;
&lt;P&gt;try Var11 : Best20.;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Other wise one of the many-times-repeated-approaches on this forum is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set have (rename=(var11=oldvar11);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; var11 = input(oldvar11,best12.);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; drop oldvar11;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Nov 2016 18:48:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-converting-character-to-numeric/m-p/310739#M67044</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-11-10T18:48:59Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with converting character to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-converting-character-to-numeric/m-p/310770#M67052</link>
      <description>&lt;P&gt;You should revise the documentation of the translate function. Your use will get rid of decimal points. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can either read the var11 field with the proper informat, as suggested by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw﻿&lt;/a&gt;&amp;nbsp;or do&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have(rename=(var11=strVar11));
	var11 = input(strVar11, ?? 20.); /* convert to numeric*/
	drop str:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that 20. and BEST20. are alias informat names.&lt;/P&gt;</description>
      <pubDate>Thu, 10 Nov 2016 20:39:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-converting-character-to-numeric/m-p/310770#M67052</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-11-10T20:39:00Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with converting character to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-converting-character-to-numeric/m-p/310912#M67097</link>
      <description>&lt;PRE&gt;
Maybe you have other non-digit characters.

data want;
	set have;
	new=compress(var11, , 'kd')); /* remove all unnecessary characters*/
	new1=input(new, 12.); /* convert it to numeric*/
.............................

&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Nov 2016 10:58:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-converting-character-to-numeric/m-p/310912#M67097</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-11-11T10:58:09Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with converting character to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-converting-character-to-numeric/m-p/311354#M67272</link>
      <description>&lt;P&gt;Perfect. It worked. I wasn't aware of this extra arguments of the compress function. Thanks&lt;/P&gt;</description>
      <pubDate>Mon, 14 Nov 2016 09:23:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-converting-character-to-numeric/m-p/311354#M67272</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2016-11-14T09:23:15Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with converting character to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-converting-character-to-numeric/m-p/311356#M67274</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw﻿&lt;/a&gt;&amp;nbsp;Thank you for the answer. I would prefer to read the variable as numeric in the first place, but the problem is that there are 'n.a.'s throughout the file, so when I specify the numeric informat I get all missing values. Is there any way to directly read such variables as numeric?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp﻿&lt;/a&gt;&amp;nbsp;Thank you for the answer. With the translate function I get rid of a thousand separator. Decimals points are separated with a dot in my file.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Nov 2016 09:30:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-converting-character-to-numeric/m-p/311356#M67274</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2016-11-14T09:30:25Z</dc:date>
    </item>
  </channel>
</rss>

