<?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: Bulkload introducing rounding errors to datetime variables in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Bulkload-introducing-rounding-errors-to-datetime-variables/m-p/508663#M15764</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/223838"&gt;@js5&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It sounds like there may be an issue with some of your data. It sounds like there may be a format (or informat) that is truncating some of the columns.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your strategy about finding the observations which display the problem and comparing them to correct values is a strong one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best of luck,&lt;/P&gt;
&lt;P&gt;Jeff&lt;/P&gt;</description>
    <pubDate>Tue, 30 Oct 2018 12:53:44 GMT</pubDate>
    <dc:creator>JBailey</dc:creator>
    <dc:date>2018-10-30T12:53:44Z</dc:date>
    <item>
      <title>Bulkload introducing rounding errors to datetime variables</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Bulkload-introducing-rounding-errors-to-datetime-variables/m-p/508352#M15759</link>
      <description>&lt;P&gt;Dear community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have just discovered something very peculiar with SAS access to ODBC: for a reason yet unknown, saving datetime values in an Microsoft SQL database results in a one second shift if bulkload is enabled: 13AUG2018:16:35:00 will shift to&amp;nbsp;&lt;SPAN&gt;13AUG2018:16:34:59 if bulkload is used but remain unchanged if it is not. This is happening with all versions of ODBC driver (11, 13 and 17) except for the ancient one built into windows.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Is this a known issue?&amp;nbsp;Thank you for your support in advance!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Oct 2018 14:30:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Bulkload-introducing-rounding-errors-to-datetime-variables/m-p/508352#M15759</guid>
      <dc:creator>js5</dc:creator>
      <dc:date>2018-10-29T14:30:24Z</dc:date>
    </item>
    <item>
      <title>Re: Bulkload introducing rounding errors to datetime variables</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Bulkload-introducing-rounding-errors-to-datetime-variables/m-p/508475#M15760</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/223838"&gt;@js5&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The situation you describe has historically been vendor (not SAS) defects in the ODBC driver and loader. You may want to call it a day and open a tech support track. That is probably the quickest way to get past this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That being said...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have tested this with SAS 9.5M5 and I am not seeing the behavior you describe (I may not have an ODBC driver that will show the issue). In order to help solve this we need to see the SAS code you are using to test this, and the interactions between SAS and the database. Pay special attention to the SASTRACE= option...&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the SAS code I am submitting...&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname mymssql odbc dsn=mssql user=someuser password=somepassword;

data work.datetest;
   format x datetime22.3;
   input x datetime22.3;
cards;
13AUG2018:16:35:00
run;

options sastrace=',,,d' sastraceloc=saslog nostsuffix;

proc append base=mymssql.datetest1 (bulkload=yes) data=work.datetest;
run;

proc append base=mymssql.datetest2 data=work.datetest;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;When I look at the resulting tables I see -&amp;nbsp;&lt;STRONG&gt;13AUG2018:16:35:00.000 -&amp;nbsp;&lt;/STRONG&gt;in both tables.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;In my SAS log I see that the CREATE TABLE statements (other than the table names )are the same for both tables. Pay special attention to the data types - I see DATETIME for both tables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good Luck,&lt;BR /&gt;Jeff&lt;/P&gt;</description>
      <pubDate>Mon, 29 Oct 2018 19:57:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Bulkload-introducing-rounding-errors-to-datetime-variables/m-p/508475#M15760</guid>
      <dc:creator>JBailey</dc:creator>
      <dc:date>2018-10-29T19:57:04Z</dc:date>
    </item>
    <item>
      <title>Re: Bulkload introducing rounding errors to datetime variables</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Bulkload-introducing-rounding-errors-to-datetime-variables/m-p/508596#M15762</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/51161"&gt;@JBailey&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for looking into this! The code I am using is nothing too extraordinary:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
	length dataset $32;
	input dataset;
	call symputx(cats("dataset", _n_), dataset);
	call symputx("last_dataset", _n_);
	datalines;
        dataset_a
        dataset_b
        dataset_c
        dataset_d
        dataset_e
	;
run;

%macro transfer_to_sql;

	libname sqllib odbc prompt="Driver={ODBC Driver 13 for SQL Server};
		Server=xxxx;
		Database=yyyy;
		Trusted_Connection=yes;"
		bulkload=yes;

	proc sql;
		%do i = 1 %to &amp;amp;last_dataset;
			drop table sqllib.&amp;amp;&amp;amp;dataset&amp;amp;i;
			create table sqllib.&amp;amp;&amp;amp;dataset&amp;amp;i as 
				select * from saslib.&amp;amp;&amp;amp;dataset&amp;amp;i;
		%end;
	quit;

	libname sqllib clear;

%mend transfer_to_sql;

%transfer_to_sql&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was debugging this further and I also was not able to reproduce the problem with a simple dataset created with datalines. As I&amp;nbsp;cannot share the complete dataset causing the issue, I need to distill it down to the smallest possible reproducer.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options sastrace=',,,d' sastraceloc=saslog nostsuffix;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I was able to see that no matter whether bulkload is enabled or not, the CREATE TABLE statements are the same in both cases. The affected columns are created as datetime2(0) datatype.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ETA1: this is very very strange. With my original data the issue can be reproduced with just one column and one row selected - effectively inserting just one value into the database, i.e.:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	%do i = 1 %to &amp;amp;last_dataset;
		drop table sqllib.&amp;amp;&amp;amp;dataset&amp;amp;i;
		create table sqllib.&amp;amp;&amp;amp;dataset&amp;amp;i as 
			select 'variable a'n
      			from saslib.&amp;amp;&amp;amp;dataset&amp;amp;i where 'variable b'n EQ 74;
	%end;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;On the other hand, with the exact same value created with datalines, the issue can no longer be reproduced, even with the same dataset name, variable name and variable format.&lt;/P&gt;&lt;P&gt;What is even stranger, the issue can still be reproduced when going via a temporary table:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	%do i = 1 %to &amp;amp;last_dataset;
		create table t_&amp;amp;&amp;amp;dataset&amp;amp;i as 
			select 'variable a'n
      			from saslib.&amp;amp;&amp;amp;dataset&amp;amp;i where 'variable b'n EQ 74;			
		drop table sqllib.&amp;amp;&amp;amp;dataset&amp;amp;i;
		create table sqllib.&amp;amp;&amp;amp;dataset&amp;amp;i as 
			select * from t_&amp;amp;&amp;amp;dataset&amp;amp;i;
	%end;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ETA2: I believe I may have found the source of the issue. Intrigued by the fact that inserting a single value into the DB allows to reproduce the problem when selecting from original table but not when using datalines, I have compared both values in hex16. format. Interestingly enough, the hex16. value for 2018-04-11 22:35:00 when taken from my original data is&amp;nbsp;41DB67A144FFFFFE but when entered using datalines it is&amp;nbsp;41DB67A145000000 which is&amp;nbsp;0000000000000002 more.&lt;/P&gt;&lt;P&gt;The original data is imported from an Excel file using xlsx engine. Is this a bug, or just a limitation of xlsx importer?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ETA3: The issue can be reproduced using the attached file and the following code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc import datafile=odbc_datetime_precision_issue.xlsx
	out=sqllib.testcase(bulkload=yes)
	replace
	dbms=xlsx;
	getnames=no;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 09:42:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Bulkload-introducing-rounding-errors-to-datetime-variables/m-p/508596#M15762</guid>
      <dc:creator>js5</dc:creator>
      <dc:date>2018-10-30T09:42:49Z</dc:date>
    </item>
    <item>
      <title>Re: Bulkload introducing rounding errors to datetime variables</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Bulkload-introducing-rounding-errors-to-datetime-variables/m-p/508663#M15764</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/223838"&gt;@js5&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It sounds like there may be an issue with some of your data. It sounds like there may be a format (or informat) that is truncating some of the columns.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your strategy about finding the observations which display the problem and comparing them to correct values is a strong one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best of luck,&lt;/P&gt;
&lt;P&gt;Jeff&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 12:53:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Bulkload-introducing-rounding-errors-to-datetime-variables/m-p/508663#M15764</guid>
      <dc:creator>JBailey</dc:creator>
      <dc:date>2018-10-30T12:53:44Z</dc:date>
    </item>
    <item>
      <title>Re: Bulkload introducing rounding errors to datetime variables</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Bulkload-introducing-rounding-errors-to-datetime-variables/m-p/508848#M15771</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/223838"&gt;@js5&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Excel uses fractions of days for the time component of a datetime value. In your example, 22:35 h is expressed as&lt;/P&gt;
&lt;PRE&gt;(22*3600+35*60)/86400=0.9409722222222...&lt;/PRE&gt;
&lt;P&gt;and together with the date component (11 Apr 2018) the number is 43201.9409722222...,&amp;nbsp;&lt;EM&gt;in theory&lt;/EM&gt; with an infinite sequence of 2s. However, &lt;EM&gt;in practice&lt;/EM&gt; the computer stores this number in a binary format with, of course, only a finite number of bits (binary digits).&amp;nbsp;Using the IEEE floating-point format which is also used by SAS (under Windows) that binary number&amp;nbsp;amounts to&lt;/P&gt;
&lt;PRE&gt;1010100011000001.1111000011100011100011100011100011100&lt;/PRE&gt;
&lt;P&gt;with 53 digits in total, where subsequent digits (01...) have been rounded off. Hence, in the decimal system this number is already slightly smaller than it should be:&lt;/P&gt;
&lt;PRE&gt;43201.9409722222&lt;FONT color="#FF0000"&gt;189884&lt;/FONT&gt;...&lt;/PRE&gt;
&lt;P&gt;I unzipped your example .xlsx file and opened the relevant sheet1.xml file in Notepad. Excerpt:&lt;/P&gt;
&lt;PRE&gt;&amp;lt;v&amp;gt;43201.9409722222&lt;FONT color="#FF0000"&gt;19&lt;/FONT&gt;&amp;lt;/v&amp;gt;&lt;/PRE&gt;
&lt;P&gt;Obviously, this is a rounded decimal representation of the value above.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Presumably, SAS reads &lt;EM&gt;this&lt;/EM&gt; value and, after another decimal-to-binary conversion, converts it&amp;nbsp;into a SAS datetime value (i.e., subtracts an offset of 21916 days and multiplies the result by 86400, but all this is done in the binary system). In the decimal sytem the result should be&amp;nbsp;something like&amp;nbsp;1839105299.99999972..., in the internal hexadecimal representation: 41DB67A144FFFFF&lt;STRONG&gt;F&lt;/STRONG&gt;, where the last "F" is rounded up from "&lt;STRONG&gt;E&lt;/STRONG&gt;D...". Except for the last rounding (up vs. down), this is what you found using the&amp;nbsp;HEX16. format. In any case the value differs from&amp;nbsp;&lt;SPAN&gt;41DB67A145000000 (internal repr.), decimal:&amp;nbsp;1839105300, the SAS datetime value&lt;/SPAN&gt;&amp;nbsp;'11APR2018:22:35:00'dt.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;So, to resolve the issue, you should apply the &lt;A href="https://documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p0tj6cmga7p8qln1ejh6ebevm0c9.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank"&gt;ROUND function&lt;/A&gt; once the values have been read (from Excel) into SAS.&lt;/STRONG&gt; If the datetime values actually don't contain fractions of a second, round to integers, otherwise round to 0.001 (seconds) or whatever the appropriate rounding unit for your data is.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using these clean values in an export from SAS to a database minimizes the risk of introducing rounding errors in the target environment.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit:&amp;nbsp;Cleaning the values also helps to avoid unexpected wrong results &lt;EM&gt;within&lt;/EM&gt; SAS. For example, the IF condition in&lt;/P&gt;
&lt;PRE&gt;if a&amp;gt;='11APR2018:22:35:00'dt then ...&lt;/PRE&gt;
&lt;P&gt;would &lt;EM&gt;not&lt;/EM&gt; be satisfied for a numeric value a with an internal HEX16. representation of 41DB67A144FFFFFF (or ...FFFE).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 18:32:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Bulkload-introducing-rounding-errors-to-datetime-variables/m-p/508848#M15771</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-10-30T18:32:25Z</dc:date>
    </item>
  </channel>
</rss>

