<?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: Creating time intervals for longitudinal data, and taking averages within intervals in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-time-intervals-for-longitudinal-data-and-taking/m-p/793411#M254304</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;Any advice on how to approach this problem? Even just the first step of creating the 5-minute time interval flags indicating that there are multiple observations. Thank you!&lt;/P&gt;</description>
    <pubDate>Mon, 31 Jan 2022 00:08:29 GMT</pubDate>
    <dc:creator>luch25</dc:creator>
    <dc:date>2022-01-31T00:08:29Z</dc:date>
    <item>
      <title>Creating time intervals for longitudinal data, and taking averages within intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-time-intervals-for-longitudinal-data-and-taking/m-p/793299#M254244</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm looking for some advice on how to go about coding this. I have a dataset with longitudinal measurements of heart rate (HR) and respirations (resp). Each individual (ID) has multiple measurements. The three main things I need to do are:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;By unique ID, flag measurements that are taken within 5 minutes of each other (flag=1)&lt;/LI&gt;
&lt;LI&gt;For measurements that are NOT taken within 5 minutes of another measurement, retain the original value of HR and resp.&lt;/LI&gt;
&lt;LI&gt;For measurements that are taken within 5 minutes of each other, apply these rules to produce the final dataset
&lt;OL class="lia-list-style-type-lower-alpha"&gt;
&lt;LI&gt;If there are 2 measurements within the 5-minute interval, take the average of the measurements, as well as the datetime value to produce a new value&lt;/LI&gt;
&lt;LI&gt;If there are &amp;gt;2 measurements within the 5-minute interval, delete the first value, and take the average fo the remaining measurements.&amp;nbsp;&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Code for an example dataset are provided below. I've also provided a description of "what's going on" for each individual below the code.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input id datetime hr resp;
	format datetime datetime22.3.;
	cards;
1 19MAR2009:13:15:00.000 90 16
1 19MAR2009:13:17:00.000 70 18
1 19MAR2009:13:18:00.000 80 17
1 19MAR2009:13.22:00.000 100 22
1 23MAR2009:10:36:00.000 83 21
2 29OCT2009:10:36:00.000 121 13
2 29OCT2009:10:38:00.000 83 14
2 08JUL2011:13:04:00.000 131 18
3 07SEP2016:14:26:59.000 50 12
3 06APR2017:13:39:00.000 76 18
3 08JUL2017:13:04:00.000 98 17
3 08JUL2017:13:08:00.000 96 18
3 24OCT2017:15:11:00.000 80 19
4 18APR2012:08:42:00.000 80 17
4 31OCT2012:10:36:00.000 96 19
4 31OCT2012:10:37:00.000 103 21
4 31OCT2012:10:38:00.000 105 25
4 31OCT2012:10:39:00.000 110 28
5 06JAN2008:05:37:00.000 98 17
5 15FEB2010:10:24:00.000 110 18
;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;ID=1 has two "5 minute intervals". The first 3 measurements make up one interval (delete the one at 13:15 and take the average of 13:17 and 13:18. Then the 2nd, 3rd, and 4th measurements make up a second interval (delete the one at 13:17, and take the average of 13:18 and 13:22).&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;ID=2 has one "5 minute interval" with 2 measurements, so take the average of these two&lt;/LI&gt;
&lt;LI&gt;ID=3 has two measurements within a "5 minute interval"&lt;/LI&gt;
&lt;LI&gt;ID=4 has 4 measurements within a "5 minute interval". Delete the one at 10:36 and take the average of the remaining ones&lt;/LI&gt;
&lt;LI&gt;ID=5 does not have any measurements within a 5 minute interval&lt;/LI&gt;
&lt;LI&gt;And as you can see, I want to retain measurements even if they are not in the 5 minute interval (flag=0)!&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Applying these rules, this is what I envision the final dataset looking like:&lt;/P&gt;
&lt;TABLE width="580"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="87"&gt;ID&lt;/TD&gt;
&lt;TD width="232"&gt;Datetime (format=datetime22.3)&lt;/TD&gt;
&lt;TD width="87"&gt;HR2&lt;/TD&gt;
&lt;TD width="87"&gt;Resp2&lt;/TD&gt;
&lt;TD width="87"&gt;Flag&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;19MAR2009:13:17:30.000&lt;/TD&gt;
&lt;TD&gt;75&lt;/TD&gt;
&lt;TD&gt;17.5&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;19MAR2009:13.19:00.000&lt;/TD&gt;
&lt;TD&gt;83.3&lt;/TD&gt;
&lt;TD&gt;19&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;23MAR2009:10:36:00.000&lt;/TD&gt;
&lt;TD&gt;83&lt;/TD&gt;
&lt;TD&gt;21&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;29OCT2009:10:37:00.000&lt;/TD&gt;
&lt;TD&gt;102&lt;/TD&gt;
&lt;TD&gt;13.5&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&g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R&gt;
&lt;TR&gt;
&lt;TD&gt;5&lt;/TD&gt;
&lt;TD&gt;06JAN2008:05:37:00.000&lt;/TD&gt;
&lt;TD&gt;98&lt;/TD&gt;
&lt;TD&gt;17&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;5&lt;/TD&gt;
&lt;TD&gt;15FEB2010:10:24:00.000&lt;/TD&gt;
&lt;TD&gt;110&lt;/TD&gt;
&lt;TD&gt;18&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any advice on how to handle this would be hugely appreciated! Thank you in advance!&lt;/P&gt;</description>
      <pubDate>Sat, 29 Jan 2022 18:11:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-time-intervals-for-longitudinal-data-and-taking/m-p/793299#M254244</guid>
      <dc:creator>luch25</dc:creator>
      <dc:date>2022-01-29T18:11:29Z</dc:date>
    </item>
    <item>
      <title>Re: Creating time intervals for longitudinal data, and taking averages within intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-time-intervals-for-longitudinal-data-and-taking/m-p/793301#M254245</link>
      <description>&lt;P&gt;First of all, thank you for providing usable sample data and a clear explanation &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Secondly. When two obs are within 5 min of each other, these two obs should become 1 obs, correct?&lt;/P&gt;</description>
      <pubDate>Sat, 29 Jan 2022 18:32:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-time-intervals-for-longitudinal-data-and-taking/m-p/793301#M254245</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-01-29T18:32:18Z</dc:date>
    </item>
    <item>
      <title>Re: Creating time intervals for longitudinal data, and taking averages within intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-time-intervals-for-longitudinal-data-and-taking/m-p/793303#M254246</link>
      <description>&lt;P&gt;Thank you for the clarification! Yes, that is correct. If there are two observations within 5 minutes of each other, they should effectively become a single (averaged) observation in the new dataset. The datetime variable would also be averaged.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The caveat is if there are &amp;gt;2 observations within 5 minutes of each other, the first observation is completely deleted. Then the remaining observations are averaged into a single observation. So if there are 3 measurements within 5 minutes, they become 1; if there are 4 measurements, they also become 1; etc.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 29 Jan 2022 18:43:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-time-intervals-for-longitudinal-data-and-taking/m-p/793303#M254246</guid>
      <dc:creator>luch25</dc:creator>
      <dc:date>2022-01-29T18:43:41Z</dc:date>
    </item>
    <item>
      <title>Re: Creating time intervals for longitudinal data, and taking averages within intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-time-intervals-for-longitudinal-data-and-taking/m-p/793411#M254304</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;Any advice on how to approach this problem? Even just the first step of creating the 5-minute time interval flags indicating that there are multiple observations. Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 31 Jan 2022 00:08:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-time-intervals-for-longitudinal-data-and-taking/m-p/793411#M254304</guid>
      <dc:creator>luch25</dc:creator>
      <dc:date>2022-01-31T00:08:29Z</dc:date>
    </item>
    <item>
      <title>Re: Creating time intervals for longitudinal data, and taking averages within intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-time-intervals-for-longitudinal-data-and-taking/m-p/793430#M254320</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/343488"&gt;@luch25&lt;/a&gt;,&amp;nbsp;sure just been busy. I'll post a solution during the day &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 31 Jan 2022 07:26:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-time-intervals-for-longitudinal-data-and-taking/m-p/793430#M254320</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-01-31T07:26:06Z</dc:date>
    </item>
    <item>
      <title>Re: Creating time intervals for longitudinal data, and taking averages within intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-time-intervals-for-longitudinal-data-and-taking/m-p/793454#M254338</link>
      <description>&lt;P&gt;Oh it's no problem at all! Take your time. Just sounded like you were onto something &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 31 Jan 2022 12:07:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-time-intervals-for-longitudinal-data-and-taking/m-p/793454#M254338</guid>
      <dc:creator>luch25</dc:creator>
      <dc:date>2022-01-31T12:07:33Z</dc:date>
    </item>
    <item>
      <title>Re: Creating time intervals for longitudinal data, and taking averages within intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-time-intervals-for-longitudinal-data-and-taking/m-p/793455#M254339</link>
      <description>Oh it's no problem at all! Take your time. Just sounded like you were onto something &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;</description>
      <pubDate>Mon, 31 Jan 2022 12:08:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-time-intervals-for-longitudinal-data-and-taking/m-p/793455#M254339</guid>
      <dc:creator>luch25</dc:creator>
      <dc:date>2022-01-31T12:08:18Z</dc:date>
    </item>
  </channel>
</rss>

