<?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: time period determination using PROC SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/time-period-determination-using-PROC-SQL/m-p/681989#M206386</link>
    <description>Briliant solution in easy way. Values datetimeTo are different than in my example but it's fine for me. Thank you very much &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;!</description>
    <pubDate>Mon, 07 Sep 2020 10:12:38 GMT</pubDate>
    <dc:creator>Lucas</dc:creator>
    <dc:date>2020-09-07T10:12:38Z</dc:date>
    <item>
      <title>time period determination using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/time-period-determination-using-PROC-SQL/m-p/681873#M206325</link>
      <description>&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have table with 3 columns (id;datetime;status) and I need to create new table with periods between two statuses: CLOSE-REOPEN.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Have:&lt;/P&gt;
&lt;TABLE width="257"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="40px" height="30px"&gt;id&lt;/TD&gt;
&lt;TD width="164px" height="30px"&gt;datetime&lt;/TD&gt;
&lt;TD width="79px" height="30px"&gt;status&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="164px" height="30px"&gt;06SEP2020:12:00:00&lt;/TD&gt;
&lt;TD width="79px" height="30px"&gt;open&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="164px" height="30px"&gt;06SEP2020:12:00:01&lt;/TD&gt;
&lt;TD width="79px" height="30px"&gt;CLOSE&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="164px" height="30px"&gt;06SEP2020:12:00:02&lt;/TD&gt;
&lt;TD width="79px" height="30px"&gt;s1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="164px" height="30px"&gt;06SEP2020:12:00:03&lt;/TD&gt;
&lt;TD width="79px" height="30px"&gt;s2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="164px" height="30px"&gt;06SEP2020:12:00:04&lt;/TD&gt;
&lt;TD width="79px" height="30px"&gt;REOPEN&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="164px" height="30px"&gt;06SEP2020:12:00:05&lt;/TD&gt;
&lt;TD width="79px" height="30px"&gt;s1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="164px" height="30px"&gt;06SEP2020:12:00:06&lt;/TD&gt;
&lt;TD width="79px" height="30px"&gt;CLOSE&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="164px" height="30px"&gt;06SEP2020:12:00:07&lt;/TD&gt;
&lt;TD width="79px" height="30px"&gt;s1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="164px" height="30px"&gt;06SEP2020:12:00:08&lt;/TD&gt;
&lt;TD width="79px" height="30px"&gt;REOPEN&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="164px" height="30px"&gt;06SEP2020:12:00:00&lt;/TD&gt;
&lt;TD width="79px" height="30px"&gt;open&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="164px" height="30px"&gt;06SEP2020:12:00:01&lt;/TD&gt;
&lt;TD width="79px" height="30px"&gt;CLOSE&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="164px" height="30px"&gt;06SEP2020:12:00:02&lt;/TD&gt;
&lt;TD width="79px" height="30px"&gt;s1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Want:&lt;/P&gt;
&lt;TABLE width="515"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;id&lt;/TD&gt;
&lt;TD width="129"&gt;status1&lt;/TD&gt;
&lt;TD width="64"&gt;status2&lt;/TD&gt;
&lt;TD width="129"&gt;datetime_from&lt;/TD&gt;
&lt;TD width="129"&gt;datetime_to&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;CLOSE&lt;/TD&gt;
&lt;TD&gt;REOPEN&lt;/TD&gt;
&lt;TD&gt;06SEP2020:12:00:01&lt;/TD&gt;
&lt;TD&gt;06SEP2020:12:00:03&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;CLOSE&lt;/TD&gt;
&lt;TD&gt;REOPEN&lt;/TD&gt;
&lt;TD&gt;06SEP2020:12:00:06&lt;/TD&gt;
&lt;TD&gt;06SEP2020:12:00:07&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is it possible to achieve this with SQL Query? I can use only PROC SQL. It would be great to do this with one query but if it is not possible then more queries are also fine.&lt;/P&gt;</description>
      <pubDate>Sun, 06 Sep 2020 13:05:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/time-period-determination-using-PROC-SQL/m-p/681873#M206325</guid>
      <dc:creator>Lucas</dc:creator>
      <dc:date>2020-09-06T13:05:12Z</dc:date>
    </item>
    <item>
      <title>Re: time period determination using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/time-period-determination-using-PROC-SQL/m-p/681877#M206328</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19776"&gt;@Lucas&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I can use only PROC SQL.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In my opinion, an unfortunate restriction. SQL doesn't really a good tool for finding the values immediately above a specific record. I'm sure someone will come along and write wizard-like SQL code that will be difficult to understand, that will get the job done.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would use a DATA step, followed by PROC TRANSPOSE. So for anyone reading along who might not have this restriction, here is the code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data times;
    set have;
    prev_time=lag(datetime);
    if status='CLOSE' then do;
    	sequence+1;
    	output_time=datetime;
    	output;
	end;
	else if status='REOPEN' then do;
	    output_time=prev_time;
	    output;
    end;
    drop prev_time datetime;
run;
proc transpose data=times out=want prefix=datetime_;
    var output_time;
    by id sequence;
    id status;
run;
data want;
    set want(drop=sequence _name_);
    if missing(datetime_reopen) then delete;
    format datetime: datetime16.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 06 Sep 2020 13:40:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/time-period-determination-using-PROC-SQL/m-p/681877#M206328</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-09-06T13:40:55Z</dc:date>
    </item>
    <item>
      <title>Re: time period determination using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/time-period-determination-using-PROC-SQL/m-p/681888#M206334</link>
      <description>&lt;P&gt;Sure. It can be achieved with a SQL query:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select 
    a.id,
    a.status,
    b.status,
    a.datetime as datetimeFrom,
    b.datetime as datetimeTo
from 
    have as a inner join
    have as b on a.id=b.id and b.datetime&amp;gt;a.datetime
where a.status = "CLOSE" and b.status="REOPEN"
group by a.id, a.datetime
having b.datetime-a.datetime = min(b.datetime-a.datetime);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 06 Sep 2020 16:51:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/time-period-determination-using-PROC-SQL/m-p/681888#M206334</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-09-06T16:51:14Z</dc:date>
    </item>
    <item>
      <title>Re: time period determination using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/time-period-determination-using-PROC-SQL/m-p/681891#M206337</link>
      <description>&lt;P&gt;This doesn't give the requested output in the column named datetimeTo&lt;/P&gt;</description>
      <pubDate>Sun, 06 Sep 2020 17:11:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/time-period-determination-using-PROC-SQL/m-p/681891#M206337</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-09-06T17:11:31Z</dc:date>
    </item>
    <item>
      <title>Re: time period determination using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/time-period-determination-using-PROC-SQL/m-p/681895#M206339</link>
      <description>&lt;P&gt;Oops. You are right &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But then, what should be the value of datetimeTo? The most recent recorded timestamp before REOPEN (whatever the status) or the REOPEN timestamp minus 1 second? What happens if CLOSE and REOPEN are consecutive?&lt;/P&gt;</description>
      <pubDate>Sun, 06 Sep 2020 18:03:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/time-period-determination-using-PROC-SQL/m-p/681895#M206339</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-09-06T18:03:47Z</dc:date>
    </item>
    <item>
      <title>Re: time period determination using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/time-period-determination-using-PROC-SQL/m-p/681897#M206341</link>
      <description>&lt;P&gt;Those are questions only &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19776"&gt;@Lucas&lt;/a&gt;&amp;nbsp;can answer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This illustrates the importance of coming up with a&amp;nbsp;&lt;EM&gt;realistic&amp;nbsp;&lt;/EM&gt;example to illustrate the problem.&lt;/P&gt;</description>
      <pubDate>Sun, 06 Sep 2020 18:34:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/time-period-determination-using-PROC-SQL/m-p/681897#M206341</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-09-06T18:34:51Z</dc:date>
    </item>
    <item>
      <title>Re: time period determination using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/time-period-determination-using-PROC-SQL/m-p/681909#M206348</link>
      <description>&lt;P&gt;Yes, it can be done with SQL, but AFAICT SQL has to do a cartesian crossing of all CLOSE vs REOPEN rows to determine the reopen most closely following&amp;nbsp;close.&amp;nbsp; It doesn't appear to take advantage of the fact that the data are ordered - ready made for a DATA step, which should be much faster.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The user is being asked to show proficiency in SQL rather than efficiency (and relative simplicity) in data processing.&lt;/P&gt;</description>
      <pubDate>Sun, 06 Sep 2020 21:07:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/time-period-determination-using-PROC-SQL/m-p/681909#M206348</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-09-06T21:07:09Z</dc:date>
    </item>
    <item>
      <title>Re: time period determination using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/time-period-determination-using-PROC-SQL/m-p/681915#M206352</link>
      <description>&lt;P&gt;If this is an assignment to help someone become proficient at SQL, then I hate the idea behind this assignment. Part of becoming proficient at SQL is understanding when SQL is not a good tool to use.&lt;/P&gt;</description>
      <pubDate>Sun, 06 Sep 2020 21:58:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/time-period-determination-using-PROC-SQL/m-p/681915#M206352</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-09-06T21:58:14Z</dc:date>
    </item>
    <item>
      <title>Re: time period determination using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/time-period-determination-using-PROC-SQL/m-p/681989#M206386</link>
      <description>Briliant solution in easy way. Values datetimeTo are different than in my example but it's fine for me. Thank you very much &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;!</description>
      <pubDate>Mon, 07 Sep 2020 10:12:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/time-period-determination-using-PROC-SQL/m-p/681989#M206386</guid>
      <dc:creator>Lucas</dc:creator>
      <dc:date>2020-09-07T10:12:38Z</dc:date>
    </item>
  </channel>
</rss>

