<?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: count average time interval in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/count-average-time-interval/m-p/487623#M127082</link>
    <description>&lt;P&gt;Any calculation that relies on data ordering is simpler with a datastep in SAS:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data cd;
set customer_data;
date = dhms(date, 0, 0, time);
format date datetime17.;
drop time;
run;


proc sort data=cd; by customer_id date; run;

data avgTimes;
do nbInt = 1 by 1 until (last.customer_id);
    set cd; by customer_id;
    if first.customer_id then firstTime = date;
    if last.customer_id then lastTime = date;
    end;
if nbInt &amp;gt; 1 then do;
    totTime = lastTime - firstTime;
    avgTime = totTime / (nbInt - 1);
    output;
    end;
format totTime mmss8. avgTime time9.;
drop nbInt lastTime firstTime date;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 17 Aug 2018 02:51:16 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2018-08-17T02:51:16Z</dc:date>
    <item>
      <title>count average time interval</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-average-time-interval/m-p/487616#M127080</link>
      <description>&lt;P&gt;this might be tricky in sas,how to&amp;nbsp;count times for each average time interval&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="1.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/22510iB2A68C0C057DC10E/image-size/large?v=v2&amp;amp;px=999" role="button" title="1.JPG" alt="1.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;below is the case for mssql,and could create a colum interval_time for each customer then sum up.how to achive it in sas？data step or proc sql&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;    CREATE TABLE customer_data (customer_id BIGINT, date DATE, time time, answer VARCHAR(100), missed_call_type VARCHAR(100));
    
    INSERT INTO customer_data
    VALUES
    (101, '2018/8/3', '12:13:00', 'no', 'employee'),
    (102, '2018/8/3', '12:15:00', 'no', 'customer'),
    (103, '2018/8/3', '12:20:00', 'no', 'employee'),
    (102, '2018/8/3', '15:15:00', 'no', 'customer'),
    (101, '2018/8/3', '18:15:00', 'no', 'employee'),
    (105, '2018/8/3', '18:18:00', 'no', 'customer'),
    (102, '2018/8/3', '19:18:00', 'no', 'employee')
    
    select cd.customer_id, answer, missed_call_type,
    		CAST(CAST(cd.date as VARCHAR(10))+' ' +CAST(cd.time as VARCHAR(10)) as datetime) as date,
    		ROW_NUMBER() OVER(PARTITION BY cd.customer_id ORDER BY date desc, time desc) as ranks
    INTO #temP
    from customer_data cd
    order by cd.customer_Id, ranks;
    
    select AVG(DATEDIFF(MINUTE, x1.date, x2.date)) as avg_mins
    from #temP x1
    INNER JOIN #temP x2 ON x1.customer_id = x2.customer_id 
    WHERE x2.ranks = (x1.ranks-1)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;-------------------------------------------------------------------------update----------------------------------------------------------------------------------------&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;cst_id&lt;/TD&gt;&lt;TD&gt;minutes&lt;/TD&gt;&lt;TD&gt;time_interval(logic for calculate it)&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;101&lt;/TD&gt;&lt;TD&gt;362&lt;/TD&gt;&lt;TD&gt;18:15:00-12:13:00=6hours and 2mintes&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;102&lt;/TD&gt;&lt;TD&gt;423&lt;/TD&gt;&lt;TD&gt;[(15:15:00-12:15:00)+(19:18:00-15:15:00)]/2=7hours 3minutes&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;this is the procss,if there is anything unclear please tell.thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 17 Aug 2018 02:39:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-average-time-interval/m-p/487616#M127080</guid>
      <dc:creator>Geo-</dc:creator>
      <dc:date>2018-08-17T02:39:11Z</dc:date>
    </item>
    <item>
      <title>Re: count average time interval [how to improve your question]</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-average-time-interval/m-p/487620#M127081</link>
      <description>&lt;P&gt;Hello &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/184018"&gt;@Geo-&lt;/a&gt;,&lt;/P&gt;&lt;BR /&gt; &lt;P&gt;Your question requires more details before experts can help.&amp;nbsp;Can you revise your question to include more information?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Review this checklist:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Specify a meaningful subject line for your topic.&amp;nbsp; Avoid generic subjects like "need help," "SAS query," or "urgent."&lt;/LI&gt;
&lt;LI&gt;When appropriate, provide sample data in text or DATA step format.&amp;nbsp; See &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;this article for one method&lt;/A&gt;&amp;nbsp;you can use.&lt;/LI&gt;
&lt;LI&gt;If you're encountering an error in SAS, include the SAS log or a screenshot of the error condition.&amp;nbsp;Use the&amp;nbsp;&lt;STRONG&gt;Photos&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;button to include the image in your message.&lt;BR /&gt;&lt;SPAN class="lia-inline-image-display-wrapper lia-image-align-inline" style="width: 279px;"&gt;&lt;IMG src="https://kntur85557.i.lithium.com/t5/image/serverpage/image-id/16608i91A52F817EAC9A69/image-dimensions/279x150?v=1.0" width="279" height="150" alt="use_buttons.png" title="use_buttons.png" /&gt;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;It also helps to include an example (table or picture) of the result that you're trying to achieve.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;To edit your original message, select the "blue gear" icon at the top of the message and select&amp;nbsp;&lt;STRONG&gt;Edit Message&lt;/STRONG&gt;.&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;From there you can adjust the title and add more details to the body of the message.&amp;nbsp; Or, simply reply to this message with any additional information you can supply.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="lia-inline-image-display-wrapper lia-image-align-inline" style="width: 229px;"&gt;&lt;IMG src="https://kntur85557.i.lithium.com/t5/image/serverpage/image-id/16605iAC020BC79315B045/image-size/large?v=1.0&amp;amp;px=600" alt="edit_post.png" title="edit_post.png" /&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;SAS experts are eager to help -- help&amp;nbsp;&lt;EM&gt;them&lt;/EM&gt; by providing as much detail as you can.&lt;/P&gt; &lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-style:italic;font-size:smaller;"&gt;This prewritten response was triggered for you by fellow SAS Support Communities member &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&lt;/SPAN&gt;&lt;/P&gt;.</description>
      <pubDate>Fri, 17 Aug 2018 02:06:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-average-time-interval/m-p/487620#M127081</guid>
      <dc:creator>Community_Guide</dc:creator>
      <dc:date>2018-08-17T02:06:47Z</dc:date>
    </item>
    <item>
      <title>Re: count average time interval</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-average-time-interval/m-p/487623#M127082</link>
      <description>&lt;P&gt;Any calculation that relies on data ordering is simpler with a datastep in SAS:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data cd;
set customer_data;
date = dhms(date, 0, 0, time);
format date datetime17.;
drop time;
run;


proc sort data=cd; by customer_id date; run;

data avgTimes;
do nbInt = 1 by 1 until (last.customer_id);
    set cd; by customer_id;
    if first.customer_id then firstTime = date;
    if last.customer_id then lastTime = date;
    end;
if nbInt &amp;gt; 1 then do;
    totTime = lastTime - firstTime;
    avgTime = totTime / (nbInt - 1);
    output;
    end;
format totTime mmss8. avgTime time9.;
drop nbInt lastTime firstTime date;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Aug 2018 02:51:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-average-time-interval/m-p/487623#M127082</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-08-17T02:51:16Z</dc:date>
    </item>
    <item>
      <title>Re: count average time interval</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-average-time-interval/m-p/487628#M127084</link>
      <description>data step is a good way for its speed and functionality compared with sql.thanks you teacher！</description>
      <pubDate>Fri, 17 Aug 2018 03:39:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-average-time-interval/m-p/487628#M127084</guid>
      <dc:creator>Geo-</dc:creator>
      <dc:date>2018-08-17T03:39:11Z</dc:date>
    </item>
    <item>
      <title>Re: count average time interval</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-average-time-interval/m-p/487629#M127085</link>
      <description>&lt;P&gt;Come to think of it, this problem doesn't rely on ordering. It can also be solved simply with SQL:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cd;
set customer_data;
date = dhms(date, 0, 0, time);
format date datetime17.;
drop time;
run;

proc sql;
create table avgTimes as
select 
    customer_id,
    range(date) as totTime format=mmss8.,
    range(date) / (count(date)-1) as avgTime format=time9.
from cd
group by customer_id
having count(date) &amp;gt; 1;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Aug 2018 04:03:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-average-time-interval/m-p/487629#M127085</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-08-17T04:03:08Z</dc:date>
    </item>
  </channel>
</rss>

