<?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: date and time format in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/date-and-time-format/m-p/879245#M39018</link>
    <description>&lt;P&gt;Assuming you have a NUMERIC variable that has a count of seconds that has the DATETIME16. format attached to it so that the numbers are displayed as the strings you showed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then just use the DATEPART() and TIMEPART() functions to generate new variables that contain days and seconds.&amp;nbsp; You can then attach appropriate formats to those new variables to have the values displayed in the style you showed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's make a sample dataset with a datetime variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  datetime=datetime();
  format datetime datetime19.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now we can make a new dataset that adds the new date and time variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  date=datepart(datetime);
  time=timepart(datetime);
  format date mmddyy10. time timeampm. ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs               datetime          date       time

 1      05JUN2023:09:36:06    06/05/2023    9:36:06 AM

&lt;/PRE&gt;</description>
    <pubDate>Mon, 05 Jun 2023 13:37:32 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-06-05T13:37:32Z</dc:date>
    <item>
      <title>date and time format</title>
      <link>https://communities.sas.com/t5/New-SAS-User/date-and-time-format/m-p/879241#M39015</link>
      <description>&lt;P&gt;Hi I have a dataset A with this format&lt;/P&gt;&lt;P&gt;11JAN21:13:15:00.&lt;/P&gt;&lt;P&gt;12JUL02:12:30:02&lt;/P&gt;&lt;P&gt;I want to convert this to separate column of date and time(am and pm).&lt;/P&gt;&lt;P&gt;Date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Time&lt;/P&gt;&lt;P&gt;01/11/2021&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1:15:00pm&lt;/P&gt;&lt;P&gt;07/12/2002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12:30:02am&lt;/P&gt;</description>
      <pubDate>Mon, 05 Jun 2023 13:24:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/date-and-time-format/m-p/879241#M39015</guid>
      <dc:creator>Smitha9</dc:creator>
      <dc:date>2023-06-05T13:24:57Z</dc:date>
    </item>
    <item>
      <title>Re: date and time format</title>
      <link>https://communities.sas.com/t5/New-SAS-User/date-and-time-format/m-p/879242#M39016</link>
      <description>&lt;P&gt;Use the &lt;EM&gt;datepart&lt;/EM&gt; and &lt;EM&gt;timepart&lt;/EM&gt; functions respectively.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Jun 2023 13:32:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/date-and-time-format/m-p/879242#M39016</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2023-06-05T13:32:02Z</dc:date>
    </item>
    <item>
      <title>Re: date and time format</title>
      <link>https://communities.sas.com/t5/New-SAS-User/date-and-time-format/m-p/879243#M39017</link>
      <description>&lt;P&gt;Try the DATEPART and TIMEPART functions:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input DateTimeValue:datetime.;
Date=datepart(DateTimeValue);
Time=timepart(DateTimeValue);
format DateTimeValue datetime. Date mmddyy10. time timeampm.;
datalines;
11JAN21:13:15:00
12JUL02:12:30:02
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r header" scope="col"&gt;Obs&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;DateTimeValue&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;Date&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;Time&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;1&lt;/TH&gt;
&lt;TD class="r data"&gt;11JAN21:13:15:00&lt;/TD&gt;
&lt;TD class="r data"&gt;01/11/2021&lt;/TD&gt;
&lt;TD class="r data"&gt;1:15:00 PM&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;2&lt;/TH&gt;
&lt;TD class="r data"&gt;12JUL02:12:30:02&lt;/TD&gt;
&lt;TD class="r data"&gt;07/12/2002&lt;/TD&gt;
&lt;TD class="r data"&gt;12:30:02 PM&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Mon, 05 Jun 2023 13:32:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/date-and-time-format/m-p/879243#M39017</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2023-06-05T13:32:09Z</dc:date>
    </item>
    <item>
      <title>Re: date and time format</title>
      <link>https://communities.sas.com/t5/New-SAS-User/date-and-time-format/m-p/879245#M39018</link>
      <description>&lt;P&gt;Assuming you have a NUMERIC variable that has a count of seconds that has the DATETIME16. format attached to it so that the numbers are displayed as the strings you showed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then just use the DATEPART() and TIMEPART() functions to generate new variables that contain days and seconds.&amp;nbsp; You can then attach appropriate formats to those new variables to have the values displayed in the style you showed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's make a sample dataset with a datetime variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  datetime=datetime();
  format datetime datetime19.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now we can make a new dataset that adds the new date and time variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  date=datepart(datetime);
  time=timepart(datetime);
  format date mmddyy10. time timeampm. ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs               datetime          date       time

 1      05JUN2023:09:36:06    06/05/2023    9:36:06 AM

&lt;/PRE&gt;</description>
      <pubDate>Mon, 05 Jun 2023 13:37:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/date-and-time-format/m-p/879245#M39018</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-05T13:37:32Z</dc:date>
    </item>
    <item>
      <title>Re: date and time format</title>
      <link>https://communities.sas.com/t5/New-SAS-User/date-and-time-format/m-p/951901#M42785</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hey, you can easily achieve this transformation using SAS or Python, depending on your preference.&lt;/P&gt;&lt;P&gt;In SAS:&lt;/P&gt;&lt;P&gt;You can use the &lt;CODE&gt;INPUT&lt;/CODE&gt; function to read the original datetime format and then split it into date and time columns:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;sas&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;CODE class=""&gt;data B; set A; format Date mmddyy10. Time timeampm.; datetime = input(OriginalColumn, datetime20.); Date = datepart(datetime); Time = timepart(datetime); run; &lt;/CODE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;In Python (Pandas):&lt;/P&gt;&lt;P&gt;Using Python, you can achieve the same with &lt;CODE&gt;datetime&lt;/CODE&gt; and &lt;CODE&gt;strftime&lt;/CODE&gt;:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;python&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;CODE class=""&gt;&lt;SPAN class=""&gt;import&lt;/SPAN&gt; pandas &lt;SPAN class=""&gt;as&lt;/SPAN&gt; pd data = {&lt;SPAN class=""&gt;'OriginalColumn'&lt;/SPAN&gt;: [&lt;SPAN class=""&gt;'11JAN21:13:15:00'&lt;/SPAN&gt;, &lt;SPAN class=""&gt;'12JUL02:12:30:02'&lt;/SPAN&gt;]} df = pd.DataFrame(data) &lt;SPAN class=""&gt;# Convert to datetime&lt;/SPAN&gt; df[&lt;SPAN class=""&gt;'Datetime'&lt;/SPAN&gt;] = pd.to_datetime(df[&lt;SPAN class=""&gt;'OriginalColumn'&lt;/SPAN&gt;], &lt;SPAN class=""&gt;format&lt;/SPAN&gt;=&lt;SPAN class=""&gt;'%d%b%y:%H:%M:%S'&lt;/SPAN&gt;) &lt;SPAN class=""&gt;# Extract Date and Time&lt;/SPAN&gt; df[&lt;SPAN class=""&gt;'Date'&lt;/SPAN&gt;] = df[&lt;SPAN class=""&gt;'Datetime'&lt;/SPAN&gt;].dt.strftime(&lt;SPAN class=""&gt;'%m/%d/%Y'&lt;/SPAN&gt;) df[&lt;SPAN class=""&gt;'Time'&lt;/SPAN&gt;] = df[&lt;SPAN class=""&gt;'Datetime'&lt;/SPAN&gt;].dt.strftime(&lt;SPAN class=""&gt;'%I:%M:%S%p'&lt;/SPAN&gt;) &lt;SPAN class=""&gt;print&lt;/SPAN&gt;(df[[&lt;SPAN class=""&gt;'Date'&lt;/SPAN&gt;, &lt;SPAN class=""&gt;'Time'&lt;/SPAN&gt;]]) &lt;/CODE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;Handling date and time can sometimes cause issues with formats like leap years or AM/PM shifts. That could be tricky, especially with February's 28-29 day inconsistency. It was fixed with an additional C++ class in&amp;nbsp;&amp;nbsp;&lt;A href="https://timechart.org" target="_new" rel="noopener"&gt;&lt;SPAN&gt;Timechart&lt;/SPAN&gt;&lt;SPAN&gt; Work&lt;/SPAN&gt;&lt;SPAN&gt; Time&lt;/SPAN&gt;&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Nov 2024 13:14:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/date-and-time-format/m-p/951901#M42785</guid>
      <dc:creator>irohitsharma535</dc:creator>
      <dc:date>2024-11-26T13:14:32Z</dc:date>
    </item>
  </channel>
</rss>

