<?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 How to save first observation from first. last. in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-save-first-observation-from-first-last/m-p/602077#M18362</link>
    <description>&lt;P&gt;I am trying to pull in longitudinal data, and keep those with greater than 3 observations. I am using a first. last. statement to keep IDs and create an observation counter to ensure that I am keeping only those with greater than 3 observations. When I do this, the last observation is kept, and when I try to merge this back into the data set, the first observation is overwritten by the last observation being pulled from the first. last. How do I keep the first observation or at least avoid overwriting? Here is the code I am using:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA UniqueID;&lt;BR /&gt;SET Dementia;&lt;BR /&gt;BY ID Age;&lt;BR /&gt;IF First.ID = 1 THEN OBSCNT = 0;&lt;BR /&gt;OBSCNT + 1;&lt;BR /&gt;IF Last.ID = 1 AND OBSCNT GE 3;&lt;BR /&gt;RUN;&lt;/P&gt;</description>
    <pubDate>Wed, 06 Nov 2019 17:50:08 GMT</pubDate>
    <dc:creator>marin1hk</dc:creator>
    <dc:date>2019-11-06T17:50:08Z</dc:date>
    <item>
      <title>How to save first observation from first. last.</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-save-first-observation-from-first-last/m-p/602077#M18362</link>
      <description>&lt;P&gt;I am trying to pull in longitudinal data, and keep those with greater than 3 observations. I am using a first. last. statement to keep IDs and create an observation counter to ensure that I am keeping only those with greater than 3 observations. When I do this, the last observation is kept, and when I try to merge this back into the data set, the first observation is overwritten by the last observation being pulled from the first. last. How do I keep the first observation or at least avoid overwriting? Here is the code I am using:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA UniqueID;&lt;BR /&gt;SET Dementia;&lt;BR /&gt;BY ID Age;&lt;BR /&gt;IF First.ID = 1 THEN OBSCNT = 0;&lt;BR /&gt;OBSCNT + 1;&lt;BR /&gt;IF Last.ID = 1 AND OBSCNT GE 3;&lt;BR /&gt;RUN;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2019 17:50:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-save-first-observation-from-first-last/m-p/602077#M18362</guid>
      <dc:creator>marin1hk</dc:creator>
      <dc:date>2019-11-06T17:50:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to save first observation from first. last.</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-save-first-observation-from-first-last/m-p/602097#M18364</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/270915"&gt;@marin1hk&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For this type of task ("select all observations from BY groups satisfying a condition which can be checked only at the end of the BY group [edit: or at least not at the beginning of the group]") many people use either PROC SQL or a double &lt;A href="http://www2.sas.com/proceedings/sugi28/099-28.pdf" target="_blank" rel="noopener"&gt;DOW loop&lt;/A&gt;. The latter comes closer to your existing approach, but requires only one DATA step (i.e. no intermediate dataset UniqueID):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=obscnt);
do until(last.id);
  set dementia;
  by id;
  obscnt=sum(obscnt,1);
end;
do until(last.id);
  set dementia;
  by id;
  if obscnt ge 3 then output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: Here's what the PROC SQL solution might look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select *
from dementia
group by id
having count(*) ge 3
order by id, age;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Unlike the DOW loop, PROC SQL doesn't require the input dataset to be sorted or indexed by the grouping variable.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2019 19:05:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-save-first-observation-from-first-last/m-p/602097#M18364</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-11-06T19:05:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to save first observation from first. last.</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-save-first-observation-from-first-last/m-p/602292#M18366</link>
      <description>&lt;P&gt;You have data sorted by ID, and you want all records for any ID with 3 or more records.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Although it is a common technique, you don't really have to count records for an ID.&amp;nbsp; The counting strategy means you have to read every ID group twice.&amp;nbsp; The first time to generate a count, and the second time to re-read the id group and output records for ID's with counts of 3 or more.&amp;nbsp; That's one of the solutions offered by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming you don't actually need to output a counter variable, you can just merge a record with the id of the second-following record.&amp;nbsp; Whenever the record-in-hand is the beginning of an ID, and the 2nd trailing record has a matching ID, you know you are at the start of an ID group that meets you condition.&amp;nbsp; In that case, just set variable _KEEPFLAG to "Y", (and let _KEEPFLAG retain its value throughout the ID group), and output only those records with _KEEPFLAG='Y'.&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 (drop=_:);
  set have (keep=id);
  by id;
  merge have
        have (firstobs=3 keep=id rename=(id=_nxt_id2));

  retain _keepflag;
  if first.id=1 then do;
    if _nxt_id2=id then _keepflag='Y';
    else _keepflag='N';
  end;
  if _keepflag='Y';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, you don't really need the extra "set have; by id;" statements.&amp;nbsp; You can also detect the start of an ID group using a test on LAG(ID) vs current ID, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  merge have
        have (firstobs=3 keep=id rename=(id=_nxt_id2));

  retain _keepflag;
  if id ^= lag(id) then do;
    if _nxt_id2=id then _keepflag='Y';
    else _keepflag='N';
  end;
  if _keepflag='Y';
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Nov 2019 22:14:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-save-first-observation-from-first-last/m-p/602292#M18366</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-11-07T22:14:34Z</dc:date>
    </item>
  </channel>
</rss>

