<?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: Creating intervals from adjacent observations in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Creating-intervals-from-adjacent-observations/m-p/322875#M9304</link>
    <description>&lt;P&gt;another approach&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2;
num=_n_+1;
set test ;
if nobs&amp;gt;=num then set test(keep=id x rename=(id=id2 x=xstop)) nobs=nobs point=num;
if id^=id2 then call missing(xstop);
drop id2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 06 Jan 2017 01:43:04 GMT</pubDate>
    <dc:creator>Jagadishkatam</dc:creator>
    <dc:date>2017-01-06T01:43:04Z</dc:date>
    <item>
      <title>Creating intervals from adjacent observations</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Creating-intervals-from-adjacent-observations/m-p/322758#M9288</link>
      <description>&lt;P&gt;Hi, I'm looking for an efficient way to transform data describing a step function from a format where observations give the locations of "jumps" to a format that has intervals where the step function is constant. &amp;nbsp;I want to define step functions to start at 0 with a value of 0 (unless a different value is specified in the input dataset). &amp;nbsp;I usually have success searching SAS forums using Google or the like, but unfortunately not this time!&amp;nbsp; I'm using SAS V9.4. &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example,&amp;nbsp;let's say I have data like the following:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  input id x y;
  cards;
  1 0 20
  1 5 15
  1 10 17
  2 1 5
  2 10 2
  3 7 8
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here &lt;EM&gt;id&lt;/EM&gt; is identifies a particular step function (there could be more than one defined in a particular dataset), &lt;EM&gt;x&lt;/EM&gt; is the location of a jump and&amp;nbsp;&lt;EM&gt;y&lt;/EM&gt; is the value the step function takes on at that jump (technically, I guess I'm imagining that this function is right-continuous). &amp;nbsp;So, function 1 starts at 0 with a value of 20, is constant until x=5 when it jumps to a value of 15, stays constant until x=10 when it assumes a value of 17 and is constant thereafter.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd like to end up with a dataset like:&lt;/P&gt;&lt;PRE&gt;data test2;
  input id xstart xstop y;
  cards;
  1 0 5 20
  1 5 10 15
  1 10 . 17
  2 0 1 0
  2 1 10 5
  2 10 . 2
  3 0 7 0
  3 7 . 8
;
run;&lt;/PRE&gt;&lt;P&gt;Where&amp;nbsp;&lt;EM&gt;id&lt;/EM&gt; is the same id as in the previous dataset, but xstart to xstop defines intervals where the step function is constant and &lt;EM&gt;y&lt;/EM&gt; is the value of the function during a particular interval.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The solutions I've tried become very complex and I'd be happy for any pointers towards efficient solutions using either a data step or PROC SQL (or both).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jan 2017 18:37:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Creating-intervals-from-adjacent-observations/m-p/322758#M9288</guid>
      <dc:creator>ruser</dc:creator>
      <dc:date>2017-01-05T18:37:51Z</dc:date>
    </item>
    <item>
      <title>Re: Creating intervals from adjacent observations</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Creating-intervals-from-adjacent-observations/m-p/322776#M9290</link>
      <description>&lt;P&gt;Here's a DATA step approach.&amp;nbsp; It might be complex, but at least it's short:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set test (rename=(x=xstart));&lt;/P&gt;
&lt;P&gt;by id;&lt;/P&gt;
&lt;P&gt;set test (firstobs=2 keep=x rename=(x=xstop)) test (drop=_all_);&lt;/P&gt;
&lt;P&gt;if last.id then xstop=.;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It does the heavy lifting (getting XSTART and XSTOP), but not all of the work.&amp;nbsp; It doesn't insert the 0 steps.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jan 2017 20:07:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Creating-intervals-from-adjacent-observations/m-p/322776#M9290</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-01-05T20:07:00Z</dc:date>
    </item>
    <item>
      <title>Re: Creating intervals from adjacent observations</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Creating-intervals-from-adjacent-observations/m-p/322864#M9303</link>
      <description>This is great! My solutions were much longer (and didn't work exactly as I wanted). Thanks.</description>
      <pubDate>Fri, 06 Jan 2017 00:46:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Creating-intervals-from-adjacent-observations/m-p/322864#M9303</guid>
      <dc:creator>ruser</dc:creator>
      <dc:date>2017-01-06T00:46:24Z</dc:date>
    </item>
    <item>
      <title>Re: Creating intervals from adjacent observations</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Creating-intervals-from-adjacent-observations/m-p/322875#M9304</link>
      <description>&lt;P&gt;another approach&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2;
num=_n_+1;
set test ;
if nobs&amp;gt;=num then set test(keep=id x rename=(id=id2 x=xstop)) nobs=nobs point=num;
if id^=id2 then call missing(xstop);
drop id2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Jan 2017 01:43:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Creating-intervals-from-adjacent-observations/m-p/322875#M9304</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2017-01-06T01:43:04Z</dc:date>
    </item>
  </channel>
</rss>

