<?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 flag first observation of a Patient as NULL? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/926904#M364795</link>
    <description>&lt;P&gt;Just divide!&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data TIME_GAP;
  set FORM;
  by site pt start_time;
  hours1=dif(start_time);
  hours2=-sum(-start_time,lag(save_time));
  if first.pt then call missing(of hours1 hours2);
  else do;
    hours1=hours1/'01:00:00't ;
    hours2=hours2/'01:00:00't;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 03 May 2024 14:38:22 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-05-03T14:38:22Z</dc:date>
    <item>
      <title>How to flag first observation of a Patient as NULL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/926846#M364760</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I need some help with writing a SAS code to handle the following scenario.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a data set that has multiple sites; each site has multiple patients; and each patient can have data of up to 7 days (one row represents one day). The data set has form start and save time. Data set is also setup to have no duplicates. First day data of each patient should always have the previous save and open time (both are user defined variables) as NULL. e.g., sr.1, 8, and 9 will have previous open and save time variables as NULL. And using these variables,&amp;nbsp;I want to calculate the the following for each patient at a site:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;number of hours a patient took between today's and yesterday's start times (i.e., from today's start to previous start) and&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;number of hours a patient took between today's open and yesterday's start times (i.e., from today's start to previous day save)&lt;/LI&gt;
&lt;/UL&gt;
&lt;TABLE style="width: 600px;" width="600"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;Sr.&lt;/TD&gt;
&lt;TD width="80"&gt;PT&lt;/TD&gt;
&lt;TD width="76"&gt;SITE&lt;/TD&gt;
&lt;TD width="107"&gt;START_TIME&lt;/TD&gt;
&lt;TD width="107"&gt;SAVE_TIME&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;001&lt;/TD&gt;
&lt;TD&gt;01&lt;/TD&gt;
&lt;TD&gt;4/9/2024 16:44&lt;/TD&gt;
&lt;TD&gt;4/9/2024 16:46&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;001&lt;/TD&gt;
&lt;TD&gt;01&lt;/TD&gt;
&lt;TD&gt;4/10/2024 16:39&lt;/TD&gt;
&lt;TD&gt;4/10/2024 16:40&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;001&lt;/TD&gt;
&lt;TD&gt;01&lt;/TD&gt;
&lt;TD&gt;4/11/2024 16:07&lt;/TD&gt;
&lt;TD&gt;4/11/2024 16:08&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;001&lt;/TD&gt;
&lt;TD&gt;01&lt;/TD&gt;
&lt;TD&gt;4/12/2024 16:05&lt;/TD&gt;
&lt;TD&gt;4/12/2024 16:06&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;5&lt;/TD&gt;
&lt;TD&gt;001&lt;/TD&gt;
&lt;TD&gt;01&lt;/TD&gt;
&lt;TD&gt;4/13/2024 20:05&lt;/TD&gt;
&lt;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&lt;/TD&gt;
&lt;TD&gt;4/9/2024 19:32&lt;/TD&gt;
&lt;TD&gt;4/9/2024 19:34&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;26&lt;/TD&gt;
&lt;TD&gt;005&lt;/TD&gt;
&lt;TD&gt;02&lt;/TD&gt;
&lt;TD&gt;4/10/2024 18:45&lt;/TD&gt;
&lt;TD&gt;4/10/2024 18:47&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;27&lt;/TD&gt;
&lt;TD&gt;005&lt;/TD&gt;
&lt;TD&gt;02&lt;/TD&gt;
&lt;TD&gt;4/11/2024 18:02&lt;/TD&gt;
&lt;TD&gt;4/11/2024 18:04&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;28&lt;/TD&gt;
&lt;TD&gt;005&lt;/TD&gt;
&lt;TD&gt;02&lt;/TD&gt;
&lt;TD&gt;4/12/2024 18:04&lt;/TD&gt;
&lt;TD&gt;4/12/2024 18:07&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;29&lt;/TD&gt;
&lt;TD&gt;005&lt;/TD&gt;
&lt;TD&gt;02&lt;/TD&gt;
&lt;TD&gt;4/13/2024 18:01&lt;/TD&gt;
&lt;TD&gt;4/13/2024 18:03&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;30&lt;/TD&gt;
&lt;TD&gt;005&lt;/TD&gt;
&lt;TD&gt;02&lt;/TD&gt;
&lt;TD&gt;4/14/2024 18:06&lt;/TD&gt;
&lt;TD&gt;4/14/2024 18:08&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks so much in advance for all the help.&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2024 20:12:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/926846#M364760</guid>
      <dc:creator>SAS_USER_928</dc:creator>
      <dc:date>2024-05-02T20:12:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag first observation of a Patient as NULL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/926851#M364763</link>
      <description>&lt;P&gt;One key issue to resolve is this.&amp;nbsp; What is in your TIME variables?&amp;nbsp; Are they character strings, or are they numeric measurements of time (number of seconds)?&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2024 20:58:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/926851#M364763</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2024-05-02T20:58:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag first observation of a Patient as NULL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/926857#M364767</link>
      <description>&lt;P&gt;Seems simple enough.&lt;/P&gt;
&lt;P&gt;First let's convert your listing into a dataset.&amp;nbsp; Your listing is using a tricky style for the DATETIME values.&amp;nbsp; We can use the ANYDTDTM informat to read those, but let's make sure to tell it that the MONTH is the first number not the DAY of the month.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options datestyle=mdy;
data have;
  infile cards truncover dsd dlm='|';
  input row PT $ SITE $ (START_TIME SAVE_TIME) ( :anydtdtm. );
  format START_TIME SAVE_TIME datetime19.;
cards;
1|001|01|4/9/2024 16:44|4/9/2024 16:46
2|001|01|4/10/2024 16:39|4/10/2024 16:40
3|001|01|4/11/2024 16:07|4/11/2024 16:08
4|001|01|4/12/2024 16:05|4/12/2024 16:06
5|001|01|4/13/2024 20:05|4/13/2024 20:06
6|001|01|4/14/2024 19:31|4/14/2024 19:52
7|001|01|4/15/2024 16:25|4/15/2024 16:25
8|002|01|4/15/2024 16:19|4/15/2024 16:20
9|003|01|4/15/2024 16:21|4/15/2024 16:22
10|002|02|4/3/2024 18:34|4/3/2024 18:41
11|002|02|4/4/2024 18:03|4/4/2024 18:09
12|002|02|4/6/2024 21:11|4/6/2024 21:14
13|002|02|4/6/2024 21:14|4/6/2024 21:16
14|002|02|4/7/2024 18:17|4/7/2024 18:20
15|002|02|4/8/2024 18:12|4/8/2024 18:14
16|002|02|4/9/2024 18:06|4/9/2024 18:08
17|004|02|4/5/2024 18:03|4/5/2024 18:10
18|004|02|4/6/2024 16:27|4/6/2024 16:32
19|004|02|4/7/2024 19:18|4/7/2024 19:22
20|004|02|4/8/2024 18:01|4/8/2024 18:07
21|004|02|4/9/2024 18:06|4/9/2024 18:16
22|004|02|4/10/2024 18:08|4/10/2024 18:10
23|004|02|4/11/2024 19:22|4/11/2024 19:26
24|005|02|4/8/2024 18:05|4/8/2024 18:07
25|005|02|4/9/2024 19:32|4/9/2024 19:34
26|005|02|4/10/2024 18:45|4/10/2024 18:47
27|005|02|4/11/2024 18:02|4/11/2024 18:04
28|005|02|4/12/2024 18:04|4/12/2024 18:07
29|005|02|4/13/2024 18:01|4/13/2024 18:03
30|005|02|4/14/2024 18:06|4/14/2024 18:08
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To get the difference between the current and the previous value just use the DIF() function.&amp;nbsp; To get the difference between the current START and previous SAVE we will need to use the LAG() function.&amp;nbsp; We can use BY group processing to know when to ignore the calculated values since the previous observation was for someone else.&amp;nbsp; Note the extra minus in the second calculation eliminates the note about multiplying -1 times a missing value for the first observation since there is not previous value of SAVE_TIME for LAG() to return.&lt;/P&gt;
&lt;P&gt;Let's just leave the differences in seconds.&amp;nbsp; If you want them in hours just divide by the number of seconds in an hour. (Hint you can use '01:00:00't if you don't want to bother to figure out how many seconds that is.)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by site pt start_time;
  time1=dif(start_time);
  time2=-sum(-start_time,lag(save_time));
  if first.pt then call missing(of time1 time2);
  format time: time12.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;row PT  SITE          START_TIME           SAVE_TIME        time1        time2

  1 001  01   09APR2024:16:44:00  09APR2024:16:46:00            .            .
  2 001  01   10APR2024:16:39:00  10APR2024:16:40:00     23:55:00     23:53:00
  3 001  01   11APR2024:16:07:00  11APR2024:16:08:00     23:28:00     23:27:00
  4 001  01   12APR2024:16:05:00  12APR2024:16:06:00     23:58:00     23:57:00
  5 001  01   13APR2024:20:05:00  13APR2024:20:06:00     28:00:00     27:59:00
  6 001  01   14APR2024:19:31:00  14APR2024:19:52:00     23:26:00     23:25:00
  7 001  01   15APR2024:16:25:00  15APR2024:16:25:00     20:54:00     20:33:00
  8 002  01   15APR2024:16:19:00  15APR2024:16:20:00            .            .
  9 003  01   15APR2024:16:21:00  15APR2024:16:22:00            .            .
 10 002  02   03APR2024:18:34:00  03APR2024:18:41:00            .            .
 11 002  02   04APR2024:18:03:00  04APR2024:18:09:00     23:29:00     23:22:00
 12 002  02   06APR2024:21:11:00  06APR2024:21:14:00     51:08:00     51:02:00
 13 002  02   06APR2024:21:14:00  06APR2024:21:16:00      0:03:00      0:00:00
 14 002  02   07APR2024:18:17:00  07APR2024:18:20:00     21:03:00     21:01:00
 15 002  02   08APR2024:18:12:00  08APR2024:18:14:00     23:55:00     23:52:00
 16 002  02   09APR2024:18:06:00  09APR2024:18:08:00     23:54:00     23:52:00
 17 004  02   05APR2024:18:03:00  05APR2024:18:10:00            .            .
 18 004  02   06APR2024:16:27:00  06APR2024:16:32:00     22:24:00     22:17:00
 19 004  02   07APR2024:19:18:00  07APR2024:19:22:00     26:51:00     26:46:00
 20 004  02   08APR2024:18:01:00  08APR2024:18:07:00     22:43:00     22:39:00
 21 004  02   09APR2024:18:06:00  09APR2024:18:16:00     24:05:00     23:59:00
 22 004  02   10APR2024:18:08:00  10APR2024:18:10:00     24:02:00     23:52:00
 23 004  02   11APR2024:19:22:00  11APR2024:19:26:00     25:14:00     25:12:00
 24 005  02   08APR2024:18:05:00  08APR2024:18:07:00            .            .
 25 005  02   09APR2024:19:32:00  09APR2024:19:34:00     25:27:00     25:25:00
 26 005  02   10APR2024:18:45:00  10APR2024:18:47:00     23:13:00     23:11:00
 27 005  02   11APR2024:18:02:00  11APR2024:18:04:00     23:17:00     23:15:00
 28 005  02   12APR2024:18:04:00  12APR2024:18:07:00     24:02:00     24:00:00
 29 005  02   13APR2024:18:01:00  13APR2024:18:03:00     23:57:00     23:54:00
 30 005  02   14APR2024:18:06:00  14APR2024:18:08:00     24:05:00     24:03:00
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2024 22:42:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/926857#M364767</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-05-02T22:42:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag first observation of a Patient as NULL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/926892#M364788</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My data set is in a sas7bdat format and the two date variables are in&amp;nbsp;DATETIME20. The entire data set is already pre-processed (cleaned and sorted by Site, PT, and Start Time).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The only two things I want to do are:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. to calculate the difference in hours between the current start and previous save time for the same patient. and&lt;/P&gt;
&lt;P&gt;2. to calculate the difference in hours between the current save and previous save time for the same patient.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I already have the following code but it is not populating some values correctly.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV&gt;DATA TIME_GAP;&lt;/DIV&gt;
&lt;DIV&gt;SET FORM;&lt;/DIV&gt;
&lt;DIV&gt;BY SITE PT START_TIME;&lt;/DIV&gt;
&lt;DIV&gt;/* Initialize below variables */&lt;/DIV&gt;
&lt;DIV&gt;IF FIRST.SITE OR FIRST.PT THEN DO;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; PREV_START_TIME = .;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; PREV_SAVE_TIME = .; END;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;ELSE DO;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; PREV_PT = LAG(PT);&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; PREV_SAVE_TIME = LAG(SAVE_TIME);&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; FORMAT PREV_SAVE_TIME DATETIME20.;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; PREV_START_TIME = LAG(START_TIME);&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; FORMAT PREV_START_TIME DATETIME20.;&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; IF NOT MISSING(PREV_SAVE_TIME) AND PT = PREV_PT&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; &amp;nbsp; &amp;nbsp; THEN START_SAVE_DIFF = ROUND(((START_TIME - PREV_SAVE_TIME) / 3600), 0.01);&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;ELSE START_SAVE_DIFF = "";&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;IF NOT MISSING(PREV_START_TIME) AND PT = PREV_PT&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;THEN START_START_DIFF = ROUND(((START_TIME - PREV_START_TIME) / 3600), 0.01);&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ELSE START_START_DIFF = ""; END;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;RUN;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Fri, 03 May 2024 13:17:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/926892#M364788</guid>
      <dc:creator>SAS_USER_928</dc:creator>
      <dc:date>2024-05-03T13:17:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag first observation of a Patient as NULL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/926894#M364789</link>
      <description>&lt;P&gt;Thanks, Tom.&lt;/P&gt;
&lt;P&gt;The code seems to work fine but a few things to add or needs modification for me to use it. You have used datalines but my data set is in sas7bdat format. Also the two data variables are in&amp;nbsp;DATETIME20. format. The difference in your code below is in hours:minutes:seconds format. I need it numeric such as 25 or 30 so that I can use it further to calculate average of hours per site.&lt;/P&gt;
&lt;P&gt;Thank so much in advance for your time and help.&lt;/P&gt;</description>
      <pubDate>Fri, 03 May 2024 13:24:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/926894#M364789</guid>
      <dc:creator>SAS_USER_928</dc:creator>
      <dc:date>2024-05-03T13:24:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag first observation of a Patient as NULL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/926895#M364790</link>
      <description>&lt;P&gt;I only include the first data step so I had your dataset.&amp;nbsp; You can just start with the second step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DATETIME and TIME values are in seconds.&amp;nbsp; You can easily convert them into hours using basic arithmetic. The only question is what to do with the minutes and seconds.&amp;nbsp; Do you want the HOURS to include the fraction of an hour?&amp;nbsp; Like 23.5 for '23:30:00't ?&lt;/P&gt;
&lt;P&gt;If not do you want to eliminate the fraction of an hour?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;hours=int(time/'01:00:00't);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Do you want to round to the closest hour?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;hours=round(time/'01:00:00't);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Do you want to round UP to the next hour?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;hours=ceil(time/'01:00:00't);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 May 2024 13:32:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/926895#M364790</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-05-03T13:32:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag first observation of a Patient as NULL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/926901#M364793</link>
      <description>Yes, I like the HOURS to include fraction of an hour, such as 23.5 for 23 hours 30 seconds. So which line in the above code should be modified to address this? Thanks</description>
      <pubDate>Fri, 03 May 2024 14:14:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/926901#M364793</guid>
      <dc:creator>SAS_USER_928</dc:creator>
      <dc:date>2024-05-03T14:14:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag first observation of a Patient as NULL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/926904#M364795</link>
      <description>&lt;P&gt;Just divide!&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data TIME_GAP;
  set FORM;
  by site pt start_time;
  hours1=dif(start_time);
  hours2=-sum(-start_time,lag(save_time));
  if first.pt then call missing(of hours1 hours2);
  else do;
    hours1=hours1/'01:00:00't ;
    hours2=hours2/'01:00:00't;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 May 2024 14:38:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/926904#M364795</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-05-03T14:38:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag first observation of a Patient as NULL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/926906#M364796</link>
      <description>Thank you so much, Tom. Much appreciate it.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 03 May 2024 14:48:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/926906#M364796</guid>
      <dc:creator>SAS_USER_928</dc:creator>
      <dc:date>2024-05-03T14:48:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag first observation of a Patient as NULL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/930838#M366217</link>
      <description>&lt;P&gt;Hi there,&lt;BR /&gt;&lt;BR /&gt;I have the same dataset as above but with a minor change in the requirements. I want to calculate:&lt;BR /&gt;&lt;BR /&gt;1. the difference (in minutes) between start times (ONLY of the observations that have the same Start Dates) per site. This will be saved as Time1 (as above). Goal is to know how closely/far apart PTs at each site are opening their forms on each day (avg. difference). &lt;BR /&gt;2. the difference (in minutes) between previous observation's save time and current observation's start time (ONLY of the observations that have the same Start Dates) per site. This will be saved as Time2 as above. Goal is to know how closely/far apart PTs at each site are saving and opening their forms on each day (avg. difference). &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note: if there are no two observations with same Start Time at a site, time1 and time2 will be blank for both the observations.&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Thanking you in advance for the help. &lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jun 2024 16:42:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/930838#M366217</guid>
      <dc:creator>SAS_USER_928</dc:creator>
      <dc:date>2024-06-04T16:42:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag first observation of a Patient as NULL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/930845#M366220</link>
      <description>&lt;P&gt;Sounds like you just need change the logic used to find the first observation in a group.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if first.pt or (datepart(start_time) ne datepart(lag(start_time)))
    then call missing(of hours1 hours2);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Jun 2024 17:31:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/930845#M366220</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-06-04T17:31:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag first observation of a Patient as NULL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/930846#M366221</link>
      <description>&lt;P&gt;Thanks, Tom. &lt;BR /&gt;I still see values populated for each observation which is not required. I want to see the time difference ONLY if the datepart(start time) for the two PTs at a site is exactly the same. &lt;BR /&gt;&lt;BR /&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jun 2024 17:55:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/930846#M366221</guid>
      <dc:creator>SAS_USER_928</dc:creator>
      <dc:date>2024-06-04T17:55:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag first observation of a Patient as NULL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/930850#M366222</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/407996"&gt;@SAS_USER_928&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks, Tom. &lt;BR /&gt;I still see values populated for each observation which is not required. I want to see the time difference ONLY if the datepart(start time) for the two PTs at a site is exactly the same. &lt;BR /&gt;&lt;BR /&gt;Thanks&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You seem to have NEW problem that is only partially related to the old one.&lt;/P&gt;
&lt;P&gt;If you trying to compare times across patients then do not use FIRST.PT as part the criteria for finding the first observation.&lt;/P&gt;
&lt;P&gt;Perhaps your data is sorted by SITE and START_TIME instead?&amp;nbsp; If so what if you have two records from the same PT in a row?&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jun 2024 18:15:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/930850#M366222</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-06-04T18:15:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag first observation of a Patient as NULL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/930852#M366224</link>
      <description>Hi Tom,&lt;BR /&gt;&lt;BR /&gt;Yes, my data is currently sorted by SITE and START_TIME. &lt;BR /&gt;&lt;BR /&gt;Also, if I have two records from the same PT and with the same DATEPART(START_TIME), I am keeping both but do not need to calculate their time difference (i.e., time difference is to be calculated only if PTs are different and have the same DATEPART(START_TIME) and SITE).&lt;BR /&gt;&lt;BR /&gt;Thanks</description>
      <pubDate>Tue, 04 Jun 2024 19:34:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-observation-of-a-Patient-as-NULL/m-p/930852#M366224</guid>
      <dc:creator>SAS_USER_928</dc:creator>
      <dc:date>2024-06-04T19:34:02Z</dc:date>
    </item>
  </channel>
</rss>

