<?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: Truncation effect while converting from character to numeric in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Truncation-effect-while-converting-from-character-to-numeric/m-p/942695#M42319</link>
    <description>&lt;P&gt;What is the LENGTH of the strings you are trying to convert?&amp;nbsp; How many leading spaces do they contain?&amp;nbsp; The examples you sight make is sound like the variable is 9 bytes long and the strings are right aligned so that reading only 8 of the 9 bytes will cause the last digit to be ignored.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why did you only try to read the first 8 bytes of the strings?&lt;/P&gt;
&lt;P&gt;Remember that the INPUT() function does not care if the WIDTH used on the INFORMAT is larger than the LENGTH of the string being read.&amp;nbsp; So just use the maximum width that the informat supports.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this instead:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;new_value = input(left(costs), 32.)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 05 Sep 2024 13:33:55 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-09-05T13:33:55Z</dc:date>
    <item>
      <title>Truncation effect while converting from character to numeric</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Truncation-effect-while-converting-from-character-to-numeric/m-p/942694#M42318</link>
      <description>&lt;P&gt;Hi guys,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm trying to convert character values to numeric. They are costs.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm using the following code: new_value = input(costs, 8.). However, original values like 1065 become 106 as well as 44.23 becomes 44.2 and original values = 0 become missing values.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone help me to deal with this issue?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Sep 2024 13:16:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Truncation-effect-while-converting-from-character-to-numeric/m-p/942694#M42318</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2024-09-05T13:16:29Z</dc:date>
    </item>
    <item>
      <title>Re: Truncation effect while converting from character to numeric</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Truncation-effect-while-converting-from-character-to-numeric/m-p/942695#M42319</link>
      <description>&lt;P&gt;What is the LENGTH of the strings you are trying to convert?&amp;nbsp; How many leading spaces do they contain?&amp;nbsp; The examples you sight make is sound like the variable is 9 bytes long and the strings are right aligned so that reading only 8 of the 9 bytes will cause the last digit to be ignored.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why did you only try to read the first 8 bytes of the strings?&lt;/P&gt;
&lt;P&gt;Remember that the INPUT() function does not care if the WIDTH used on the INFORMAT is larger than the LENGTH of the string being read.&amp;nbsp; So just use the maximum width that the informat supports.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this instead:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;new_value = input(left(costs), 32.)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Sep 2024 13:33:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Truncation-effect-while-converting-from-character-to-numeric/m-p/942695#M42319</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-09-05T13:33:55Z</dc:date>
    </item>
    <item>
      <title>Re: Truncation effect while converting from character to numeric</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Truncation-effect-while-converting-from-character-to-numeric/m-p/942696#M42320</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/134532"&gt;@NewUsrStat&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First of all, cost should never be character, it should always be numeric, and this is really what you need to fix. Then the problem goes away. Then you have a much superior process.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I cannot replicate your issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
    length costs $ 8;
    costs='1065';
    output;
    costs='44.23';
    output;
run;

data b;
    set a;
    new_value = input(costs, 8.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since we cannot reproduce your problem from your word description, you need to show us a portion of your actual SAS data set via working SAS data step code (&lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;Examples and instructions&lt;/A&gt;), and the full data step code you are using. Please do this for this question and please do this for all future questions. Thanks.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Sep 2024 13:36:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Truncation-effect-while-converting-from-character-to-numeric/m-p/942696#M42320</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-09-05T13:36:55Z</dc:date>
    </item>
    <item>
      <title>Re: Truncation effect while converting from character to numeric</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Truncation-effect-while-converting-from-character-to-numeric/m-p/942713#M42322</link>
      <description>&lt;P&gt;The only way I can duplicate that behavior for 1065 is if the character value actually has 5 leading blanks in the value:&lt;/P&gt;
&lt;PRE&gt;data example;
   x1='1065';
   x2=' 1065';
   x3='  1065';
   x4='   1065';
   x5='    1065';
   x6='     1065';
   y1= input(x1,8.);
   y2= input(x2,8.);
   y3= input(x3,8.);
   y4= input(x4,8.);
   y5= input(x5,8.);
   y6= input(x6,8.);
run;&lt;/PRE&gt;
&lt;P&gt;If your character data is inconsistent then use the LEFT instruction to remove the leading spaces with the input:&lt;/P&gt;
&lt;PRE&gt;data example;
   x6='     1065';
   y6= input(left(x6),8.);
run;&lt;/PRE&gt;
&lt;P&gt;the 32. informat will work 1065 until you have 29 leading spaces. 29 spaces plus 4 numeric positions = 33 characters to read and 32. informat will only use the first 32.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is an example of "Know thy data".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Sep 2024 14:36:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Truncation-effect-while-converting-from-character-to-numeric/m-p/942713#M42322</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-09-05T14:36:05Z</dc:date>
    </item>
  </channel>
</rss>

