<?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: Add date into file with time in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Add-date-into-file-with-time/m-p/631543#M187136</link>
    <description>&lt;P&gt;The dataset that contain time have the format:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;time&lt;/P&gt;&lt;P&gt;23:30:01&lt;/P&gt;&lt;P&gt;23:30:04&lt;/P&gt;&lt;P&gt;23:30:06&lt;/P&gt;&lt;P&gt;23:30:06&lt;/P&gt;&lt;P&gt;23:30:08&lt;/P&gt;&lt;P&gt;23:30:10&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and so on..,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;23:59:59&lt;/P&gt;&lt;P&gt;00:00:01&lt;/P&gt;&lt;P&gt;00:00:03&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And the datafile that contains the date have the format:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data&lt;/P&gt;&lt;P&gt;2019-09-02&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(only one row with starting date)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want a new variable with the format&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;datetime&lt;/P&gt;&lt;P&gt;2019-09-02&amp;nbsp;23:30:01&lt;/P&gt;&lt;P&gt;2019-09-02&amp;nbsp;23:30:04&lt;/P&gt;&lt;P&gt;2019-09-02&amp;nbsp;23:30:06&lt;/P&gt;&lt;P&gt;2019-09-02&amp;nbsp;23:30:06&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and so on...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2019-09-02&amp;nbsp;23:59:59&lt;/P&gt;&lt;P&gt;2019-09-03 00:00:01&lt;/P&gt;&lt;P&gt;2019-09-03 00:00:03&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 12 Mar 2020 13:10:47 GMT</pubDate>
    <dc:creator>TobbeNord</dc:creator>
    <dc:date>2020-03-12T13:10:47Z</dc:date>
    <item>
      <title>Add date into file with time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-date-into-file-with-time/m-p/631539#M187132</link>
      <description>&lt;P&gt;Hello all.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It's really a simple question, but I can't find the solution.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a file with a time variable and another dataset with the starting date.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The time variable is over several days and I want to create a time date variable (that increase the date by 1 at midnight of course)&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My solution so far doesn't work:&lt;/P&gt;&lt;P&gt;Time is time variable in text format.&lt;/P&gt;&lt;P&gt;Datum is date in text format.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code below (inside a macro):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data &amp;amp;dataset_name.date;&lt;BR /&gt;set &amp;amp;dataset_name.id;&lt;BR /&gt;timme=substr(time,1,2)*1;&lt;BR /&gt;if _n_ eq 1 then do;&lt;BR /&gt;set &amp;amp;dataset_name._date(rename=(data=datum));&lt;BR /&gt;end;&lt;BR /&gt;datumnum=mdy(substr(datum,6,2),substr(datum,9,2),substr(datum,1,4));&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data &amp;amp;dataset_name.dateid;&lt;BR /&gt;retain datumnum;&lt;BR /&gt;set &amp;amp;dataset_name.date;&lt;BR /&gt;by datumnum;&lt;BR /&gt;if dif(timme)&amp;lt;0 then datumnum+1;&lt;BR /&gt;Timedate=DHMS( datumnum, substr(time,1,2), substr(time,4,2), substr(time,7,2) );&lt;BR /&gt;format Timedate datetime.;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 12:48:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-date-into-file-with-time/m-p/631539#M187132</guid>
      <dc:creator>TobbeNord</dc:creator>
      <dc:date>2020-03-12T12:48:17Z</dc:date>
    </item>
    <item>
      <title>Re: Add date into file with time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-date-into-file-with-time/m-p/631541#M187134</link>
      <description>&lt;P&gt;Please give us examples for your datasets that illustrate what you have, and what you want to get out of it.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 12:59:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-date-into-file-with-time/m-p/631541#M187134</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-03-12T12:59:30Z</dc:date>
    </item>
    <item>
      <title>Re: Add date into file with time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-date-into-file-with-time/m-p/631543#M187136</link>
      <description>&lt;P&gt;The dataset that contain time have the format:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;time&lt;/P&gt;&lt;P&gt;23:30:01&lt;/P&gt;&lt;P&gt;23:30:04&lt;/P&gt;&lt;P&gt;23:30:06&lt;/P&gt;&lt;P&gt;23:30:06&lt;/P&gt;&lt;P&gt;23:30:08&lt;/P&gt;&lt;P&gt;23:30:10&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and so on..,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;23:59:59&lt;/P&gt;&lt;P&gt;00:00:01&lt;/P&gt;&lt;P&gt;00:00:03&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And the datafile that contains the date have the format:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data&lt;/P&gt;&lt;P&gt;2019-09-02&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(only one row with starting date)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want a new variable with the format&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;datetime&lt;/P&gt;&lt;P&gt;2019-09-02&amp;nbsp;23:30:01&lt;/P&gt;&lt;P&gt;2019-09-02&amp;nbsp;23:30:04&lt;/P&gt;&lt;P&gt;2019-09-02&amp;nbsp;23:30:06&lt;/P&gt;&lt;P&gt;2019-09-02&amp;nbsp;23:30:06&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and so on...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2019-09-02&amp;nbsp;23:59:59&lt;/P&gt;&lt;P&gt;2019-09-03 00:00:01&lt;/P&gt;&lt;P&gt;2019-09-03 00:00:03&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 13:10:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-date-into-file-with-time/m-p/631543#M187136</guid>
      <dc:creator>TobbeNord</dc:creator>
      <dc:date>2020-03-12T13:10:47Z</dc:date>
    </item>
    <item>
      <title>Re: Add date into file with time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-date-into-file-with-time/m-p/631546#M187138</link>
      <description>&lt;P&gt;See this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data times;
input time :time8.;
format time time8.;
datalines;
23:30:01
23:30:04
23:30:06
23:30:06
23:30:08
23:30:10 
23:59:59
00:00:01
00:00:03
;

data dates;
input startdate :yymmdd10.;
format startdate yymmddd10.;
datalines;
2019-09-02
;

data want;
set times;
if _n_ = 1 then set dates;
if time &amp;lt; lag(time) then startdate + 1;
dt = dhms(startdate,0,0,time);
format dt e8601dt19.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It also illustrates the importance of storing date and time values as SAS date and time values, which makes it easy to use simple arithmetic and the functions and formats SAS provides for dates and times.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 13:25:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-date-into-file-with-time/m-p/631546#M187138</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-03-12T13:25:04Z</dc:date>
    </item>
    <item>
      <title>Re: Add date into file with time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-date-into-file-with-time/m-p/631569#M187143</link>
      <description>&lt;P&gt;Thank you, but solution do exactly the same as I already have done, and then only the first row after midnight change date, then it's starting date again.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 14:20:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-date-into-file-with-time/m-p/631569#M187143</guid>
      <dc:creator>TobbeNord</dc:creator>
      <dc:date>2020-03-12T14:20:58Z</dc:date>
    </item>
    <item>
      <title>Re: Add date into file with time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-date-into-file-with-time/m-p/631574#M187145</link>
      <description>&lt;P&gt;You obviously have not run my code as posted, see this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data times;
input time :time8.;
format time time8.;
datalines;
23:30:01
23:30:04
23:30:06
23:30:06
23:30:08
23:30:10 
23:59:59
00:00:01
00:00:03
;

data dates;
input startdate :yymmdd10.;
format startdate yymmddd10.;
datalines;
2019-09-02
;

data want;
set times;
if _n_ = 1 then set dates;
if time &amp;lt; lag(time) then startdate + 1;
dt = dhms(startdate,0,0,time);
format dt e8601dt19.;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;    time     startdate    dt

23:30:01    2019-09-02    2019-09-02T23:30:01
23:30:04    2019-09-02    2019-09-02T23:30:04
23:30:06    2019-09-02    2019-09-02T23:30:06
23:30:06    2019-09-02    2019-09-02T23:30:06
23:30:08    2019-09-02    2019-09-02T23:30:08
23:30:10    2019-09-02    2019-09-02T23:30:10
23:59:59    2019-09-02    2019-09-02T23:59:59
 0:00:01    2019-09-03    2019-09-03T00:00:01
 0:00:03    2019-09-03    2019-09-03T00:00:03
&lt;/PRE&gt;
&lt;P&gt;As you can see, ALL observations after midnight have the new date.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 14:29:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-date-into-file-with-time/m-p/631574#M187145</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-03-12T14:29:11Z</dc:date>
    </item>
    <item>
      <title>Re: Add date into file with time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-date-into-file-with-time/m-p/631593#M187147</link>
      <description>&lt;P&gt;It's strange, because on my dataset it's not working that way.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 14:56:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-date-into-file-with-time/m-p/631593#M187147</guid>
      <dc:creator>TobbeNord</dc:creator>
      <dc:date>2020-03-12T14:56:40Z</dc:date>
    </item>
    <item>
      <title>Re: Add date into file with time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-date-into-file-with-time/m-p/631599#M187150</link>
      <description>&lt;P&gt;It is essential that dates and times are stored as such (counts of days from 1960-01-01 / seconds from midnight).&lt;/P&gt;
&lt;P&gt;If my code does not work for you, post your dataset as a data step with datalines (so that the created dataset looks like yours in terms of structure/variable attributes), and the log of the code.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 15:15:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-date-into-file-with-time/m-p/631599#M187150</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-03-12T15:15:38Z</dc:date>
    </item>
  </channel>
</rss>

