<?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 keep first observation to last observation including missings in between? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-keep-first-observation-to-last-observation-including/m-p/274298#M54711</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id x;
cards;
1 . 
1 .
1 .
1 3
1 .
1 4
1 .
1 2
1 .
1 .
1 3
1 .
1 .
1 .
1 .
1 .
2 5
2 .
2 3
2 .
2 .
2 3
2 .
2 .
2 2
2 4
2 .
2 5
2 .
2 .
2 .
2 .
;
run;
data want;
 do until(last.x);
  set have;
  by id x notsorted;
  if first.id then first=1;
  if last.id then last=1;
 end;
 
  do until(last.x);
  set have;
  by id x notsorted;
  if not (missing(x) and first) and 
     not (missing(x) and last) then output;
 end;
 keep id x;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 01 Jun 2016 08:57:55 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2016-06-01T08:57:55Z</dc:date>
    <item>
      <title>how to keep first observation to last observation including missings in between?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-keep-first-observation-to-last-observation-including/m-p/274276#M54705</link>
      <description>&lt;P&gt;Data sample&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;ID X (variable)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;1 .&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 .&lt;/P&gt;&lt;P&gt;1 .&lt;/P&gt;&lt;P&gt;1 3&lt;/P&gt;&lt;P&gt;1 .&lt;/P&gt;&lt;P&gt;1 4&lt;/P&gt;&lt;P&gt;1 .&lt;/P&gt;&lt;P&gt;1 2&lt;/P&gt;&lt;P&gt;1 .&lt;/P&gt;&lt;P&gt;1 .&lt;/P&gt;&lt;P&gt;1 3&lt;/P&gt;&lt;P&gt;1 .&lt;/P&gt;&lt;P&gt;1 .&lt;/P&gt;&lt;P&gt;1 .&lt;/P&gt;&lt;P&gt;1 .&lt;/P&gt;&lt;P&gt;1 .&lt;/P&gt;&lt;P&gt;2 5&lt;/P&gt;&lt;P&gt;2 .&lt;/P&gt;&lt;P&gt;2 3&lt;/P&gt;&lt;P&gt;2 .&lt;/P&gt;&lt;P&gt;2 .&lt;/P&gt;&lt;P&gt;2 3&lt;/P&gt;&lt;P&gt;2 .&lt;/P&gt;&lt;P&gt;2 .&lt;/P&gt;&lt;P&gt;2 2&lt;/P&gt;&lt;P&gt;2 4&lt;/P&gt;&lt;P&gt;2 .&lt;/P&gt;&lt;P&gt;2 5&lt;/P&gt;&lt;P&gt;2 .&lt;/P&gt;&lt;P&gt;2 .&lt;/P&gt;&lt;P&gt;2 .&lt;/P&gt;&lt;P&gt;2 .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to save obervations from row4-11 and row17-28... and save it as a different dataset.&lt;/P&gt;&lt;P&gt;Basically.... for ID=1, I want to keep all the observations between the first non-missing obs and the last non-missing obs, and for ID=2 do the same...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance for your help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Jun 2016 04:38:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-keep-first-observation-to-last-observation-including/m-p/274276#M54705</guid>
      <dc:creator>imsenny</dc:creator>
      <dc:date>2016-06-01T04:38:57Z</dc:date>
    </item>
    <item>
      <title>Re: how to keep first observation to last observation including missings in between?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-keep-first-observation-to-last-observation-including/m-p/274279#M54706</link>
      <description>Perhaps not the most efficient way:&lt;BR /&gt;First pass - assign row no to a variable (_n_) and delete all Obs with missing until the firs non missing. Use first. logic and a flag to keep track if you encountered the first non missing within the by group.&lt;BR /&gt;Second - resort the data set descending based on the previously created row no variable.&lt;BR /&gt;Third - repeat the logic from the first pass. Remember to use descending on the BY statement.</description>
      <pubDate>Wed, 01 Jun 2016 05:33:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-keep-first-observation-to-last-observation-including/m-p/274279#M54706</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-06-01T05:33:26Z</dc:date>
    </item>
    <item>
      <title>Re: how to keep first observation to last observation including missings in between?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-keep-first-observation-to-last-observation-including/m-p/274281#M54707</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id x;
cards;
1 . 
1 .
1 .
1 3
1 .
1 4
1 .
1 2
1 .
1 .
1 3
1 .
1 .
1 .
1 .
1 .
2 5
2 .
2 3
2 .
2 .
2 3
2 .
2 .
2 2
2 4
2 .
2 5
2 .
2 .
2 .
2 .
;
run;

data int1;
set have;
by id;
retain flag1;
if first.id then flag1 = 0;
if x ne . then flag1 = 1;
obsno = _n_;
run;

proc sort data=int1;
by descending obsno;
run;

data int2;
set int1;
by id notsorted;
retain flag2;
if first.id then flag2 = 0;
if x ne . then flag2 = 1;
run;

proc sort
  data=int2 (where=(flag1 = 1 and flag2 = 1))
  out=want (drop=flag1 flag2 obsno)
;
by obsno;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to omit the non-missing value rows that start and end your sequence, you need to add another step that eliminates the first. and last. rows for each id.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Jun 2016 05:39:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-keep-first-observation-to-last-observation-including/m-p/274281#M54707</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-06-01T05:39:33Z</dc:date>
    </item>
    <item>
      <title>Re: how to keep first observation to last observation including missings in between?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-keep-first-observation-to-last-observation-including/m-p/274282#M54708</link>
      <description>&lt;P&gt;Heh.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13674"&gt;@LinusH﻿&lt;/a&gt; supplied the logic, I the code &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Jun 2016 05:43:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-keep-first-observation-to-last-observation-including/m-p/274282#M54708</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-06-01T05:43:02Z</dc:date>
    </item>
    <item>
      <title>Re: how to keep first observation to last observation including missings in between?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-keep-first-observation-to-last-observation-including/m-p/274298#M54711</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id x;
cards;
1 . 
1 .
1 .
1 3
1 .
1 4
1 .
1 2
1 .
1 .
1 3
1 .
1 .
1 .
1 .
1 .
2 5
2 .
2 3
2 .
2 .
2 3
2 .
2 .
2 2
2 4
2 .
2 5
2 .
2 .
2 .
2 .
;
run;
data want;
 do until(last.x);
  set have;
  by id x notsorted;
  if first.id then first=1;
  if last.id then last=1;
 end;
 
  do until(last.x);
  set have;
  by id x notsorted;
  if not (missing(x) and first) and 
     not (missing(x) and last) then output;
 end;
 keep id x;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Jun 2016 08:57:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-keep-first-observation-to-last-observation-including/m-p/274298#M54711</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-06-01T08:57:55Z</dc:date>
    </item>
    <item>
      <title>Re: how to keep first observation to last observation including missings in between?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-keep-first-observation-to-last-observation-including/m-p/274310#M54714</link>
      <description>&lt;P&gt;Another DOW loop solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do _n_=1 by 1 until(last.id);
  set have;
  by id;
  if x&amp;gt;. then do;
    if start=. then start=_n_;
    finish=_n_;
  end;
end;
do _n_=1 by 1 until(last.id);
  set have;
  by id;
  if start&amp;lt;=_n_&amp;lt;=finish then output;
end;
drop start finish;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp﻿&lt;/a&gt;: Why do you need _x?&lt;/P&gt;</description>
      <pubDate>Wed, 01 Jun 2016 08:10:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-keep-first-observation-to-last-observation-including/m-p/274310#M54714</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-06-01T08:10:11Z</dc:date>
    </item>
    <item>
      <title>Re: how to keep first observation to last observation including missings in between?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-keep-first-observation-to-last-observation-including/m-p/274313#M54715</link>
      <description>&lt;P&gt;Yeah. No need _x ,just&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token operator"&gt;not&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;missing&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;x&lt;SPAN class="token punctuation"&gt;)........&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I just follow what I am thinking.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Jun 2016 08:55:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-keep-first-observation-to-last-observation-including/m-p/274313#M54715</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-06-01T08:55:24Z</dc:date>
    </item>
  </channel>
</rss>

