<?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 take the first observation in a grouping in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-take-the-first-observation-in-a-grouping/m-p/938619#M368664</link>
    <description>&lt;P&gt;If you use the NOTSORTED option (to accomodate "CODE" value that might either ascend or descend, then&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID SEQ Code $ date :ddmmyy10.;
format date ddmmyy10.;
datalines;
001 1 ST 05/04/2014
001 2 ST 05/01/2014
001 3 ST 04/01/2014
001 4 GP 02/05/2014
001 5 ST 02/01/2014
002 1 ST 05/01/2014
002 2 ST 03/02/2014
002 3 GP 02/01/2014
002 4 GP 02/01/2014
run;

data want (drop=_:) ;
  set have;
  by id code notsorted;
  if first.id=1 then _code_group=0;
  _code_group + (last.code=1);
  if last.code and _code_group=1;
  
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 08 Aug 2024 03:08:57 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2024-08-08T03:08:57Z</dc:date>
    <item>
      <title>How to take the first observation in a grouping</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-take-the-first-observation-in-a-grouping/m-p/938513#M368631</link>
      <description>&lt;P&gt;Hello experts,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would greatly appreciate your help with this issue I am having. I need to keep the first occurrence of code change that happens in my dataset.&amp;nbsp; I have created a sample of the data I am working with below. For ID 001 we want the SEQ 3 for it. This is the first time they were changed to their current code which you can see they are still on with their recent "review" from observation 1 and date of 05/04/2014. For 002 we would need to keep SEQ 2 for them.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried to sort and duplicate out the observation not in the first group of 'ST' but the issue I have is if in their history they were on that code before the older dates get mixed in and those are two separate occurrences. I have no data point to separate the occurrence other than a different status happening in between, such as we see with 'GP' on observation 4. I also tried to count the number of ST's in the group and stop when it reaches GP but I had no such look due to needing a by statement and the sort has the issues mentioned above.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In short, I need a piece of code that can identify the first occurrence of their most recent Code, here it is ST but I also have SP and DD. For ID 001 observation line 3 is the one needed to keep and for 002 observation line 7 is the one to keep.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for any help I can receive.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;P.s. Do note the format of the date is ddmmyy.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;input ID SEQ Code $ date :ddmmyy10.;&lt;BR /&gt;format date ddmmyy10.;&lt;BR /&gt;datalines;&lt;BR /&gt;001 1 ST 05/04/2014&lt;BR /&gt;001 2 ST 05/01/2014&lt;BR /&gt;001 3 ST 04/01/2014&lt;BR /&gt;001 4 GP 02/05/2014&lt;BR /&gt;001 5 ST 02/01/2014&lt;BR /&gt;002 1 ST 05/01/2014&lt;BR /&gt;002 2 ST 03/02/2014&lt;BR /&gt;002 3 GP 02/01/2014&lt;BR /&gt;002 4 GP 02/01/2014&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Aug 2024 13:32:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-take-the-first-observation-in-a-grouping/m-p/938513#M368631</guid>
      <dc:creator>Jamerkin</dc:creator>
      <dc:date>2024-08-07T13:32:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to take the first observation in a grouping</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-take-the-first-observation-in-a-grouping/m-p/938515#M368632</link>
      <description>&lt;P&gt;I don't see your mentioned sample data?&lt;/P&gt;</description>
      <pubDate>Wed, 07 Aug 2024 13:26:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-take-the-first-observation-in-a-grouping/m-p/938515#M368632</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2024-08-07T13:26:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to take the first observation in a grouping</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-take-the-first-observation-in-a-grouping/m-p/938520#M368633</link>
      <description>&lt;P&gt;Apologies. The insert code button was not working. I updated my post by just pasting my sample data at the bottom.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Aug 2024 13:32:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-take-the-first-observation-in-a-grouping/m-p/938520#M368633</guid>
      <dc:creator>Jamerkin</dc:creator>
      <dc:date>2024-08-07T13:32:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to take the first observation in a grouping</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-take-the-first-observation-in-a-grouping/m-p/938521#M368634</link>
      <description>&lt;P&gt;So your desired result here is a data set with 2 observations, correct?&lt;/P&gt;</description>
      <pubDate>Wed, 07 Aug 2024 13:40:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-take-the-first-observation-in-a-grouping/m-p/938521#M368634</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2024-08-07T13:40:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to take the first observation in a grouping</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-take-the-first-observation-in-a-grouping/m-p/938522#M368635</link>
      <description>&lt;P&gt;Correct I should end with observation 3 and 7. Mind you the full dataset is over 500,000 individuals so doing this manually was not an option.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 07 Aug 2024 13:45:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-take-the-first-observation-in-a-grouping/m-p/938522#M368635</guid>
      <dc:creator>Jamerkin</dc:creator>
      <dc:date>2024-08-07T13:45:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to take the first observation in a grouping</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-take-the-first-observation-in-a-grouping/m-p/938619#M368664</link>
      <description>&lt;P&gt;If you use the NOTSORTED option (to accomodate "CODE" value that might either ascend or descend, then&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID SEQ Code $ date :ddmmyy10.;
format date ddmmyy10.;
datalines;
001 1 ST 05/04/2014
001 2 ST 05/01/2014
001 3 ST 04/01/2014
001 4 GP 02/05/2014
001 5 ST 02/01/2014
002 1 ST 05/01/2014
002 2 ST 03/02/2014
002 3 GP 02/01/2014
002 4 GP 02/01/2014
run;

data want (drop=_:) ;
  set have;
  by id code notsorted;
  if first.id=1 then _code_group=0;
  _code_group + (last.code=1);
  if last.code and _code_group=1;
  
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Aug 2024 03:08:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-take-the-first-observation-in-a-grouping/m-p/938619#M368664</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-08-08T03:08:57Z</dc:date>
    </item>
  </channel>
</rss>

