<?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 to insert missing datetime records in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-missing-datetime-records/m-p/933043#M366996</link>
    <description>&lt;P&gt;Why bother going to the trouble to make an Excel file and then attach the file to your post?&amp;nbsp; It is much easier for you (and much much easier for someone to help you) if you just post the SAS code to make the dataset(s) directly.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input (s e) (:datetime.);
  format s e datetime19.;
cards;
02FEB2023:20:44:00 02FEB2023:20:48:00 
02FEB2023:20:48:00 02FEB2023:22:03:00 
03FEB2023:13:23:00 03FEB2023:15:21:00 
03FEB2023:15:21:00 03FEB2023:17:21:00 
03FEB2023:17:25:00 03FEB2023:17:26:00 
03FEB2023:17:26:00 03FEB2023:18:10:00 
05FEB2023:01:18:00 05FEB2023:02:46:00
;


data expect;
  infile cards truncover ;
  input (s e) (:datetime.) (2*flag) (:$8.);
  format s e datetime19.;
cards;
02FEB2023:20:44:00 02FEB2023:20:48:00
02FEB2023:20:48:00 02FEB2023:22:03:00
02FEB2023:22:03:00 03FEB2023:13:23:00 --&amp;gt; New
03FEB2023:13:23:00 03FEB2023:15:21:00
03FEB2023:15:21:00 03FEB2023:17:21:00
03FEB2023:17:21:00 03FEB2023:17:25:00 --&amp;gt; New
03FEB2023:17:25:00 03FEB2023:17:26:00
03FEB2023:17:26:00 03FEB2023:18:10:00
03FEB2023:18:10:00 05FEB2023:01:18:00 --&amp;gt; New
05FEB2023:01:18:00 05FEB2023:02:46:00
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Just remember the previous end datetime value so that when there is a gap you can add another observation.&amp;nbsp; It is simplest if you sort the result later.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  output;
  lag_e=lag(e);
  if lag_e &amp;lt; s and _n_&amp;gt;1 then do;
    e=s;
    s=lag_e;
    output;
  end;
  drop lag_e;
run;

proc sort;
  by s e;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But you could get a little tricky and write the new observation before the old.&amp;nbsp; For example by adding an extra SET to re-read the current observation to get the original dates back.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  lag_e=lag(e);
  if lag_e &amp;lt; s and _n_&amp;gt;1 then do;
    e=s;
    s=lag_e;
    output;
    set have point=_n_;
  end;
  output;
  drop lag_e;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 19 Jun 2024 16:48:25 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-06-19T16:48:25Z</dc:date>
    <item>
      <title>How to insert missing datetime records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-missing-datetime-records/m-p/932957#M366978</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've a dataset with start / end datetimes. Sometimes there is a gap between two records. I want to insert a record where this new record fills the gap between both records. In the example below, a record must be added with start date_time 02FEB2023:22:03:00 and end date_time 03FEB2023:13:23:00.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="test223_0-1718790433211.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/97618i9EC222F3B6DEEE98/image-size/medium?v=v2&amp;amp;px=400" role="button" title="test223_0-1718790433211.png" alt="test223_0-1718790433211.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Who can help me with this code?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jun 2024 09:51:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-missing-datetime-records/m-p/932957#M366978</guid>
      <dc:creator>Jaap_K</dc:creator>
      <dc:date>2024-06-19T09:51:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert missing datetime records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-missing-datetime-records/m-p/932958#M366979</link>
      <description>&lt;P&gt;You wrote: "&lt;SPAN&gt;must be added with start date_time 02FEB2023:22:03:00 and end date_time 03FEB2023:13:23:00.&lt;/SPAN&gt;"&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are those dates calculated somehow? (e.g. average of previous and next) or they are fixed (always the same)?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jun 2024 09:55:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-missing-datetime-records/m-p/932958#M366979</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-06-19T09:55:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert missing datetime records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-missing-datetime-records/m-p/932964#M366980</link>
      <description>&lt;P&gt;Hi Bart,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The dataset is sorted and is fixed. So you must look at the row above.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jun 2024 10:39:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-missing-datetime-records/m-p/932964#M366980</guid>
      <dc:creator>Jaap_K</dc:creator>
      <dc:date>2024-06-19T10:39:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert missing datetime records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-missing-datetime-records/m-p/932965#M366981</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input start_date end_date;
format start_date end_date datetime21.;
cards;
1 2
3 4
. 6
7 .
;
run;
proc print;
run;

data want;
 set have;
 prev_s=lag(start_date);
 prev_e=lag(end_date);

 start_date = coalesce(start_date,prev_s); 
 end_date = coalesce(end_date,prev_e);

 drop prev_:;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Truy this.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jun 2024 10:50:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-missing-datetime-records/m-p/932965#M366981</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-06-19T10:50:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert missing datetime records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-missing-datetime-records/m-p/932969#M366983</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That's not exactly what I mean. See Excelfile. Sometimes there is a time difference between two records. In the 'want' example you see new records that 'resolve' the timegap. I'm looking for the code who create these records.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jun 2024 11:13:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-missing-datetime-records/m-p/932969#M366983</guid>
      <dc:creator>Jaap_K</dc:creator>
      <dc:date>2024-06-19T11:13:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert missing datetime records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-missing-datetime-records/m-p/932974#M366984</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/451267"&gt;@Jaap_K&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The dataset is sorted and is fixed. So you must look at the row above.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;and then what, once I looked at the row above? What is the calculation or algorithm?&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jun 2024 12:08:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-missing-datetime-records/m-p/932974#M366984</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-06-19T12:08:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert missing datetime records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-missing-datetime-records/m-p/933043#M366996</link>
      <description>&lt;P&gt;Why bother going to the trouble to make an Excel file and then attach the file to your post?&amp;nbsp; It is much easier for you (and much much easier for someone to help you) if you just post the SAS code to make the dataset(s) directly.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input (s e) (:datetime.);
  format s e datetime19.;
cards;
02FEB2023:20:44:00 02FEB2023:20:48:00 
02FEB2023:20:48:00 02FEB2023:22:03:00 
03FEB2023:13:23:00 03FEB2023:15:21:00 
03FEB2023:15:21:00 03FEB2023:17:21:00 
03FEB2023:17:25:00 03FEB2023:17:26:00 
03FEB2023:17:26:00 03FEB2023:18:10:00 
05FEB2023:01:18:00 05FEB2023:02:46:00
;


data expect;
  infile cards truncover ;
  input (s e) (:datetime.) (2*flag) (:$8.);
  format s e datetime19.;
cards;
02FEB2023:20:44:00 02FEB2023:20:48:00
02FEB2023:20:48:00 02FEB2023:22:03:00
02FEB2023:22:03:00 03FEB2023:13:23:00 --&amp;gt; New
03FEB2023:13:23:00 03FEB2023:15:21:00
03FEB2023:15:21:00 03FEB2023:17:21:00
03FEB2023:17:21:00 03FEB2023:17:25:00 --&amp;gt; New
03FEB2023:17:25:00 03FEB2023:17:26:00
03FEB2023:17:26:00 03FEB2023:18:10:00
03FEB2023:18:10:00 05FEB2023:01:18:00 --&amp;gt; New
05FEB2023:01:18:00 05FEB2023:02:46:00
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Just remember the previous end datetime value so that when there is a gap you can add another observation.&amp;nbsp; It is simplest if you sort the result later.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  output;
  lag_e=lag(e);
  if lag_e &amp;lt; s and _n_&amp;gt;1 then do;
    e=s;
    s=lag_e;
    output;
  end;
  drop lag_e;
run;

proc sort;
  by s e;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But you could get a little tricky and write the new observation before the old.&amp;nbsp; For example by adding an extra SET to re-read the current observation to get the original dates back.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  lag_e=lag(e);
  if lag_e &amp;lt; s and _n_&amp;gt;1 then do;
    e=s;
    s=lag_e;
    output;
    set have point=_n_;
  end;
  output;
  drop lag_e;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Jun 2024 16:48:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-missing-datetime-records/m-p/933043#M366996</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-06-19T16:48:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert missing datetime records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-missing-datetime-records/m-p/933064#M367001</link>
      <description>&lt;P&gt;Use merge with a "FIRSTOBS=2" option to look ahead at the next start value:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  startdate_time='02feb2023:19:08:00'dt; enddate_time='02feb2023:20:44:00'dt; output;
  startdate_time='02feb2023:20:44:00'dt; enddate_time='02feb2023:20:48:00'dt; output;
  startdate_time='02feb2023:20:48:00'dt; enddate_time='02feb2023:22:03:00'dt; output;
  startdate_time='03feb2023:13:23:00'dt; enddate_time='03feb2023:15:21:00'dt; output;
  format startdate_time enddate_time datetime18.0;
run;

data want (drop=nxt_:);
  merge have  have (firstobs=2 keep=startdate_time rename=(startdate_time=nxt_s));
  output;
  if enddate_time&amp;lt;nxt_s;
  startdate_time=enddate_time;
  enddate_time=nxt_s;
  output;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 Jun 2024 01:15:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-missing-datetime-records/m-p/933064#M367001</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-06-20T01:15:07Z</dc:date>
    </item>
  </channel>
</rss>

