<?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 Time since last observation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Time-since-last-observation/m-p/925172#M364113</link>
    <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;I have a dataset that I would like to get the time difference from one observed value from the last observation value by patient_ID. Here is an example of what I want:.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
input Hour HR Temp SepsisLabel Patient_ID onset_time time_difference;
datalines;
0 88 36.11 0 34 -13
1 88 36.17 0 34 -12
2 88 .  0 34 -11
3 83.5 . 0 34 -10
4 80 .  0 34 -9
5 88 36.5 0 34 -8
6 91 . 0 34 -7
7 88 . 0 34 -6
8 80 .  0 34 -5
9 80 . 0 34 -4
10 80 . 0 34 -3
11 82 . 0 34 -2
12 77 . 0 34 -1
0 88 36 0 40 -5
1 88 36 0 40 -4
2 88 36 0 40 -3
3 88 36 0 40 -2
4 88 36 0 40 -1
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Please let me know what's the easiest way to get this! Chatgpt is struggling to understand how lag functions work, and so am I.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
    <pubDate>Mon, 22 Apr 2024 01:28:41 GMT</pubDate>
    <dc:creator>pdick2</dc:creator>
    <dc:date>2024-04-22T01:28:41Z</dc:date>
    <item>
      <title>Time since last observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-since-last-observation/m-p/925172#M364113</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;I have a dataset that I would like to get the time difference from one observed value from the last observation value by patient_ID. Here is an example of what I want:.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
input Hour HR Temp SepsisLabel Patient_ID onset_time time_difference;
datalines;
0 88 36.11 0 34 -13
1 88 36.17 0 34 -12
2 88 .  0 34 -11
3 83.5 . 0 34 -10
4 80 .  0 34 -9
5 88 36.5 0 34 -8
6 91 . 0 34 -7
7 88 . 0 34 -6
8 80 .  0 34 -5
9 80 . 0 34 -4
10 80 . 0 34 -3
11 82 . 0 34 -2
12 77 . 0 34 -1
0 88 36 0 40 -5
1 88 36 0 40 -4
2 88 36 0 40 -3
3 88 36 0 40 -2
4 88 36 0 40 -1
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Please let me know what's the easiest way to get this! Chatgpt is struggling to understand how lag functions work, and so am I.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 22 Apr 2024 01:28:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-since-last-observation/m-p/925172#M364113</guid>
      <dc:creator>pdick2</dc:creator>
      <dc:date>2024-04-22T01:28:41Z</dc:date>
    </item>
    <item>
      <title>Re: Time since last observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-since-last-observation/m-p/925174#M364114</link>
      <description>&lt;P&gt;Based on your data please explain the logic in detail for calculation of such differences.&lt;/P&gt;
&lt;P&gt;For example can you please explain how exactly you calculate time_difference= 5 with below data?&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1713749731002.png" style="width: 482px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/95734iB798FE4993DECD1E/image-dimensions/482x224?v=v2" width="482" height="224" role="button" title="Patrick_0-1713749731002.png" alt="Patrick_0-1713749731002.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Apr 2024 01:35:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-since-last-observation/m-p/925174#M364114</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-04-22T01:35:56Z</dc:date>
    </item>
    <item>
      <title>Re: Time since last observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-since-last-observation/m-p/925175#M364115</link>
      <description>&lt;PRE&gt;&lt;CODE class=""&gt;data have;
input Hour HR Temp SepsisLabel Patient_ID time_difference;
datalines;
0 88 36.11 0 34 -13
1 88 36.17 0 34 -12
2 88 .  0 34 -11
3 83.5 . 0 34 -10
4 80 .  0 34 -9
5 88 36.5 0 34 -8
6 91 . 0 34 -7
7 88 . 0 34 -6
8 80 .  0 34 -5
9 80 . 0 34 -4
10 80 . 0 34 -3
11 82 . 0 34 -2
12 77 . 0 34 -1
0 88 36 0 40 -5
1 88 36 0 40 -4
2 88 36 0 40 -3
3 88 36 0 40 -2
4 88 36 0 40 -1
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Opps. I left the "onset_time" from a input template from a previous dataset. This line of code should make more sense. Apologies!&lt;/P&gt;</description>
      <pubDate>Mon, 22 Apr 2024 01:38:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-since-last-observation/m-p/925175#M364115</guid>
      <dc:creator>pdick2</dc:creator>
      <dc:date>2024-04-22T01:38:45Z</dc:date>
    </item>
    <item>
      <title>Re: Time since last observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-since-last-observation/m-p/925177#M364116</link>
      <description>&lt;P&gt;It would really help if you could also explain the logic/formula in words for at least one or two of your observations.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1713750136584.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/95735i09A6D573D087C54A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1713750136584.png" alt="Patrick_0-1713750136584.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Apr 2024 01:42:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-since-last-observation/m-p/925177#M364116</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-04-22T01:42:55Z</dc:date>
    </item>
    <item>
      <title>Re: Time since last observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-since-last-observation/m-p/925178#M364117</link>
      <description>&lt;P&gt;Lets adjust that. Here is something that might be easier to work with:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
input Hour HR Temp SepsisLabel Patient_ID time_difference;
datalines;
0 88 36.11 0 34 -12
1 88 36.17 0 34 -11
2 88 .  0 34 -10
3 83.5 . 0 34 -9
4 80 .  0 34 -8
5 88 36.5 0 34 -7
6 91 . 0 34 -6
7 88 . 0 34 -5
8 80 .  0 34 -4
9 80 . 0 34 -3
10 80 . 0 34 -2
11 82 . 0 34 -1
12 77 . 0 34 0
0 88 36 0 40 -4
1 88 36 0 40 -3
2 88 36 0 40 -2
3 88 36 0 40 -1
4 88 36 0 40 0
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What I want is to label the hours since the last observation. For example, patient_ID=40 has their last observed hour = 4. This will be time_difference = 0. Then the previous row will have time_difference = -1 (coming from 3-4 = -1).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So ideally it would look something like this (for each unique patient_ID):&lt;/P&gt;&lt;P&gt;time_difference = (last observed hour for that unique patient_ID) - (hour of interest for that unique patient_ID) = a negative time difference per row.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think this will be easier if I do it in the format above, since I have another dataset in a similar format.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let me know if you need anything else, and thanks for helping! I'm slowly learning my way around SAS..&lt;/P&gt;</description>
      <pubDate>Mon, 22 Apr 2024 01:55:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-since-last-observation/m-p/925178#M364117</guid>
      <dc:creator>pdick2</dc:creator>
      <dc:date>2024-04-22T01:55:13Z</dc:date>
    </item>
    <item>
      <title>Re: Time since last observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-since-last-observation/m-p/925179#M364118</link>
      <description>&lt;P&gt;Below one way how to do this.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Hour HR Temp SepsisLabel Patient_ID time_difference;
datalines;
0 88 36.11 0 34 -12
1 88 36.17 0 34 -11
2 88 .  0 34 -10
3 83.5 . 0 34 -9
4 80 .  0 34 -8
5 88 36.5 0 34 -7
6 91 . 0 34 -6
7 88 . 0 34 -5
8 80 .  0 34 -4
9 80 . 0 34 -3
10 80 . 0 34 -2
11 82 . 0 34 -1
12 77 . 0 34 0
0 88 36 0 40 -4
1 88 36 0 40 -3
2 88 36 0 40 -2
3 88 36 0 40 -1
4 88 36 0 40 0
;

data last_hour_per_patient;
  set have;
  by Patient_ID hour;
  if last.Patient_ID;
  last_hour=hour;
  keep Patient_ID last_hour;
run;

data want;
  merge have last_hour_per_patient;
  by patient_id;
  time_diff_derived=hour-last_hour;
  drop last_hour;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Here a SQL approach:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table want2 as
  select l.*, l.hour-r.hour as time_diff_derived
  from have l
  inner join
  (select patient_id, max(hour) as hour from have group by patient_id) as r
  on l.patient_id=r.patient_id
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;And here a 3rd option using a "double dow loop" ...well "dou" actually. (dow loop is not official SAS terminology but you will find white papers about it).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do until(last.patient_id);
    set have(keep=Patient_ID hour);
    by Patient_ID hour;
  end;
  last_hour=hour;
  do until(last.patient_id);
    set have;
    by Patient_ID hour;
    time_difference_derived=hour-last_hour;
    drop last_hour;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Apr 2024 02:31:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-since-last-observation/m-p/925179#M364118</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-04-22T02:31:21Z</dc:date>
    </item>
    <item>
      <title>Re: Time since last observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-since-last-observation/m-p/925180#M364119</link>
      <description>1st set of codes worked! Thanks so much!</description>
      <pubDate>Mon, 22 Apr 2024 02:38:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-since-last-observation/m-p/925180#M364119</guid>
      <dc:creator>pdick2</dc:creator>
      <dc:date>2024-04-22T02:38:17Z</dc:date>
    </item>
  </channel>
</rss>

