<?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: Deal with rounding problem while exporting to CSV in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Deal-with-rounding-problem-while-exporting-to-CSV/m-p/913815#M83291</link>
    <description>&lt;P&gt;1) Start your own thread. As the creator of the thread you can then indicate a correct solution to your particular problem for others to find on the forum.&lt;/P&gt;
&lt;P&gt;2) Reference this (or the other dozen or so related threads)&lt;/P&gt;
&lt;P&gt;3) Provide code of how you create the CSV file, or at least very clearly describe how.&lt;/P&gt;
&lt;P&gt;4) Provide example data, best in the form of working data step that replicates your issue.&lt;/P&gt;
&lt;P&gt;5) Clearly describe what you expect as the result in the target CSV file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And in very serious note, &lt;STRONG&gt;never &lt;/STRONG&gt;trust what a spreadsheet displays when opening a CSV file. Depending on the particular software and values intended values can change types. And if you allow the spreadsheet software to save the csv file may permanently change the actual values.&lt;/P&gt;</description>
    <pubDate>Wed, 31 Jan 2024 15:38:47 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2024-01-31T15:38:47Z</dc:date>
    <item>
      <title>Deal with rounding problem while exporting to CSV</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Deal-with-rounding-problem-while-exporting-to-CSV/m-p/739015#M80512</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;When export to CSV I have following problem.&lt;/P&gt;&lt;P&gt;My original dataset in SAS before export (Ratio is fixed to $13.9):&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Region&lt;/TD&gt;&lt;TD&gt;Type&lt;/TD&gt;&lt;TD&gt;Ratio&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;CAN&lt;/TD&gt;&lt;TD&gt;Banana&lt;/TD&gt;&lt;TD&gt;0.234236354&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;CAN&lt;/TD&gt;&lt;TD&gt;Apple&lt;/TD&gt;&lt;TD&gt;0.706008361&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;CAN&lt;/TD&gt;&lt;TD&gt;Orange&lt;/TD&gt;&lt;TD&gt;0.059755285&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;After exporting to CSV I get:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Region&lt;/TD&gt;&lt;TD&gt;Type&lt;/TD&gt;&lt;TD&gt;Ratio&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;CAN&lt;/TD&gt;&lt;TD&gt;Banana&lt;/TD&gt;&lt;TD&gt;0.2342363540200000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;CAN&lt;/TD&gt;&lt;TD&gt;Apple&lt;/TD&gt;&lt;TD&gt;0.7060083610000980&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;CAN&lt;/TD&gt;&lt;TD&gt;Orange&lt;/TD&gt;&lt;TD&gt;0.0597552850000087&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the original dataset I know the sum is perfectly 1 but after exporting the dataset to csv the sum become&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;1.0000000000201100&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;DIV class="mceNonEditable lia-copypaste-placeholder"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;I saw someone mention about floating point in excel CSV before but is there a way that we can avoid the rounding error when export to csv in SAS? Any suggestion will be appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried "ODS csv file=" and "proc export data=" two methods.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 May 2021 18:07:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Deal-with-rounding-problem-while-exporting-to-CSV/m-p/739015#M80512</guid>
      <dc:creator>notepadd123</dc:creator>
      <dc:date>2021-05-04T18:07:27Z</dc:date>
    </item>
    <item>
      <title>Re: Deal with rounding problem while exporting to CSV</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Deal-with-rounding-problem-while-exporting-to-CSV/m-p/739027#M80513</link>
      <description>&lt;P&gt;Numerical precision issues. &lt;BR /&gt;Here's the SAS reference:&lt;BR /&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lrcon/p0ji1unv6thm0dn1gp4t01a1u0g6.htm" target="_blank" rel="noopener"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lrcon/p0ji1unv6thm0dn1gp4t01a1u0g6.htm&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;And here's the Excel reference:&lt;BR /&gt;&lt;A href="https://www.microsoft.com/en-us/microsoft-365/blog/2008/04/10/understanding-floating-point-precision-aka-why-does-excel-give-me-seemingly-wrong-answers/#:~:text=The%20precision%20of%20a%20number,within%2015%20digits%20of%20precision" target="_blank" rel="noopener"&gt;https://www.microsoft.com/en-us/microsoft-365/blog/2008/04/10/understanding-floating-point-precision-aka-why-does-excel-give-me-seemingly-wrong-answers/#:~:text=The%20precision%20of%20a%20number,within%2015%20digits%20of%20precision&lt;/A&gt;.&lt;BR /&gt;&lt;BR /&gt;&lt;EM&gt;&lt;STRONG&gt;The precision of a number varies depending on the size of the mantissa. Excel can store numbers from 1.79769313486232E308 to 2.2250738585072E-308; however, &lt;FONT color="#FF0000"&gt;it can only do so within 15 digits of precision&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/EM&gt;&lt;BR /&gt;&lt;BR /&gt;The 15 digit precision is common in many applications.&lt;/P&gt;</description>
      <pubDate>Tue, 04 May 2021 18:34:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Deal-with-rounding-problem-while-exporting-to-CSV/m-p/739027#M80513</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-05-04T18:34:04Z</dc:date>
    </item>
    <item>
      <title>Re: Deal with rounding problem while exporting to CSV</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Deal-with-rounding-problem-while-exporting-to-CSV/m-p/739040#M80514</link>
      <description>&lt;P&gt;What does "(Ratio is fixed to $13.9)" mean? If a variable is character, which would be a $ type format, then DECIMALS have no place in the format. If your value has a 13.9 format assigned the value in SAS could very well hold decimals you don't think are there but would be when the value is "exported". If you calculated the ratio value and assigned a format to the result this is extremely likely to be the case.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;data forexporttest;
   x= 123;
   y= 57;
   ratio = y/x;
   put "with 13.9 format " ratio= 13.9;
   put "with best18. format " ratio= best18.;
run;

proc export data=forexporttest
   outfile='exporttest.csv' replace;
run;&lt;/PRE&gt;
&lt;P&gt;The Put statements show:&lt;/P&gt;
&lt;PRE&gt;with 13.9 format ratio=0.463414634
with best18. format ratio=0.46341463414634
&lt;/PRE&gt;
&lt;P&gt;The resulting csv file in my Proc Export shows:&lt;/P&gt;
&lt;PRE&gt;x,y,ratio
123,57,0.4634146341
&lt;/PRE&gt;
&lt;P&gt;And why does the value for ratio look like: 0.4634146341&lt;/P&gt;
&lt;P&gt;Because when I examine the LOG for proc export we see a generated data step:&lt;/P&gt;
&lt;PRE&gt;655   /**********************************************************************
656   *   PRODUCT:   SAS
657   *   VERSION:   9.4
658   *   CREATOR:   External File Interface
659   *   DATE:      03MAY21
660   *   DESC:      Generated SAS Datastep Code
661   *   TEMPLATE SOURCE:  (None Specified.)
662   ***********************************************************************/
663      data _null_;
664      %let _EFIERR_ = 0; /* set the ERROR detection macro variable */
665      %let _EFIREC_ = 0;     /* clear export record count macro variable */
666      file 'exporttest.csv' delimiter=',' DSD DROPOVER lrecl=32767;
667      if _n_ = 1 then        /* write column names or labels */
668       do;
669         put
670            "x"
671         ','
672            "y"
673         ','
674            "ratio"
675         ;
676       end;
677     set  FOREXPORTTEST   end=EFIEOD;
678         format x best12. ;
679         format y best12. ;
680         format ratio best12. ;
681       do;
682         EFIOUT + 1;
683         put x @;
684         put y @;
685         put ratio ;
686         ;
687       end;
688      if _ERROR_ then call symputx('_EFIERR_',1);  /* set ERROR detection macro variable */
689      if EFIEOD then call symputx('_EFIREC_',EFIOUT);
690      run;
&lt;/PRE&gt;
&lt;P&gt;Which used the format BEST12. for the export. Which truncates/rounds the value of the repeating decimal value I generated in the data step. How you export the data could well be setting or using a different format that shows more digits than you looked at in SAS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And your&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;In the original dataset I know the sum is perfectly 1 but after exporting the dataset to csv the sum become&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;
&lt;BLOCKQUOTE&gt;1.0000000000201100&lt;/BLOCKQUOTE&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;statement of "sum is perfectly 1" is almost not true, but the format that was used in SAS to look at the value likely rounded the value, especially if it only displayed 9 decimals.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You probably should also show just how you exported your data to CSV.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 May 2021 19:23:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Deal-with-rounding-problem-while-exporting-to-CSV/m-p/739040#M80514</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-05-04T19:23:27Z</dc:date>
    </item>
    <item>
      <title>Re: Deal with rounding problem while exporting to CSV</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Deal-with-rounding-problem-while-exporting-to-CSV/m-p/739868#M80524</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/377090"&gt;@notepadd123&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;I don't know what your environment was, but I&amp;nbsp; don't have the issue you are having.&lt;BR /&gt;I have tested the following code on SASOnDemand for Analytics.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Creating a dataset with your values */
data test;
input @1 Region $	@5 Type  $	@11 Ratio;
datalines;
CAN Banana 0.234236354
CAN Apple  0.706008361
CAN Orange 0.059755285
;;
/*Exporting to csv*/
proc export data=test outfile="/home/mypath/test.csv";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;My result is as follows. I have attached the csv I generated.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2021-05-07 3.25.21 PM.png" style="width: 192px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/59139i3E0394583970262A/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2021-05-07 3.25.21 PM.png" alt="Screenshot 2021-05-07 3.25.21 PM.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I have opened the csv on a spreadsheet (google sheets, I am using Chromebook). It looks OK&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2021-05-07 3.28.35 PM.png" style="width: 258px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/59140iE9F9543B4067D19D/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2021-05-07 3.28.35 PM.png" alt="Screenshot 2021-05-07 3.28.35 PM.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;If you are using excel ,then you may try changing the number format in your excel worksheet.&lt;/P&gt;</description>
      <pubDate>Fri, 07 May 2021 19:34:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Deal-with-rounding-problem-while-exporting-to-CSV/m-p/739868#M80524</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2021-05-07T19:34:33Z</dc:date>
    </item>
    <item>
      <title>Re: Deal with rounding problem while exporting to CSV</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Deal-with-rounding-problem-while-exporting-to-CSV/m-p/739901#M80526</link>
      <description>&lt;P&gt;SAS stored numbers a floating point.&amp;nbsp; The format attached to a number is how you want it DISPLAYED, it has nothing to do with how the values are stored.&amp;nbsp; Decimal fractions cannot be exactly represented in binary floating point numbers (other than combinations of powers or two, like 0.5, 0.25, 0.125 ....)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You seemed to be saying your values are formatted to 9 decimal places.&amp;nbsp; And the difference in the SUM you are showing is zero for more than 9 decimal places.&lt;/P&gt;
&lt;PRE&gt;* 1.000 000 000 020 110 0 ;
&lt;/PRE&gt;
&lt;P&gt;So what is your issue?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to debug more you need to provide more information.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What are the exact values in the SAS dataset before writing it to the CSV file?&amp;nbsp; To see the exact values use the HEX16. format as that will show the actual hex codes from the 64 bit floating point number that SAS uses to store the number.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What are the exact value in the CSV file? Show the TEXT of the CSV file, do NOT touch the CSV file with Excel or any other spreadsheet program.&amp;nbsp; Open it in a text editor or just type it to the terminal window.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How did you create the CSV file?&amp;nbsp; Did you use some tool like PROC EXPORT? Some GUI push button tool in Enterprise Guide or SAS/Studio user interface? Or did you write it yourself using a data step with the DSD option on the FILE statement?&amp;nbsp; What format was attached to the variable in question?&lt;/P&gt;</description>
      <pubDate>Fri, 07 May 2021 21:00:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Deal-with-rounding-problem-while-exporting-to-CSV/m-p/739901#M80526</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-07T21:00:28Z</dc:date>
    </item>
    <item>
      <title>Re: Deal with rounding problem while exporting to CSV</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Deal-with-rounding-problem-while-exporting-to-CSV/m-p/913808#M83290</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have a very similar issue. Essentially, the tables on the screen in SAS are, say, displayed as zeroes (0), yet whilst exporting them to CSV files (in case of our recipients the delimiter is a tilde, yet I checked also another basic delimiters like a pipe or semicolon) I see very small numbers saved in CSV as, say, "2E-9" instead of zeroes.&lt;/P&gt;&lt;P&gt;That format of figures (written as exponentials) can't be accepted by further inbound systems to which this CSV needs to be uploaded.&lt;/P&gt;&lt;P&gt;How to make sure that figures in the CSV file will be saved as figures to, say, six decimal places, as I rounded those up in my SAS table?&lt;/P&gt;&lt;P&gt;Many thanks!&lt;/P&gt;&lt;P&gt;Marzena&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2024 15:01:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Deal-with-rounding-problem-while-exporting-to-CSV/m-p/913808#M83290</guid>
      <dc:creator>MarzenaKurowska</dc:creator>
      <dc:date>2024-01-31T15:01:54Z</dc:date>
    </item>
    <item>
      <title>Re: Deal with rounding problem while exporting to CSV</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Deal-with-rounding-problem-while-exporting-to-CSV/m-p/913815#M83291</link>
      <description>&lt;P&gt;1) Start your own thread. As the creator of the thread you can then indicate a correct solution to your particular problem for others to find on the forum.&lt;/P&gt;
&lt;P&gt;2) Reference this (or the other dozen or so related threads)&lt;/P&gt;
&lt;P&gt;3) Provide code of how you create the CSV file, or at least very clearly describe how.&lt;/P&gt;
&lt;P&gt;4) Provide example data, best in the form of working data step that replicates your issue.&lt;/P&gt;
&lt;P&gt;5) Clearly describe what you expect as the result in the target CSV file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And in very serious note, &lt;STRONG&gt;never &lt;/STRONG&gt;trust what a spreadsheet displays when opening a CSV file. Depending on the particular software and values intended values can change types. And if you allow the spreadsheet software to save the csv file may permanently change the actual values.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2024 15:38:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Deal-with-rounding-problem-while-exporting-to-CSV/m-p/913815#M83291</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-01-31T15:38:47Z</dc:date>
    </item>
  </channel>
</rss>

