<?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: keep first or last observation by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/keep-first-or-last-observation-by-group/m-p/622945#M183292</link>
    <description>&lt;P&gt;Thank you guys for your kindly assistance!&lt;/P&gt;</description>
    <pubDate>Fri, 07 Feb 2020 05:41:18 GMT</pubDate>
    <dc:creator>JacAder</dc:creator>
    <dc:date>2020-02-07T05:41:18Z</dc:date>
    <item>
      <title>keep first or last observation by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-first-or-last-observation-by-group/m-p/621583#M182733</link>
      <description>&lt;P&gt;I would like to keep the first or last observations for different dategroups:&lt;/P&gt;&lt;P&gt;*for each ID in each year-month, keep the FIRST observation if dategroup=BEG;&lt;/P&gt;&lt;P&gt;*for each ID in each year-month, keep the LAST observation if dategroup=END;&lt;/P&gt;&lt;P&gt;The idea is as following, how to make the code works? appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;output will be like:&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;ID&amp;nbsp; year&amp;nbsp; month&amp;nbsp; value&amp;nbsp; &amp;nbsp;dategroup&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;AA&amp;nbsp; 2001&amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; 11&amp;nbsp; &amp;nbsp; &amp;nbsp; BEG&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;AA&amp;nbsp; 2001&amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; 23&amp;nbsp; &amp;nbsp; &amp;nbsp; END&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;BB&amp;nbsp; 2003&amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; 31&amp;nbsp; &amp;nbsp; &amp;nbsp; BEG&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;CC&amp;nbsp; 2005&amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; 43&amp;nbsp; &amp;nbsp; &amp;nbsp; END&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID $3. year month value dategroup $3.;
datalines;
AA 2001 1 11 BEG
AA 2001 1 12 BEG
AA 2001 1 13 BEG
AA 2001 1 21 END
AA 2001 1 22 END
AA 2001 1 23 END
BB 2003 2 31 BEG
BB 2003 2 32 BEG
BB 2003 2 33 BEG
CC 2005 3 41 END
CC 2005 3 42 END
CC 2005 3 43 END
run;

data want;
set have;
*for each ID in each year-month, keep the FIRST observation if dategroup=BEG;
if dategroup="BEG" then do;
by ID year month;
if first.month then output;
end;
*for each ID in each year-month, keep the LAST observation if dategroup=END;
if dategroup="END" then do;
by ID year month;
if last.month then output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Feb 2020 00:33:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-first-or-last-observation-by-group/m-p/621583#M182733</guid>
      <dc:creator>JacAder</dc:creator>
      <dc:date>2020-02-01T00:33:49Z</dc:date>
    </item>
    <item>
      <title>Re: keep first or last observation by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-first-or-last-observation-by-group/m-p/621869#M182886</link>
      <description>&lt;P&gt;You need to apply by group processing on id and dategroup. Select the first.dategroup &amp;amp; dategroup='BEG' combination as well as the last.dategroup &amp;amp; dategroup='END' combination:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	by id dategroup;

	if (dategroup='BEG')*(first.dategroup) or (dategroup='END')*(last.dategroup);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 Feb 2020 05:24:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-first-or-last-observation-by-group/m-p/621869#M182886</guid>
      <dc:creator>unison</dc:creator>
      <dc:date>2020-02-03T05:24:40Z</dc:date>
    </item>
    <item>
      <title>Re: keep first or last observation by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-first-or-last-observation-by-group/m-p/621879#M182892</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/270457"&gt;@unison&lt;/a&gt;&amp;nbsp;Small Correction may be&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
by ID year month;
run;

data want;
set have;
by ID year month;
if (dategroup='BEG')*(first.month) or (dategroup='END')*(last.month);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 Feb 2020 07:44:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-first-or-last-observation-by-group/m-p/621879#M182892</guid>
      <dc:creator>Satish_Parida</dc:creator>
      <dc:date>2020-02-03T07:44:15Z</dc:date>
    </item>
    <item>
      <title>Re: keep first or last observation by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-first-or-last-observation-by-group/m-p/621916#M182911</link>
      <description>&lt;P&gt;Yes, to be safe. Looks like dategroup was flagged based on id, year and month.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Feb 2020 12:39:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-first-or-last-observation-by-group/m-p/621916#M182911</guid>
      <dc:creator>unison</dc:creator>
      <dc:date>2020-02-03T12:39:56Z</dc:date>
    </item>
    <item>
      <title>Re: keep first or last observation by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-first-or-last-observation-by-group/m-p/622945#M183292</link>
      <description>&lt;P&gt;Thank you guys for your kindly assistance!&lt;/P&gt;</description>
      <pubDate>Fri, 07 Feb 2020 05:41:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-first-or-last-observation-by-group/m-p/622945#M183292</guid>
      <dc:creator>JacAder</dc:creator>
      <dc:date>2020-02-07T05:41:18Z</dc:date>
    </item>
  </channel>
</rss>

