<?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: Calculate difference between two dates in the same column conditional to other columns in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Calculate-difference-between-two-dates-in-the-same-column/m-p/447222#M69513</link>
    <description>&lt;P&gt;Your data appear to already by ordered by timestamp.&amp;nbsp; While the "proc sort; by user timestamp;" solution will work, it will be an expensive solution if the data set is big, especially if you want to re-sort to original order.&amp;nbsp; That a lot of disk input/output activity and disk storage.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To avoid that inefficiency, you can use a hash object, keyed on USER, holding the most recent timestamp and most recent date (in variables last_ts and last_dt).&amp;nbsp; Then you can retrieve from the hash object, compare DATE to LAST_DT and, if appropriate subtract TIMESTAMP minus LAST_TS:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id dummy :mmddyy10. Timestamp :time8.0 User :$4. date :mmddyy10.;
  timestamp=dummy*24*60*60+timestamp;
  drop dummy;
  format timestamp datetime19.0 date date9.;
datalines;
1390267 2/19/2018 20:01:33 Rick 2/19/2018 
1390273 2/19/2018 20:02:36 Mike 2/19/2018 
1390276 2/19/2018 20:03:15 Rick 2/19/2018 
1390763 2/19/2018 22:33:49 Fred 2/19/2018 
1390979 2/19/2018 22:54:18 Suzi 2/19/2018 
1391119 2/19/2018 23:12:39 Fred 2/19/2018 
1391124 2/19/2018 23:13:21 Fred 2/19/2018 
1392279 2/20/2018 22:54:00 Suzi 2/20/2018 
1391339 2/20/2018 23:12:00 Fred 2/20/2018 
run;


data want;
  set have  ;
  if _n_=1 then do;
    if 0 then set have (keep=date timestamp rename=(date=last_dt timestamp=last_ts));
    declare hash h (dataset:'have (keep=user date timestamp
                               rename=(date=last_dt timestamp=last_ts)');
     h.definekey('user');
	 h.definedata('last_dt','last_ts');
	 h.definedone();
  end;
  rc=h.find();
  if rc=0 and date=last_dt then dif=timestamp-last_ts;
  else dif=0;
  h.replace(key:user,data:date,data:timestamp);
  drop last_: rc;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The advantage is that no data sorting is required.&amp;nbsp; The disadvantage is the need to develop and understanding of the hash object, which&amp;nbsp;was not intuitively obvious to me when I first came across it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, what if a user has&amp;nbsp;2 simultaneous records?&amp;nbsp; Then DIF is a true zero, as opposed to a zero representing "not appropriate".&amp;nbsp; In that case you might drop the "&lt;EM&gt;&lt;STRONG&gt;else dif=0;&lt;/STRONG&gt;&lt;/EM&gt;" statement, which will leave DIF as a missing value.&lt;/P&gt;</description>
    <pubDate>Tue, 20 Mar 2018 18:38:03 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2018-03-20T18:38:03Z</dc:date>
    <item>
      <title>Calculate difference between two dates in the same column conditional to other columns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculate-difference-between-two-dates-in-the-same-column/m-p/446968#M69491</link>
      <description>&lt;P&gt;I have stacked user actions in a table and would like to calculate the number of seconds between each action for each user within that day. The issue is multiple users can access at once, so their actions are stacked, and I would like to only calculate that difference if the actions occurred on the same day.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;example:&lt;/P&gt;&lt;P&gt;data have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;id&lt;/TD&gt;&lt;TD&gt;Timestamp&lt;/TD&gt;&lt;TD&gt;User&lt;/TD&gt;&lt;TD&gt;date&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1390267&lt;/TD&gt;&lt;TD&gt;2/19/2018 20:01:33&lt;/TD&gt;&lt;TD&gt;Rick&lt;/TD&gt;&lt;TD&gt;2/19/2018&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1390273&lt;/TD&gt;&lt;TD&gt;2/19/2018 20:02:36&lt;/TD&gt;&lt;TD&gt;Mike&lt;/TD&gt;&lt;TD&gt;2/19/2018&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1390276&lt;/TD&gt;&lt;TD&gt;2/19/2018 20:03:15&lt;/TD&gt;&lt;TD&gt;Rick&lt;/TD&gt;&lt;TD&gt;2/19/2018&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1390763&lt;/TD&gt;&lt;TD&gt;2/19/2018 22:33:49&lt;/TD&gt;&lt;TD&gt;Fred&lt;/TD&gt;&lt;TD&gt;2/19/2018&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1390979&lt;/TD&gt;&lt;TD&gt;2/19/2018 22:54:18&lt;/TD&gt;&lt;TD&gt;Suzi&lt;/TD&gt;&lt;TD&gt;2/19/2018&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1391119&lt;/TD&gt;&lt;TD&gt;2/19/2018 23:12:39&lt;/TD&gt;&lt;TD&gt;Fred&lt;/TD&gt;&lt;TD&gt;2/19/2018&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1391124&lt;/TD&gt;&lt;TD&gt;2/19/2018 23:13:21&lt;/TD&gt;&lt;TD&gt;Fred&lt;/TD&gt;&lt;TD&gt;2/19/2018&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1392279&lt;/TD&gt;&lt;TD&gt;2/20/2018 22:54:00&lt;/TD&gt;&lt;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;&lt;TD&gt;2/19/2018&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1390979&lt;/TD&gt;&lt;TD&gt;2/19/2018 22:54:18&lt;/TD&gt;&lt;TD&gt;Suzi&lt;/TD&gt;&lt;TD&gt;2/19/2018&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1391119&lt;/TD&gt;&lt;TD&gt;2/19/2018 23:12:39&lt;/TD&gt;&lt;TD&gt;Fred&lt;/TD&gt;&lt;TD&gt;2/19/2018&lt;/TD&gt;&lt;TD&gt;2171&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1391124&lt;/TD&gt;&lt;TD&gt;2/19/2018 23:13:21&lt;/TD&gt;&lt;TD&gt;Fred&lt;/TD&gt;&lt;TD&gt;2/19/2018&lt;/TD&gt;&lt;TD&gt;42&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1392279&lt;/TD&gt;&lt;TD&gt;2/20/2018 22:54:00&lt;/TD&gt;&lt;TD&gt;Suzi&lt;/TD&gt;&lt;TD&gt;2/20/2018&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1391339&lt;/TD&gt;&lt;TD&gt;2/20/2018 23:12:00&lt;/TD&gt;&lt;TD&gt;Fred&lt;/TD&gt;&lt;TD&gt;2/20/2018&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Mon, 19 Mar 2018 23:16:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculate-difference-between-two-dates-in-the-same-column/m-p/446968#M69491</guid>
      <dc:creator>jogianni</dc:creator>
      <dc:date>2018-03-19T23:16:50Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate difference between two dates in the same column conditional to other columns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculate-difference-between-two-dates-in-the-same-column/m-p/446978#M69493</link>
      <description>&lt;P&gt;Please indicate which rows are being compared to get the values of 87 and 2171. The only one I can duplicate is the 42.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following possible solution assumes that the timestamp variable is an actual SAS datetime valued variable.&lt;/P&gt;
&lt;PRE&gt;proc sort data=have;
   by user timestamp;
run;

data want;
   set have;
   by user timestamp;
   difstamp = dif(timestamp);
   if first.user then do;
      dif=0;
   end;
   else dif=difstamp;
   drop difstamp;
run;

proc sort data=want;
   by timestamp;
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 19 Mar 2018 23:43:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculate-difference-between-two-dates-in-the-same-column/m-p/446978#M69493</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-03-19T23:43:46Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate difference between two dates in the same column conditional to other columns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculate-difference-between-two-dates-in-the-same-column/m-p/446984#M69494</link>
      <description>&lt;P&gt;Here is another alternate way by using LAG and INTCK functions&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have1;
by user TimeStamp;
run;

data want(drop=LAG);
format LAG DATETIME24.;
set have1;
LAG=LAG(TimeStamp);
by user;
if first.user then dif=0;
else dif=intCK("Second",LAG,TimeStamp);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Mar 2018 00:43:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculate-difference-between-two-dates-in-the-same-column/m-p/446984#M69494</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-03-20T00:43:21Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate difference between two dates in the same column conditional to other columns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculate-difference-between-two-dates-in-the-same-column/m-p/447006#M69496</link>
      <description>&lt;P&gt;I think the proposed solutions have the right idea, but fail to account for changes in the day.&amp;nbsp; A slightly improved version:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;proc sort data=have;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp;by user date timestamp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;data want;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp;set have;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp;by user date;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp;dif = dif(timestamp);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp;if first.date them dif = 0;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;If desired, you can always re-sort the data set by TIMESTAMP at that point.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Mar 2018 05:54:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculate-difference-between-two-dates-in-the-same-column/m-p/447006#M69496</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-03-20T05:54:35Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate difference between two dates in the same column conditional to other columns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculate-difference-between-two-dates-in-the-same-column/m-p/447222#M69513</link>
      <description>&lt;P&gt;Your data appear to already by ordered by timestamp.&amp;nbsp; While the "proc sort; by user timestamp;" solution will work, it will be an expensive solution if the data set is big, especially if you want to re-sort to original order.&amp;nbsp; That a lot of disk input/output activity and disk storage.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To avoid that inefficiency, you can use a hash object, keyed on USER, holding the most recent timestamp and most recent date (in variables last_ts and last_dt).&amp;nbsp; Then you can retrieve from the hash object, compare DATE to LAST_DT and, if appropriate subtract TIMESTAMP minus LAST_TS:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id dummy :mmddyy10. Timestamp :time8.0 User :$4. date :mmddyy10.;
  timestamp=dummy*24*60*60+timestamp;
  drop dummy;
  format timestamp datetime19.0 date date9.;
datalines;
1390267 2/19/2018 20:01:33 Rick 2/19/2018 
1390273 2/19/2018 20:02:36 Mike 2/19/2018 
1390276 2/19/2018 20:03:15 Rick 2/19/2018 
1390763 2/19/2018 22:33:49 Fred 2/19/2018 
1390979 2/19/2018 22:54:18 Suzi 2/19/2018 
1391119 2/19/2018 23:12:39 Fred 2/19/2018 
1391124 2/19/2018 23:13:21 Fred 2/19/2018 
1392279 2/20/2018 22:54:00 Suzi 2/20/2018 
1391339 2/20/2018 23:12:00 Fred 2/20/2018 
run;


data want;
  set have  ;
  if _n_=1 then do;
    if 0 then set have (keep=date timestamp rename=(date=last_dt timestamp=last_ts));
    declare hash h (dataset:'have (keep=user date timestamp
                               rename=(date=last_dt timestamp=last_ts)');
     h.definekey('user');
	 h.definedata('last_dt','last_ts');
	 h.definedone();
  end;
  rc=h.find();
  if rc=0 and date=last_dt then dif=timestamp-last_ts;
  else dif=0;
  h.replace(key:user,data:date,data:timestamp);
  drop last_: rc;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The advantage is that no data sorting is required.&amp;nbsp; The disadvantage is the need to develop and understanding of the hash object, which&amp;nbsp;was not intuitively obvious to me when I first came across it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, what if a user has&amp;nbsp;2 simultaneous records?&amp;nbsp; Then DIF is a true zero, as opposed to a zero representing "not appropriate".&amp;nbsp; In that case you might drop the "&lt;EM&gt;&lt;STRONG&gt;else dif=0;&lt;/STRONG&gt;&lt;/EM&gt;" statement, which will leave DIF as a missing value.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Mar 2018 18:38:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculate-difference-between-two-dates-in-the-same-column/m-p/447222#M69513</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-03-20T18:38:03Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate difference between two dates in the same column conditional to other columns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculate-difference-between-two-dates-in-the-same-column/m-p/447229#M69514</link>
      <description>&lt;P&gt;thanks, though this doesn't reset at day I used it and used&amp;nbsp;&lt;SPAN class="login-bold"&gt;&lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954" target="_self"&gt;Astounding&lt;/A&gt;'s if statement&amp;nbsp;&lt;/SPAN&gt;on there:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
format LAG DATETIME24.;
set have;
LAG=LAG(timestamp);
by user date;
if first.date then dif=0;
else dif=intCK("Second",LAG,timestamp);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Mar 2018 18:52:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculate-difference-between-two-dates-in-the-same-column/m-p/447229#M69514</guid>
      <dc:creator>jogianni</dc:creator>
      <dc:date>2018-03-20T18:52:51Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate difference between two dates in the same column conditional to other columns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculate-difference-between-two-dates-in-the-same-column/m-p/552216#M74595</link>
      <description>For a newbie it is a bit difficult to understand LAG=LAG(TimeStamp) why not pick var names that a different from SAS function names at these forums? Add a simple _ before the name at the very least.</description>
      <pubDate>Thu, 18 Apr 2019 17:56:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculate-difference-between-two-dates-in-the-same-column/m-p/552216#M74595</guid>
      <dc:creator>xd9813</dc:creator>
      <dc:date>2019-04-18T17:56:35Z</dc:date>
    </item>
  </channel>
</rss>

