<?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: How can I read a SAS datetime value in Python in Developers</title>
    <link>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433682#M134</link>
    <description>I partially found a solution. main2['iadmdttm'] = pd.to_datetime((main2['IADMDTTM'] + 315619200)) But the date-time values are: 1970-01-01 00:00:02.057248200 1970-01-01 00:00:02.072612280 etc... rather than date-times from around 1/1/2016</description>
    <pubDate>Fri, 02 Feb 2018 20:03:41 GMT</pubDate>
    <dc:creator>DocMartin</dc:creator>
    <dc:date>2018-02-02T20:03:41Z</dc:date>
    <item>
      <title>How can I read a SAS datetime value in Python</title>
      <link>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433593#M124</link>
      <description>&lt;P&gt;I'm using Pandas read_sas method to read a SAS data set into Python. The problem is that a SAS data set stores datetime values as seconds since 1/1/1960 (I think that's right). I want to use Pandas' datetime module, but it expects a datetime format, not an integer. Is there any way around this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's my code:&amp;nbsp;&lt;/P&gt;&lt;P&gt;from tkinter.filedialog import askopenfilename&lt;/P&gt;&lt;P&gt;import numpy as np&lt;/P&gt;&lt;P&gt;import pandas as pd&lt;/P&gt;&lt;P&gt;import datetime as dt&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;infileName = askopenfilename() main1 = pd.read_sas(infileName, index='SUBID', format='sas7bdat') main2 = main1.loc[: , ['APS', 'Age', 'Diabetes', 'hosdead', 'icudead', 'mv', 'hospid', 'iculos', 'ICU_TYPE', 'visit', 'dx', 'dx1', 'IADMDTTM', 'IDISDTTM', 'HADMDTTM', 'HDISDTTM']] _ = main2.dropna(thresh=12, inplace=True)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;The statement below is where I run into problems. &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;ilos = dt.timedelta(dt.datetime(main2.loc[:, 'IDISDTTM']) - dt.datetime(main2.loc[:,'IADMDTTM']))&lt;/P&gt;</description>
      <pubDate>Fri, 02 Feb 2018 16:15:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433593#M124</guid>
      <dc:creator>DocMartin</dc:creator>
      <dc:date>2018-02-02T16:15:59Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read a SAS datetime value in Python</title>
      <link>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433613#M125</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/83350"&gt;@DocMartin&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would use something like that, do not forget that it will be UTC time. If you need a local time, you have to do TZ conversion too.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;#!/usr/bin/python
import datetime
sas_time = 1201972927
time = datetime.datetime.fromtimestamp(sas_time + 315619200).isoformat()
print(time)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Feb 2018 17:31:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433613#M125</guid>
      <dc:creator>alexal</dc:creator>
      <dc:date>2018-02-02T17:31:32Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read a SAS datetime value in Python</title>
      <link>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433643#M126</link>
      <description>&lt;P&gt;I'm trying to broadcast the function across all date-time values. When I issue this command (IADMDTTM is ICU admission time):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;main2['iadmdttm'] = dt.datetime.fromtimestamp(main2['IADMDTTM'] + 315619200).isoformat()&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get this error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;TypeError: cannot convert the series to&amp;nbsp;&amp;lt;class 'int'&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I'm not sure what to do. &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Feb 2018 18:17:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433643#M126</guid>
      <dc:creator>DocMartin</dc:creator>
      <dc:date>2018-02-02T18:17:53Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read a SAS datetime value in Python</title>
      <link>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433654#M127</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/83350"&gt;@DocMartin&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you sure that main2['IADMDTTM'] is a timestamp? Run these lines:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;print(isinstance(main2['IADMDTTM'], int))
print(main2['IADMDTTM'])&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Feb 2018 18:35:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433654#M127</guid>
      <dc:creator>alexal</dc:creator>
      <dc:date>2018-02-02T18:35:10Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read a SAS datetime value in Python</title>
      <link>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433661#M128</link>
      <description>No. main2['IADMDTTM'] is the SAS integer representation of the date-time.</description>
      <pubDate>Fri, 02 Feb 2018 18:44:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433661#M128</guid>
      <dc:creator>DocMartin</dc:creator>
      <dc:date>2018-02-02T18:44:36Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read a SAS datetime value in Python</title>
      <link>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433663#M129</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/83350"&gt;@DocMartin&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Show me the output of the program above, please.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Feb 2018 18:46:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433663#M129</guid>
      <dc:creator>alexal</dc:creator>
      <dc:date>2018-02-02T18:46:51Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read a SAS datetime value in Python</title>
      <link>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433669#M130</link>
      <description>print(isinstance(main2['IADMDTTM'], int))&lt;BR /&gt;print(main2['IADMDTTM'])&lt;BR /&gt;False&lt;BR /&gt;SUBID&lt;BR /&gt;b'B1000001790' 1.741629e+09&lt;BR /&gt;b'B1000001848' 1.756993e+09&lt;BR /&gt;etc...</description>
      <pubDate>Fri, 02 Feb 2018 19:06:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433669#M130</guid>
      <dc:creator>DocMartin</dc:creator>
      <dc:date>2018-02-02T19:06:13Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read a SAS datetime value in Python</title>
      <link>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433670#M131</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/83350"&gt;@DocMartin&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here you go. This is an object and the object type isn't an int. Give me a few minutes to check Pandas read_sas method. I've never used it before.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Feb 2018 19:11:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433670#M131</guid>
      <dc:creator>alexal</dc:creator>
      <dc:date>2018-02-02T19:11:20Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read a SAS datetime value in Python</title>
      <link>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433673#M132</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/83350"&gt;@DocMartin&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Actually, you do not need to convert anything. Check this program. I've read BUY dataset from SASHELP:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;import pandas as pd
buy = pd.read_sas("/home/alex/buy.sas7bdat")
/* Just print */
print(buy)
/* If you want to iterate over each row, you can do this */
for index, row in buy.iterrows():
  print(row['DATE'])
  print(row['AMOUNT'])&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;python pandas_read_sas.py
         DATE    AMOUNT
0  1996-01-01 -110000.0
1  1997-01-01   -1000.0
2  1998-01-01   -1000.0
3  1999-01-01  -51000.0
4  2000-01-01   -2000.0
5  2001-01-01   -2000.0
6  2002-01-01   -2000.0
7  2003-01-01   -2000.0
8  2004-01-01   -2000.0
9  2005-01-01   -2000.0
10 2006-01-01   48000.0
1996-01-01 00:00:00
-110000.0
1997-01-01 00:00:00
-1000.0
1998-01-01 00:00:00
-1000.0
1999-01-01 00:00:00
-51000.0
2000-01-01 00:00:00
-2000.0
2001-01-01 00:00:00
-2000.0
2002-01-01 00:00:00
-2000.0
2003-01-01 00:00:00
-2000.0
2004-01-01 00:00:00
-2000.0
2005-01-01 00:00:00
-2000.0
2006-01-01 00:00:00
48000.0&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Feb 2018 19:35:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433673#M132</guid>
      <dc:creator>alexal</dc:creator>
      <dc:date>2018-02-02T19:35:08Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read a SAS datetime value in Python</title>
      <link>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433677#M133</link>
      <description>When I use that code on main2, I still get SAS date-time float values.</description>
      <pubDate>Fri, 02 Feb 2018 19:44:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433677#M133</guid>
      <dc:creator>DocMartin</dc:creator>
      <dc:date>2018-02-02T19:44:04Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read a SAS datetime value in Python</title>
      <link>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433682#M134</link>
      <description>I partially found a solution. main2['iadmdttm'] = pd.to_datetime((main2['IADMDTTM'] + 315619200)) But the date-time values are: 1970-01-01 00:00:02.057248200 1970-01-01 00:00:02.072612280 etc... rather than date-times from around 1/1/2016</description>
      <pubDate>Fri, 02 Feb 2018 20:03:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433682#M134</guid>
      <dc:creator>DocMartin</dc:creator>
      <dc:date>2018-02-02T20:03:41Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read a SAS datetime value in Python</title>
      <link>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433696#M135</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/83350"&gt;@DocMartin&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to review the output from this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;print(main2.dtypes)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Feb 2018 20:45:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/How-can-I-read-a-SAS-datetime-value-in-Python/m-p/433696#M135</guid>
      <dc:creator>alexal</dc:creator>
      <dc:date>2018-02-02T20:45:30Z</dc:date>
    </item>
  </channel>
</rss>

