<?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: Converting character value to numeric, retain decimals in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Converting-character-value-to-numeric-retain-decimals/m-p/777614#M247422</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/60547"&gt;@sbxkoenk&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All three conversions below end up with 5 decimals.&lt;/P&gt;
&lt;P&gt;Not sure why it "stops" at 5 decimals.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
    format CharVar $50. /* value_num1 value_num2 20.14 */;
    CharVar="7663229328,8184132475796";
    Value_num1 = input(tranwrd(CharVar,",","."), best32.); put Value_num1= 32.15;
    Value_num2 = tranwrd(CharVar,",",".")*1;               put Value_num2= 32.15;
    Value_num3 = input(CharVar,commax32.);                 put Value_num3= 32.15;
run;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Will investigate further tomorrow.&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Simple, too many significant digits. Lots of precision discussions on this and other forums. You get about 15 significant digits in a Windows environment.&lt;/P&gt;
&lt;P&gt;Move the comma and you get more decimals as the significant digits in the integer portion are fewer and allow more in the decimal (though still not 15 depending on where you place the decimal/comma in the original string).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;11   data test;
12       format CharVar $50. /* value_num1 value_num2 20.14 */;
13       CharVar="76632,293288184132475796";
14       Value_num1 = input(tranwrd(CharVar,",","."), best32.); put Value_num1= 32.15;
15       Value_num2 = tranwrd(CharVar,",",".")*1;               put Value_num2= 32.15;
16       Value_num3 = input(CharVar,commax32.);                 put Value_num3= 32.15;
17   run;

NOTE: Character values have been converted to numeric values at the places given by:
      (Line):(Column).
      15:18
Value_num1=76632.293288184100000
Value_num2=76632.293288184100000
Value_num3=76632.293288184100000
&lt;/PRE&gt;
&lt;P&gt;Count the digits in the about output, when you get to 15 everything after the "841" is 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 31 Oct 2021 23:16:21 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2021-10-31T23:16:21Z</dc:date>
    <item>
      <title>Converting character value to numeric, retain decimals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-character-value-to-numeric-retain-decimals/m-p/777595#M247410</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a database with values saved as character. The number of decimal points can vary between observation I.e. both 10,132 and 6,254252 could be found.&lt;/P&gt;&lt;P&gt;I want to convert these character values to a numeric value while retaining all decimal points. Using input(char,best.) yields me 1 decimal points. Simply multiplying the character string with 1 yields me 5. Exactly what is going on here, and how can i adjust the code to include all/more decimals?&lt;/P&gt;&lt;P&gt;See example below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data test;
    format CharVar $50. value_num1 value_num2 20.14;
    CharVar="7663229328,8184132475796";
    Value_num1 = input(tranwrd(CharVar,",","."), best.);
    Value_num2 = tranwrd(CharVar,",",".")*1;
run;

CharVar= 7663229328,8184132475796
Value_num1=7663229328.800000000
Value_num2=7663229328.818410000&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 31 Oct 2021 17:49:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-character-value-to-numeric-retain-decimals/m-p/777595#M247410</guid>
      <dc:creator>Shawnty</dc:creator>
      <dc:date>2021-10-31T17:49:46Z</dc:date>
    </item>
    <item>
      <title>Re: Converting character value to numeric, retain decimals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-character-value-to-numeric-retain-decimals/m-p/777598#M247412</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/161756"&gt;@Shawnty&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS stores numerical values as double precision floating point values. That means that any value is truncated to 16 digits (the largest exact number represented is&amp;nbsp;9007199254740992), and the decimal point can be placed anywhere (that's what floating point means).&amp;nbsp;So your test value cannot be represented better than&amp;nbsp;7663229328.81841, the remaining digits after the decimal point will always be lost.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
    CharVar="7663229328,8184132475796";
	Value_num3 = input(CharVar,numx26.13);
        put Value_num3 25.13;
	put Value_num3 20.8;
	put Value_num3 17.5;
run;

 7663229328.8184100000000
 7663229328.81841000
 7663229328.81841
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Oct 2021 18:39:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-character-value-to-numeric-retain-decimals/m-p/777598#M247412</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2021-10-31T18:39:01Z</dc:date>
    </item>
    <item>
      <title>Re: Converting character value to numeric, retain decimals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-character-value-to-numeric-retain-decimals/m-p/777599#M247413</link>
      <description>&lt;P&gt;First, what's going on here ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The BEST family of informats has a default width.&amp;nbsp; Evidently, if you don't specify a width such as best8 or best14, it will read 12 characters.&amp;nbsp; So that's why you end up with 1 decimal place.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So&amp;nbsp; your second approach would be more accurate.&amp;nbsp; However, SAS cannot store the full number exactly.&amp;nbsp; SAS stores numbers in 8 bytes.&amp;nbsp; Internally, that allows it to store roughly 15 digits, plus an indication of positive or negative, plus an indication of where the decimal point should go.&amp;nbsp; If you want the full value stored as numeric, you would have to choose some other plan.&amp;nbsp; For example, you could keep the variable as character.&amp;nbsp; Or you could split the variable into two pieces (digits before the decimal point, and digits after the decimal point).&amp;nbsp; There is no way that SAS can accurately store the full value in one numeric variable.&lt;/P&gt;</description>
      <pubDate>Sun, 31 Oct 2021 18:43:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-character-value-to-numeric-retain-decimals/m-p/777599#M247413</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-10-31T18:43:12Z</dc:date>
    </item>
    <item>
      <title>Re: Converting character value to numeric, retain decimals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-character-value-to-numeric-retain-decimals/m-p/777600#M247414</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All three conversions below end up with 5 decimals.&lt;/P&gt;
&lt;P&gt;Not sure why it "stops" at 5 decimals.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
    format CharVar $50. /* value_num1 value_num2 20.14 */;
    CharVar="7663229328,8184132475796";
    Value_num1 = input(tranwrd(CharVar,",","."), best32.); put Value_num1= 32.15;
    Value_num2 = tranwrd(CharVar,",",".")*1;               put Value_num2= 32.15;
    Value_num3 = input(CharVar,commax32.);                 put Value_num3= 32.15;
run;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Will investigate further tomorrow.&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Sun, 31 Oct 2021 18:55:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-character-value-to-numeric-retain-decimals/m-p/777600#M247414</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-10-31T18:55:45Z</dc:date>
    </item>
    <item>
      <title>Re: Converting character value to numeric, retain decimals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-character-value-to-numeric-retain-decimals/m-p/777604#M247417</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/161756"&gt;@Shawnty&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just to add a few details to what has been said already: Assuming SAS 9.4 under Windows or Unix, the internal 64-bit floating-point representation of your example value&amp;nbsp;&lt;FONT face="courier new,courier" size="4"&gt;"7663229328,8184132475796"&lt;/FONT&gt;, read with the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/leforinforref/n0yvn3aj60qyjmn11fg3jbrbvs5c.htm" target="_blank" rel="noopener"&gt;NUMX32. informat&lt;/A&gt;, is&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;&lt;FONT color="#008000"&gt;0&lt;/FONT&gt;&lt;FONT color="#FF0000"&gt;10000011111&lt;/FONT&gt;&lt;FONT color="#3366FF"&gt;11001000110000111001100110010000&lt;/FONT&gt;&lt;STRONG&gt;11010001100000111000&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;(use&amp;nbsp;&lt;FONT face="courier new,courier"&gt;put(input(CharVar,numx32.),binary64.)&lt;/FONT&gt; to get this and see&amp;nbsp;&lt;A href="https://documentation.sas.com/?docsetId=lrcon&amp;amp;docsetTarget=p0ji1unv6thm0dn1gp4t01a1u0g6.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank" rel="noopener"&gt;Numerical Accuracy in SAS Software&lt;/A&gt; for the documentation).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The green zero represents the positive sign of the number.&lt;/LI&gt;
&lt;LI&gt;The 11 red bits -- the (mathematical) binary representation of 1023+&lt;U&gt;32&lt;/U&gt;=1055 -- store the order of magnitude, i.e., "where the decimal point is," more precisely: that the absolute value of the number is contained in the interval [2**&lt;U&gt;32&lt;/U&gt;, 2**33).&lt;/LI&gt;
&lt;LI&gt;The 32 blue bits are needed to store the integer part of the number, &lt;FONT face="courier new,courier" size="4"&gt;7663229328&lt;/FONT&gt;, whose binary representation is&amp;nbsp;&lt;STRONG&gt;&lt;FONT face="courier new,courier" size="4" color="#3366FF"&gt;&lt;FONT color="#00FF00"&gt;1&lt;/FONT&gt;11001000110000111001100110010000&lt;/FONT&gt;&lt;/STRONG&gt;&amp;nbsp;(3&lt;STRONG&gt;3&lt;/STRONG&gt; digits, the first digit is the so-called "implied bit," omitted in the internal representation).&lt;/LI&gt;
&lt;LI&gt;Only the 20 black bits are left to store a binary approximation of the fractional part, &lt;FONT face="courier new,courier" size="4"&gt;0.8184132475796&lt;/FONT&gt;, whose &lt;EM&gt;exact&lt;/EM&gt; binary representation is infinitely (!) long and starts with&lt;BR /&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;0.&lt;STRONG&gt;11010001100000111000&lt;/STRONG&gt;011111010100111101111011000001000&lt;FONT color="#999999"&gt;00011000010000101100011001011010111001111001110...&lt;/FONT&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So it must be rounded (&lt;EM&gt;down&lt;/EM&gt; in this case). The least significant bit of the internal representation of your number has a place value of &lt;FONT face="courier new,courier" size="4"&gt;2**-20 =&amp;nbsp;0.00000095367431640625 ≈ 1E-6&lt;/FONT&gt;. Converted back to the decimal system, the internal representation and its "neighbors" (i.e., one less and one more in the last bit, hence approx. &lt;FONT face="courier new,courier"&gt;-/+ 1E-6&lt;/FONT&gt;) correspond to&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;&lt;FONT color="#999999"&gt;7663229328.81841&lt;EM&gt;182708740234375&lt;/EM&gt;&lt;/FONT&gt;
7663229328.81841&lt;EM&gt;278076171875000&lt;/EM&gt;
&lt;FONT color="#999999"&gt;7663229328.81841&lt;EM&gt;373443603515625&lt;/EM&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You see that SAS chose the closest possible value to your number, given the limited space available (64 bits). The sixth decimal place (16th decimal digit overall) is not &lt;EM&gt;displayed&lt;/EM&gt; with standard numeric SAS formats. That's why you got&amp;nbsp;&lt;FONT size="4"&gt;&lt;FONT face="courier new,courier"&gt;7663229328.81841&lt;/FONT&gt;, &lt;FONT size="3"&gt;not the more complete&lt;/FONT&gt;&amp;nbsp;&lt;FONT face="courier new,courier"&gt;7663229328.81841&lt;U&gt;3&lt;/U&gt;&lt;/FONT&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With 24 additional bits, i.e., 44 for the fractional part, SAS could have achieved a sufficient precision:&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;&lt;FONT color="#999999"&gt;7663229328.818413247579&lt;EM&gt;54225788125768303871154785156250&lt;/EM&gt;&lt;/FONT&gt;
7663229328.818413247579&lt;EM&gt;59910130011849105358123779296875&lt;/EM&gt;
&lt;FONT color="#999999"&gt;7663229328.818413247579&lt;EM&gt;65594471897929906845092773437500&lt;/EM&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Storing the fractional part in a separate numeric variable (the good idea suggested by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;) would provide those additional bits and even a few more (a total of 52 mantissa bits plus the implied bit). Indeed, the internal representation of &lt;FONT face="courier new,courier" size="4"&gt;&lt;EM&gt;0&lt;/EM&gt;.8184132475796&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;001111111110&lt;STRONG&gt;1010001100000111000&lt;/STRONG&gt;011111010100111101111011000001000&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;(note the "missing" implied bit and the additional 33 bits at the end compared to what we had when we included the space-consuming integer part)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;corresponds to&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;0.818413247579599&lt;EM&gt;98947853819117881357669830322265625
&lt;/EM&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;which is a better approximation of the exact decimal value.&lt;/P&gt;</description>
      <pubDate>Sun, 31 Oct 2021 21:46:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-character-value-to-numeric-retain-decimals/m-p/777604#M247417</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-10-31T21:46:47Z</dc:date>
    </item>
    <item>
      <title>Re: Converting character value to numeric, retain decimals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-character-value-to-numeric-retain-decimals/m-p/777614#M247422</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/60547"&gt;@sbxkoenk&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All three conversions below end up with 5 decimals.&lt;/P&gt;
&lt;P&gt;Not sure why it "stops" at 5 decimals.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
    format CharVar $50. /* value_num1 value_num2 20.14 */;
    CharVar="7663229328,8184132475796";
    Value_num1 = input(tranwrd(CharVar,",","."), best32.); put Value_num1= 32.15;
    Value_num2 = tranwrd(CharVar,",",".")*1;               put Value_num2= 32.15;
    Value_num3 = input(CharVar,commax32.);                 put Value_num3= 32.15;
run;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Will investigate further tomorrow.&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Simple, too many significant digits. Lots of precision discussions on this and other forums. You get about 15 significant digits in a Windows environment.&lt;/P&gt;
&lt;P&gt;Move the comma and you get more decimals as the significant digits in the integer portion are fewer and allow more in the decimal (though still not 15 depending on where you place the decimal/comma in the original string).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;11   data test;
12       format CharVar $50. /* value_num1 value_num2 20.14 */;
13       CharVar="76632,293288184132475796";
14       Value_num1 = input(tranwrd(CharVar,",","."), best32.); put Value_num1= 32.15;
15       Value_num2 = tranwrd(CharVar,",",".")*1;               put Value_num2= 32.15;
16       Value_num3 = input(CharVar,commax32.);                 put Value_num3= 32.15;
17   run;

NOTE: Character values have been converted to numeric values at the places given by:
      (Line):(Column).
      15:18
Value_num1=76632.293288184100000
Value_num2=76632.293288184100000
Value_num3=76632.293288184100000
&lt;/PRE&gt;
&lt;P&gt;Count the digits in the about output, when you get to 15 everything after the "841" is 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Oct 2021 23:16:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-character-value-to-numeric-retain-decimals/m-p/777614#M247422</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-10-31T23:16:21Z</dc:date>
    </item>
  </channel>
</rss>

