<?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 a row corresponding to start and end of time period and take nearest value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-a-row-corresponding-to-start-and-end-of-time/m-p/835537#M330327</link>
    <description>&lt;P&gt;Thanks for the code Shmuel, sadly it's not quite what I need.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm hoping to have each time category sandwiched by two time points.&lt;/P&gt;&lt;P&gt;For time_cat = 0, I want to create an entry with time 00:00:00 and an entry at time 06:00:00.&lt;/P&gt;&lt;P&gt;For time_cat = 1, an entry at the start with time 06:00:00 and one at the end at 10:00:00..&lt;/P&gt;&lt;P&gt;For time_cat = 2, one starting at 11:00:00AM and one ending at 15:00:00.&lt;/P&gt;&lt;P&gt;For time_cat = 3, the first entry starting at 17:00:00 and one final entry ending at 21:00:00.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In each case I'm hoping to create those entries and pull the sensor value that occurs closest to that time point.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 28 Sep 2022 08:19:32 GMT</pubDate>
    <dc:creator>SVSzwec</dc:creator>
    <dc:date>2022-09-28T08:19:32Z</dc:date>
    <item>
      <title>How to insert a row corresponding to start and end of time period and take nearest value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-a-row-corresponding-to-start-and-end-of-time/m-p/835532#M330324</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset where I am trying to work with measures over specific time periods (e.g. Midnight to 6AM, 6AM to 10AM etc.). The data is measured regularly in roughly 5 minute intervals however it is not always measured on the hour. I would like to try and create an entry corresponding to the start and end of each time period (time_cat) and pull the measure value closest to that time point.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example in the first case I want to create an entry at time 00:00:00 and take the measure value of the entry following it. In the very last case I would want ot create an entry at 21:00:00 and take the measure value preceding it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm not really sure where the best place to start with this is and any help would be great appreciated.&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data WORK.measure_tp;&lt;BR /&gt;infile datalines dsd truncover;&lt;BR /&gt;input sensor:BEST. in_range_flag:32. datepart:DDMMYY. timepart:TIME. time_cat:32.;&lt;BR /&gt;format sensor BEST. datepart DDMMYY. timepart TIME.;&lt;BR /&gt;label sensor="Sensor Measure";&lt;BR /&gt;datalines;&lt;BR /&gt;7.1 1 18/07/19 0:00:35 0&lt;BR /&gt;7.4 1 18/07/19 0:05:35 0&lt;BR /&gt;6.8 1 18/07/19 0:10:35 0&lt;BR /&gt;6.8 1 18/07/19 0:15:35 0&lt;BR /&gt;6.4 1 18/07/19 0:20:35 0&lt;BR /&gt;6.9 1 18/07/19 5:40:34 0&lt;BR /&gt;7.2 1 18/07/19 5:45:34 0&lt;BR /&gt;7.3 1 18/07/19 5:50:34 0&lt;BR /&gt;7.8 1 18/07/19 5:55:34 0&lt;BR /&gt;7 1 18/07/19 6:00:34 1&lt;BR /&gt;6.7 1 18/07/19 6:05:34 1&lt;BR /&gt;6.4 1 18/07/19 6:10:34 1&lt;BR /&gt;6.3 1 18/07/19 6:15:34 1&lt;BR /&gt;6 1 18/07/19 9:45:33 1&lt;BR /&gt;5.9 1 18/07/19 9:50:33 1&lt;BR /&gt;5.6 1 18/07/19 9:55:33 1&lt;BR /&gt;5.4 1 18/07/19 10:00:33 .&lt;BR /&gt;5.2 1 18/07/19 10:05:34 .&lt;BR /&gt;5.5 1 18/07/19 10:10:33 .&lt;BR /&gt;5.9 1 18/07/19 10:15:33 .&lt;BR /&gt;6.9 1 18/07/19 10:45:33 .&lt;BR /&gt;7 1 18/07/19 10:50:33 .&lt;BR /&gt;7 1 18/07/19 10:55:33 .&lt;BR /&gt;7.2 1 18/07/19 11:00:33 2&lt;BR /&gt;7.3 1 18/07/19 11:05:33 2&lt;BR /&gt;7.4 1 18/07/19 11:10:33 2&lt;BR /&gt;7.3 1 18/07/19 11:15:34 2&lt;BR /&gt;11.2 0 18/07/19 14:45:33 2&lt;BR /&gt;11.6 0 18/07/19 14:50:33 2&lt;BR /&gt;11.8 0 18/07/19 14:55:33 2&lt;BR /&gt;12.2 0 18/07/19 15:00:33 .&lt;BR /&gt;12.3 0 18/07/19 15:05:33 .&lt;BR /&gt;12.3 0 18/07/19 15:10:33 .&lt;BR /&gt;12.4 0 18/07/19 15:15:33 .&lt;BR /&gt;15.6 0 18/07/19 16:45:33 .&lt;BR /&gt;15.7 0 18/07/19 16:50:33 .&lt;BR /&gt;16 0 18/07/19 16:55:33 .&lt;BR /&gt;16 0 18/07/19 17:00:33 3&lt;BR /&gt;15.9 0 18/07/19 17:05:34 3&lt;BR /&gt;16 0 18/07/19 17:10:34 3&lt;BR /&gt;16 0 18/07/19 17:15:32 3&lt;BR /&gt;15.9 0 18/07/19 17:20:32 3&lt;BR /&gt;7.7 1 18/07/19 20:45:32 3&lt;BR /&gt;10 1 18/07/19 20:45:38 3&lt;BR /&gt;10.2 0 18/07/19 20:50:32 3&lt;BR /&gt;10.2 0 18/07/19 20:55:33 3&lt;BR /&gt;;;;;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Sep 2022 08:05:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-a-row-corresponding-to-start-and-end-of-time/m-p/835532#M330324</guid>
      <dc:creator>SVSzwec</dc:creator>
      <dc:date>2022-09-28T08:05:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert a row corresponding to start and end of time period and take nearest value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-a-row-corresponding-to-start-and-end-of-time/m-p/835535#M330325</link>
      <description>&lt;P&gt;Try next code. I hope I understood you correctly:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set measures_tp;
   by datepart timepart;
        retain nxt;
       if first.datepert then do;
         nxt = timepart;
         timepart=0; output;
         timepart = nxt;
      end;
      
      output;
      if last.datepart then do;
         timepart = input('21:00',time5.);
         output;
       end;
run;

          &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 28 Sep 2022 08:05:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-a-row-corresponding-to-start-and-end-of-time/m-p/835535#M330325</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2022-09-28T08:05:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert a row corresponding to start and end of time period and take nearest value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-a-row-corresponding-to-start-and-end-of-time/m-p/835537#M330327</link>
      <description>&lt;P&gt;Thanks for the code Shmuel, sadly it's not quite what I need.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm hoping to have each time category sandwiched by two time points.&lt;/P&gt;&lt;P&gt;For time_cat = 0, I want to create an entry with time 00:00:00 and an entry at time 06:00:00.&lt;/P&gt;&lt;P&gt;For time_cat = 1, an entry at the start with time 06:00:00 and one at the end at 10:00:00..&lt;/P&gt;&lt;P&gt;For time_cat = 2, one starting at 11:00:00AM and one ending at 15:00:00.&lt;/P&gt;&lt;P&gt;For time_cat = 3, the first entry starting at 17:00:00 and one final entry ending at 21:00:00.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In each case I'm hoping to create those entries and pull the sensor value that occurs closest to that time point.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Sep 2022 08:19:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-a-row-corresponding-to-start-and-end-of-time/m-p/835537#M330327</guid>
      <dc:creator>SVSzwec</dc:creator>
      <dc:date>2022-09-28T08:19:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert a row corresponding to start and end of time period and take nearest value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-a-row-corresponding-to-start-and-end-of-time/m-p/835611#M330341</link>
      <description>&lt;P&gt;I assume:&lt;/P&gt;
&lt;P&gt;1) your data have more then one day.&lt;/P&gt;
&lt;P&gt;2) the data is already sorted&amp;nbsp;&amp;nbsp;by datepart timepart and&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;time_cat is assigned correctly according to timepart/&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In such case I preffer to create all time_cat(s) entries and merge them with the given data&amp;nbsp;as in next tested code. Do you realy need the sensor on the new added rows?&lt;/P&gt;
&lt;P&gt;It can be done within the merge step or in a new step by using similar methods as in my previous post.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code is:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let dt_start = '18JUL2019'd;  /*** adapt dt_start dt_end dates as need ***/
%let dt_end = '18JUL2019'd;
data cats;
  format datepart DDMMYY. timepart TIME.;

  do datepart = &amp;amp;dt_start to &amp;amp;dt_end by 1;
     timepart = 0; time_cat=0; output;
	 timepart = input('06:00't,hhmm5.); time_cat=0; output;
	 
	 timepart = input('06:00't,hhmm5.); time_cat=1; output;
	 timepart = input('10:00't,hhmm5.); time_cat=1; output;
	 
	 timepart = input('11:00't,hhmm5.); time_cat=2; output;
	 timepart = input('15:00't,hhmm5.); time_cat=2; output;

	 timepart = input('17:00't,hhmm5.); time_cat=3; output;
	 timepart = input('21:00't,hhmm5.); time_cat=3; output;
  end;
run;

data temp;
 merge measure_tp cats;
   by datepart timepart;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Sep 2022 14:13:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-a-row-corresponding-to-start-and-end-of-time/m-p/835611#M330341</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2022-09-28T14:13:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert a row corresponding to start and end of time period and take nearest value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-a-row-corresponding-to-start-and-end-of-time/m-p/835730#M330421</link>
      <description>&lt;P&gt;Many thanks Shmuel!&amp;nbsp; The combination of your two blocks of code do exactly what I want for this!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Sep 2022 23:45:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-a-row-corresponding-to-start-and-end-of-time/m-p/835730#M330421</guid>
      <dc:creator>SVSzwec</dc:creator>
      <dc:date>2022-09-28T23:45:38Z</dc:date>
    </item>
  </channel>
</rss>

