<?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: Find maximum and minimum value within certain time span in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Find-maximum-and-minimum-value-within-certain-time-span/m-p/958983#M43071</link>
    <description>&lt;P&gt;Thank you PaigeMiller and other forum member for help!&lt;BR /&gt;&lt;BR /&gt;Here is what I need (area_under_curve I am just guess so fill with 1). I need the max in previous hour and area under curve for EVERY row in dataset.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;participant_id	datetime measure max_prev_hour area_under_curve
A1	30OCT20:10:16:24	1.028 1.028 1
A1	30OCT20:13:14:14	1.183 1.183 1
A1	30OCT20:13:24:12	1.277 1.277 1
A1	30OCT20:14:29:14	1.464 1.464 1
A1	30OCT20:14:44:13	1.619 1.619 1
A1	30OCT20:14:49:02	1.744 1.744 1
A1	30OCT20:14:56:15	1.806 1.806 1
A2	20NOV20:14:22:18	2.492 2.492 1
A2	20NOV20:14:37:17	2.492 2.492 1
A2	20NOV20:14:52:19	2.242 2.492 1
A2	20NOV20:14:59:20	2.305 2.305 1
A2	20NOV20:15:22:22	2.274 2.492 1
A2	20NOV20:15:37:21	2.398 2.398 1
A2	20NOV20:15:52:23	2.367 2.398 1
A3	18NOV20:10:25:11	2.367 2.367 1
A3	18NOV20:10:30:13	2.461 2.461 1
A3	18NOV20:11:45:16	2.461 2.461 1
A3	18NOV20:11:55:12	2.429 2.461 1
A3	18NOV20:12:45:21	2.616 2.616 1
A3	18NOV20:12:50:23	2.398 2.616 1
A3	18NOV20:13:55:28	2.429 2.429 1&lt;/PRE&gt;</description>
    <pubDate>Tue, 11 Feb 2025 23:06:34 GMT</pubDate>
    <dc:creator>linlin87</dc:creator>
    <dc:date>2025-02-11T23:06:34Z</dc:date>
    <item>
      <title>Find maximum and minimum value within certain time span</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Find-maximum-and-minimum-value-within-certain-time-span/m-p/958700#M43060</link>
      <description>&lt;P&gt;Hi Sas Community&lt;BR /&gt;&lt;BR /&gt;Have data and want &lt;STRONG&gt;for every datapoint&lt;/STRONG&gt; (by participant) to find maximum value (of &lt;STRONG&gt;measure&lt;/STRONG&gt;) in previous 1 hour, minimum value in following 1 hour, and the area under the curve in the previous 1 hour. How do I do this? &lt;BR /&gt;&lt;BR /&gt;Help will be really appreciate!&lt;BR /&gt;&lt;BR /&gt;Data example:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;participant_id	datetime	measure
A1	30OCT20:10:16:24	1.028037383
A1	30OCT20:13:14:14	1.183800623
A1	30OCT20:13:24:12	1.277258567
A1	30OCT20:14:29:14	1.464174455
A1	30OCT20:14:44:13	1.619937695
A1	30OCT20:14:49:02	1.744548287
A1	30OCT20:14:56:15	1.806853583
A2	20NOV20:14:22:18	2.492211838
A2	20NOV20:14:37:17	2.492211838
A2	20NOV20:14:52:19	2.242990654
A2	20NOV20:14:59:20	2.30529595
A2	20NOV20:15:22:22	2.274143302
A2	20NOV20:15:37:21	2.398753894
A2	20NOV20:15:52:23	2.367601246
A3	18NOV20:10:25:11	2.367601246
A3	18NOV20:10:30:13	2.46105919
A3	18NOV20:11:45:16	2.46105919
A3	18NOV20:11:55:12	2.429906542
A3	18NOV20:12:45:21	2.61682243
A3	18NOV20:12:50:23	2.398753894
A3	18NOV20:13:55:28	2.429906542
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Feb 2025 20:05:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Find-maximum-and-minimum-value-within-certain-time-span/m-p/958700#M43060</guid>
      <dc:creator>linlin87</dc:creator>
      <dc:date>2025-02-07T20:05:28Z</dc:date>
    </item>
    <item>
      <title>Re: Find maximum and minimum value within certain time span</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Find-maximum-and-minimum-value-within-certain-time-span/m-p/958701#M43061</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/426087"&gt;@linlin87&lt;/a&gt;, it would be helpful if you provided an example of your desired output. It's hard to grasp what you mean by previous/following hour (previous/following relative to what reference point?). Also, what do you mean by area under the curve? Is it for the distribution of those values for that participant?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Feb 2025 20:14:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Find-maximum-and-minimum-value-within-certain-time-span/m-p/958701#M43061</guid>
      <dc:creator>antonbcristina</dc:creator>
      <dc:date>2025-02-07T20:14:23Z</dc:date>
    </item>
    <item>
      <title>Re: Find maximum and minimum value within certain time span</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Find-maximum-and-minimum-value-within-certain-time-span/m-p/958702#M43062</link>
      <description>&lt;P&gt;for each datapoint in the dataset, by participant_id&lt;/P&gt;</description>
      <pubDate>Fri, 07 Feb 2025 20:15:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Find-maximum-and-minimum-value-within-certain-time-span/m-p/958702#M43062</guid>
      <dc:creator>linlin87</dc:creator>
      <dc:date>2025-02-07T20:15:11Z</dc:date>
    </item>
    <item>
      <title>Re: Find maximum and minimum value within certain time span</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Find-maximum-and-minimum-value-within-certain-time-span/m-p/958703#M43063</link>
      <description>&lt;P&gt;for each data point in data set, look over past hour and identify :&lt;BR /&gt;&lt;BR /&gt;a) maximum value&lt;BR /&gt;b) area under curve&lt;/P&gt;</description>
      <pubDate>Fri, 07 Feb 2025 20:17:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Find-maximum-and-minimum-value-within-certain-time-span/m-p/958703#M43063</guid>
      <dc:creator>linlin87</dc:creator>
      <dc:date>2025-02-07T20:17:59Z</dc:date>
    </item>
    <item>
      <title>Re: Find maximum and minimum value within certain time span</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Find-maximum-and-minimum-value-within-certain-time-span/m-p/958796#M43067</link>
      <description>&lt;P&gt;One way of getting the min and max. Still, please elaborate what you mean by "curve".&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let ref_dttm = 20NOV20:15:00:00;
%let Dttm_min_1hr = %sysfunc(intnx(DTHOUR,"&amp;amp;ref_dttm"dt,-1 ),datetime16.);
%let Dttm_plus_1hr = %sysfunc(intnx(DTHOUR,"&amp;amp;ref_dttm"dt,+1 ), datetime16.);

%put &amp;amp;Dttm_min_1hr 
&amp;amp;Dttm_plus_1hr;

proc sql;
	create table work.want as
		select participant_id, 
			min(case when dttm &amp;gt; "&amp;amp;ref_dttm"dt then . else Measure end) as min_prev_hr,
			max(case when dttm &amp;lt; "&amp;amp;ref_dttm"dt then . else Measure end) as max_next_hr
		from work.have
		where dttm between "&amp;amp;Dttm_min_1hr."dt and "&amp;amp;Dttm_plus_1hr"dt
		group by participant_id
	;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 10 Feb 2025 10:21:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Find-maximum-and-minimum-value-within-certain-time-span/m-p/958796#M43067</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2025-02-10T10:21:08Z</dc:date>
    </item>
    <item>
      <title>Re: Find maximum and minimum value within certain time span</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Find-maximum-and-minimum-value-within-certain-time-span/m-p/958808#M43069</link>
      <description>&lt;P&gt;I would think the best approach would be to use PROC IML, then finding records in the one hour before or one hour after is relatively easy as IML allows you to "look ahead" or "look back" easily. Then area under the curve is also easy to compute in PROC IML, see&amp;nbsp;&amp;nbsp;&lt;A href="https://blogs.sas.com/content/iml/2011/06/01/the-trapezoidal-rule-of-integration.html" target="_blank" rel="noopener"&gt;https://blogs.sas.com/content/iml/2011/06/01/the-trapezoidal-rule-of-integration.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/426087"&gt;@linlin87&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;you were asked to provide an example of the output, and you haven't provided that, you just re-stated the words of what you need. It would be really really really helpful if you provide an example of the output data set you want.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2025 13:35:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Find-maximum-and-minimum-value-within-certain-time-span/m-p/958808#M43069</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-02-10T13:35:31Z</dc:date>
    </item>
    <item>
      <title>Re: Find maximum and minimum value within certain time span</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Find-maximum-and-minimum-value-within-certain-time-span/m-p/958983#M43071</link>
      <description>&lt;P&gt;Thank you PaigeMiller and other forum member for help!&lt;BR /&gt;&lt;BR /&gt;Here is what I need (area_under_curve I am just guess so fill with 1). I need the max in previous hour and area under curve for EVERY row in dataset.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;participant_id	datetime measure max_prev_hour area_under_curve
A1	30OCT20:10:16:24	1.028 1.028 1
A1	30OCT20:13:14:14	1.183 1.183 1
A1	30OCT20:13:24:12	1.277 1.277 1
A1	30OCT20:14:29:14	1.464 1.464 1
A1	30OCT20:14:44:13	1.619 1.619 1
A1	30OCT20:14:49:02	1.744 1.744 1
A1	30OCT20:14:56:15	1.806 1.806 1
A2	20NOV20:14:22:18	2.492 2.492 1
A2	20NOV20:14:37:17	2.492 2.492 1
A2	20NOV20:14:52:19	2.242 2.492 1
A2	20NOV20:14:59:20	2.305 2.305 1
A2	20NOV20:15:22:22	2.274 2.492 1
A2	20NOV20:15:37:21	2.398 2.398 1
A2	20NOV20:15:52:23	2.367 2.398 1
A3	18NOV20:10:25:11	2.367 2.367 1
A3	18NOV20:10:30:13	2.461 2.461 1
A3	18NOV20:11:45:16	2.461 2.461 1
A3	18NOV20:11:55:12	2.429 2.461 1
A3	18NOV20:12:45:21	2.616 2.616 1
A3	18NOV20:12:50:23	2.398 2.616 1
A3	18NOV20:13:55:28	2.429 2.429 1&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Feb 2025 23:06:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Find-maximum-and-minimum-value-within-certain-time-span/m-p/958983#M43071</guid>
      <dc:creator>linlin87</dc:creator>
      <dc:date>2025-02-11T23:06:34Z</dc:date>
    </item>
    <item>
      <title>Re: Find maximum and minimum value within certain time span</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Find-maximum-and-minimum-value-within-certain-time-span/m-p/958990#M43072</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/426087"&gt;@linlin87&lt;/a&gt;&amp;nbsp; &lt;BR /&gt;Below returns the hourly max value same as in your sample data (max_prev_hour_derived) except for one row where I believe you've got it wrong.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input participant_id $ datetime:datetime. measure max_prev_hour area_under_curve;
    format datetime datetime20.;
    informat datetime anydtdtm20.;
    datalines;
A1 30OCT20:10:16:24 1.028 1.028 1
A1 30OCT20:13:14:14 1.183 1.183 1
A1 30OCT20:13:24:12 1.277 1.277 1
A1 30OCT20:14:29:14 1.464 1.464 1
A1 30OCT20:14:44:13 1.619 1.619 1
A1 30OCT20:14:49:02 1.744 1.744 1
A1 30OCT20:14:56:15 1.806 1.806 1
A2 20NOV20:14:22:18 2.492 2.492 1
A2 20NOV20:14:37:17 2.492 2.492 1
A2 20NOV20:14:52:19 2.242 2.492 1
A2 20NOV20:14:59:20 2.305 2.305 1
A2 20NOV20:15:22:22 2.274 2.492 1
A2 20NOV20:15:37:21 2.398 2.398 1
A2 20NOV20:15:52:23 2.367 2.398 1
A3 18NOV20:10:25:11 2.367 2.367 1
A3 18NOV20:10:30:13 2.461 2.461 1
A3 18NOV20:11:45:16 2.461 2.461 1
A3 18NOV20:11:55:12 2.429 2.461 1
A3 18NOV20:12:45:21 2.616 2.616 1
A3 18NOV20:12:50:23 2.398 2.616 1
A3 18NOV20:13:55:28 2.429 2.429 1
;
run;

data want(drop=_:);
  array a_m {2,10} 8 _temporary_;
  set have;
  by participant_id datetime;
  if first.participant_id then call missing(of a_m[*]);
  
  _i_curr=mod(_n_-1,10)+1;
  a_m[1,_i_curr]=datetime;
  a_m[2,_i_curr]=measure;
  
  max_prev_hour_derived=a_m[2,_i_curr];
  do _ind=dim2(a_m)-1 to 1 by -1;
    _i_start=mod(_n_-1+_ind,10)+1;
    if 0&amp;lt;=datetime-a_m[1,_i_start]&amp;lt;3600 then
      do;
        max_prev_hour_derived=max(max_prev_hour_derived,a_m[2,_i_start]);
      end;
    else leave;
  end;
  comp_flg=(max_prev_hour_derived=max_prev_hour);
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1739321511949.png" style="width: 673px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104551i7F2803EEEA28E6A5/image-dimensions/673x442?v=v2" width="673" height="442" role="button" title="Patrick_0-1739321511949.png" alt="Patrick_0-1739321511949.png" /&gt;&lt;/span&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>Wed, 12 Feb 2025 00:52:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Find-maximum-and-minimum-value-within-certain-time-span/m-p/958990#M43072</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2025-02-12T00:52:03Z</dc:date>
    </item>
  </channel>
</rss>

