ReadStat icon indicating copy to clipboard operation
ReadStat copied to clipboard

Trailing digits (.d) in SAS DATETIMEw.d are lost upon read

Open DanteDT opened this issue 10 months ago • 2 comments

I believe this is related to https://github.com/WizardMac/ReadStat/issues/233.

As a result of that one, the width w of SAS date/time formats are now preserved, although the digits that SAS refers to as d "digits to the right of the decimal point" are still lost upon read.

From Doug's prior report, see examples in each of SAS's datetime formats

Further details on SAS date/time formats for numeric data:

A trivial test is attached, reading in dttm_wd.sas7bdat (zipped)

created in SAS:

data write.dttm_wd;
  attrib e8601dt length=8 format=e8601dt23.3 label='E8601DTw.d with digits to right of decimal';
  attrib datetime length=8 format=datetime22.3 label='DATETIMEw.d with digits to right of decimal';
  
  do e8601dt = '2023-10-28T01:02:33.456'dt, '2024-10-28T12:03:44.567'dt, '2025-10-28T23:04:55.678'dt;
    datetime = e8601dt;
    put e8601dt best16.3 @20 datetime best16.3;
    output;
  end;
run;

Note the numeric values of those w.d date/times:

   2014074153.456     2014074153.456
   2045736224.567     2045736224.567
   2077311895.678     2077311895.678

View original data in SAS Universal Viewer, unformatted numeric data:

Image

Image

read/write using pyreadstat 1.2.8

import pandas as pd
import pyreadstat as pyrs
dttm_wd = pyrs.read_sas7bdat('data_out/dttm_wd.sas7bdat') 
print(dttm_wd[1].readstat_variable_types)
print(dttm_wd[1].original_variable_types)

pyrs.write_xport(dttm_wd[0], 'data_out/dttm_wd.xpt')

Loss of ".d" digits in these formats.

{'e8601dt': 'double', 'datetime': 'double'}
{'e8601dt': 'E8601DT23', 'datetime': 'DATETIME22'}

View resulting XPT in SAS Univ Viewer, unformatted numeric data:

  • ISO8601 format not preserved, but numeric digits preserved
  • DATETIME format preserved, but numeric digits lost.

Image

Image

dttm_wd.zip

DanteDT avatar Mar 28 '25 19:03 DanteDT

Hi, thanks for the detailed report and the example file. I think I've isolated the issue and will try to have a fix soon.

evanmiller avatar May 22 '25 16:05 evanmiller

Try this: https://github.com/WizardMac/ReadStat/commit/fdf4e1e0c950aae982d3dd1ad91d0f8afc82d06f

evanmiller avatar May 22 '25 18:05 evanmiller