<?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 Good method for selecting observations based on min or max date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588584#M168228</link>
    <description>&lt;P&gt;Hello SAS community,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have come across this scenario a few times and don't feel I have found or created a good solution. Lets say we have data as such:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines;
		input personID $ attribute1 $ qty  date yymmdd10.;
		format date yymmdd10.;
datalines;
A m1 10 2019-03-01
A m1 20 2019-03-01
A m1 20 2019-02-01
A m2 20 2019-01-01
A m3 20 2019-02-01
B m3 10 2019-02-15
B m3 20 2019-02-15
C m2 20 2019-01-15
D m1 20 2019-01-15
D m1 20 2019-02-15
;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;We can have multiple observations per person per attribute. We want to return one observation per person per attribute with the earliest date, min(date). In the event there are multiple observations for the same person and attribute then select for the largest qty, max(qty).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Desired output:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;A m1 20 2019-02-01
A m2 20 2019-01-01
A m3 20 2019-02-01
B m3 20 2019-02-15
C m2 20 2019-01-15
D m1 20 2019-01-15
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For starters I tried this to return one row per person per attribute with the earliest date, but it doesn't work. As expected I get the note:&amp;nbsp;NOTE: The query requires remerging summary statistics back with the original data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	/** return one row per person and attribute **/
	create table want1 as
		select *
		from work.have
		group by personID, attribute1
		having min(date);
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is there a simple solution for this that I am not seeing?&amp;nbsp;Is SQL the best approach? Or is there a better approach using a data step?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;edit: fixed first obs in desired output&lt;/EM&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 16 Sep 2019 12:24:27 GMT</pubDate>
    <dc:creator>supp</dc:creator>
    <dc:date>2019-09-16T12:24:27Z</dc:date>
    <item>
      <title>Good method for selecting observations based on min or max date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588584#M168228</link>
      <description>&lt;P&gt;Hello SAS community,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have come across this scenario a few times and don't feel I have found or created a good solution. Lets say we have data as such:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines;
		input personID $ attribute1 $ qty  date yymmdd10.;
		format date yymmdd10.;
datalines;
A m1 10 2019-03-01
A m1 20 2019-03-01
A m1 20 2019-02-01
A m2 20 2019-01-01
A m3 20 2019-02-01
B m3 10 2019-02-15
B m3 20 2019-02-15
C m2 20 2019-01-15
D m1 20 2019-01-15
D m1 20 2019-02-15
;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;We can have multiple observations per person per attribute. We want to return one observation per person per attribute with the earliest date, min(date). In the event there are multiple observations for the same person and attribute then select for the largest qty, max(qty).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Desired output:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;A m1 20 2019-02-01
A m2 20 2019-01-01
A m3 20 2019-02-01
B m3 20 2019-02-15
C m2 20 2019-01-15
D m1 20 2019-01-15
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For starters I tried this to return one row per person per attribute with the earliest date, but it doesn't work. As expected I get the note:&amp;nbsp;NOTE: The query requires remerging summary statistics back with the original data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	/** return one row per person and attribute **/
	create table want1 as
		select *
		from work.have
		group by personID, attribute1
		having min(date);
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is there a simple solution for this that I am not seeing?&amp;nbsp;Is SQL the best approach? Or is there a better approach using a data step?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;edit: fixed first obs in desired output&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2019 12:24:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588584#M168228</guid>
      <dc:creator>supp</dc:creator>
      <dc:date>2019-09-16T12:24:27Z</dc:date>
    </item>
    <item>
      <title>Re: Good method for selecting observations based on min or max date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588586#M168230</link>
      <description>&lt;P&gt;Easy and boring with a data step.&lt;/P&gt;
&lt;P&gt;Sort by person, date and descending quantity so the maxiumum quantity is first with multiple dates and then take the first record of each person/attribute group.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;EDIT: Forgot attribute, added that in.&amp;nbsp;&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 personID attribute date descending qt1;
run;

data want;
set have;
by personID attribute date;
if first.attribute;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18331"&gt;@supp&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello SAS community,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have come across this scenario a few times and don't feel I have found or created a good solution. Lets say we have data as such:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines;
		input personID $ attribute1 $ qty  date yymmdd10.;
		format date yymmdd10.;
datalines;
A m1 10 2019-03-01
A m1 20 2019-03-01
A m1 20 2019-02-01
A m2 20 2019-01-01
A m3 20 2019-02-01
B m3 10 2019-02-15
B m3 20 2019-02-15
C m2 20 2019-01-15
D m1 20 2019-01-15
D m1 20 2019-02-15
;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;We can have multiple observations per person per attribute. We want to return one observation per person per attribute with the earliest date, min(date). In the event there are multiple observations for the same person and attribute then select for the largest qty, max(qty).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Desired output:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;A m1 20 2019-03-01
A m2 20 2019-01-01
A m3 20 2019-02-01
B m3 20 2019-02-15
C m2 20 2019-01-15
D m1 20 2019-01-15
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For starters I tried this to return one row per person per attribute with the earliest date, but it doesn't work. As expected I get the note:&amp;nbsp;NOTE: The query requires remerging summary statistics back with the original data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	/** return one row per person and attribute **/
	create table want1 as
		select *
		from work.have
		group by personID, attribute1
		having min(date);
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is there a simple solution for this that I am not seeing?&amp;nbsp;Is SQL the best approach? Or is there a better approach using a data step?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2019 16:53:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588586#M168230</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-09-13T16:53:41Z</dc:date>
    </item>
    <item>
      <title>Re: Good method for selecting observations based on min or max date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588587#M168231</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18331"&gt;@supp&lt;/a&gt;&amp;nbsp; &amp;nbsp;The way you frame questions is very intriguing and nice. This time, i am just gonna correct your SQL and wait for others to respond with their innovative answers. I am gonna see if I can test myself i.e my own capability to come up with something that others haven't. But mind you, if somebody among the likes of PGstats, Tom, Paul D, Reinhard, John King, Xia Keshan et al etc has answered. I am just gonna take that notes. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; and be in awe.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You missed an equality operator to filter the group having identified the MIN&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	/** return one row per person and attribute **/
	create table want1 as
		select *
		from work.have
		group by personID, attribute1
		having min(date)=date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2019 16:56:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588587#M168231</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-13T16:56:24Z</dc:date>
    </item>
    <item>
      <title>Re: Good method for selecting observations based on min or max date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588588#M168232</link>
      <description>&lt;P&gt;Use a subquery:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select *
from (select * from have group by personId, attribute1 having date = min(date))
group by personId, attribute1, date
having qty = max(qty);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Sep 2019 16:55:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588588#M168232</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-09-13T16:55:30Z</dc:date>
    </item>
    <item>
      <title>Re: Good method for selecting observations based on min or max date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588598#M168236</link>
      <description>&lt;P&gt;Does this work?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
 select *
	from work.have
     group by personID, attribute1
	having max(qty)=qty and min(date)=date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Sep 2019 17:22:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588598#M168236</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-13T17:22:08Z</dc:date>
    </item>
    <item>
      <title>Re: Good method for selecting observations based on min or max date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588602#M168239</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt; This can't always work because the max(qty) might not occur on the first date.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2019 17:27:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588602#M168239</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-09-13T17:27:38Z</dc:date>
    </item>
    <item>
      <title>Re: Good method for selecting observations based on min or max date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588603#M168240</link>
      <description>&lt;P&gt;Thank you Sir&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp; Great catch&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2019 17:29:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588603#M168240</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-13T17:29:02Z</dc:date>
    </item>
    <item>
      <title>Re: Good method for selecting observations based on min or max date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588608#M168242</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;,&amp;nbsp;you are correct.&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;, I think the solution worked in the example I provided because all my scenarios had the max qty on the earliest date per person per attribute. I added person E to the data such that the max qty is not on the earliest date:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines;
		input personID $ attribute1 $ qty  date yymmdd10.;
		format date yymmdd10.;
datalines;
A m1 10 2019-03-01
A m1 20 2019-03-01
A m1 20 2019-02-01
A m2 20 2019-01-01
A m3 20 2019-02-01
B m3 10 2019-02-15
B m3 20 2019-02-15
C m2 10 2019-01-15
D m1 20 2019-01-15
D m1 20 2019-02-15
E m2 30 2019-02-01
E m2 20 2019-01-01
;
run;


proc sql;
	/** return one row per person and attribute **/
	create table want1 as
		select *
		from work.have
		group by personID, attribute1
		having min(date)=date and qty=max(qty);
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We don't return any observations for person E. Want to return the observation with the earliest date:&lt;/P&gt;
&lt;PRE&gt;A	m1	20	2019-02-01
A	m2	20	2019-01-01
A	m3	20	2019-02-01
B	m3	20	2019-02-15
C	m2	10	2019-01-15
D	m1	20	2019-01-15&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Sep 2019 17:36:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588608#M168242</guid>
      <dc:creator>supp</dc:creator>
      <dc:date>2019-09-13T17:36:36Z</dc:date>
    </item>
    <item>
      <title>Re: Good method for selecting observations based on min or max date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588610#M168244</link>
      <description>It depends on the criteria. I read that if records with multiple minimum dates the first time I coded it, or it could be the max if multiple overall which does change the question a bit.</description>
      <pubDate>Fri, 13 Sep 2019 17:37:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588610#M168244</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-09-13T17:37:37Z</dc:date>
    </item>
    <item>
      <title>Re: Good method for selecting observations based on min or max date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588611#M168245</link>
      <description>Thanks for catching my syntax error. That actually helped a lot!</description>
      <pubDate>Fri, 13 Sep 2019 17:37:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588611#M168245</guid>
      <dc:creator>supp</dc:creator>
      <dc:date>2019-09-13T17:37:56Z</dc:date>
    </item>
    <item>
      <title>Re: Good method for selecting observations based on min or max date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588618#M168248</link>
      <description>&lt;P&gt;That's the reason i don't like PG because he is Prodigygeniusstats aka pgstats &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2019 18:12:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588618#M168248</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-13T18:12:12Z</dc:date>
    </item>
    <item>
      <title>Re: Good method for selecting observations based on min or max date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588620#M168249</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Hi&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18331" target="_blank"&gt;@supp&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;The way you frame questions is very intriguing and nice.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;, if you have any suggestions on how to improve asking these questions let me know!&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2019 18:23:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588620#M168249</guid>
      <dc:creator>supp</dc:creator>
      <dc:date>2019-09-13T18:23:13Z</dc:date>
    </item>
    <item>
      <title>Re: Good method for selecting observations based on min or max date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588625#M168251</link>
      <description>&lt;P&gt;You are doing great. We love it!&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2019 18:33:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588625#M168251</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-13T18:33:27Z</dc:date>
    </item>
    <item>
      <title>Re: Good method for selecting observations based on min or max date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588721#M168299</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18331"&gt;@supp&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A third competitor besides PROC SQL (using GROUP BY, HAVING and nested subqueries) and the combination of PROC SORT and a DATA step (using BY and FIRST.&lt;EM&gt;by-variable&lt;/EM&gt; or LAST.&lt;EM&gt;by-variable&lt;/EM&gt;) is &lt;A href="https://documentation.sas.com/?docsetId=proc&amp;amp;docsetTarget=p0aq3hsvflztfzn1xa2wt6s35oy6.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank" rel="noopener"&gt;PROC SUMMARY&lt;/A&gt;&amp;nbsp;(or its twin PROC MEANS):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have nway;
class personID attribute1;
output out=want(drop=_:) idgrp(min(date) max(qty) out (date qty)=);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the input dataset is sorted by the grouping variables, which is the case in your sample data, you can replace the CLASS statement with a BY statement and omit the NWAY option:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have;
by personID attribute1;
output out=want(drop=_:) idgrp(min(date) max(qty) out (date qty)=);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I always have difficulties remembering the syntax of the&amp;nbsp;&lt;A href="https://documentation.sas.com/?docsetId=proc&amp;amp;docsetTarget=p04vbvpcjg2vrjn1v8wyf0daypfi.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en#p1p58yhxlrc0can1scam7bco7y96" target="_blank" rel="noopener"&gt;IDGROUP&lt;/A&gt; (alias IDGRP) specification, but actually it's quite logical: Within the BY group (or combination of CLASS variable values) take the &lt;FONT face="courier new,courier"&gt;min(date)&lt;/FONT&gt; and, if there are ties, use &lt;FONT face="courier new,courier"&gt;max(qty)&lt;/FONT&gt; as a tiebreaker (and if there are still ties left, use the &lt;EM&gt;first&lt;/EM&gt;&amp;nbsp;of the tied observations in the input dataset -- or the last if the keyword LAST is inserted before OUT). The list of ID variables, &lt;FONT face="courier new,courier"&gt;(date qty)&lt;/FONT&gt;, could include more variables if desired (e.g. all remaining variables of HAVE, if any).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Like the PROC SORT/DATA step approach PROC SUMMARY guarantees that only one observation per group will be included in the output dataset (see tiebreaking rules), whereas the HAVING clause of PROC SQL is prone to include tied observations (which may or may not be desirable, depending on the task).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's also easily extendable to more than just two ID variables (date, qty). PROC SQL, however, would require additional levels of nested subqueries because the minimum/maximum specifications refer to different groupings and therefore require different GROUP BY clauses.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC SUMMARY shares with PROC SQL the advantages of doing everything in one step and not requiring sorted/indexed input data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(I think the first date in the "desired output" in your initial post should read&amp;nbsp;&lt;FONT face="courier new,courier"&gt;2019-0&lt;STRONG&gt;2&lt;/STRONG&gt;-01&lt;/FONT&gt;.)&lt;/P&gt;</description>
      <pubDate>Sat, 14 Sep 2019 12:06:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588721#M168299</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-09-14T12:06:00Z</dc:date>
    </item>
    <item>
      <title>Re: Good method for selecting observations based on min or max date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588855#M168353</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18331"&gt;@supp&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;From your description, it looks as though the date in the first output record should rather be 2019-02-01, as it precedes 2019-03-01 for the same ID "A" and attribute "m1".&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Sep 2019 17:13:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588855#M168353</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-09-15T17:13:19Z</dc:date>
    </item>
    <item>
      <title>Re: Good method for selecting observations based on min or max date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588888#M168358</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18331"&gt;@supp&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;You've got a cornucopia of fine solutions, so methought another one, based on the hash object's prowess, wouldn't hurt at least from the standpoint of learning how it can be done. Note that like MEANS/SUMMARY it doesn't need the input file to be sorted, so I use it unsorted and have also added another satellite variable, VAR, to see if it gets transplanted from the input to the output correctly (it does). Despite the unsorted input, the output comes out sorted (again sharing the trait with MEANS/SUMMARY).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                                                                             
  input personID $ attribute1 $ qty  date yymmdd10. var ;                                                                               
  format date yymmdd10. ;                                                                                                               
  cards ;                                                                                                                               
A m3 20 2019-02-01  1                                                                                                                   
B m3 10 2019-02-15  2                                                                                                                   
A m1 20 2019-03-01  3                                                                                                                   
B m3 20 2019-02-15  4                                                                                                                   
A m1 20 2019-02-01  5                                                                                                                   
C m2 20 2019-01-15  6                                                                                                                   
D m1 20 2019-01-15  7                                                                                                                   
A m1 10 2019-03-01  8                                                                                                                   
D m1 20 2019-02-15  9                                                                                                                   
A m2 20 2019-01-01 10                                                                                                                   
;                                                                                                                                       
run;                                                                                                                                    
                                                                                                                                        
data _null_ ;                                                                                                                           
  dcl hash h (dataset:"have(obs=0)", ordered:"a") ;                                                                                     
  h.definekey  ("personID", "attribute1") ;                                                                                             
  h.definedata (all:"y") ;                                                                                                              
  h.definedone () ;                                                                                                                     
  do until (z) ;                                                                                                                        
    set have (rename = (qty=_q date=_d var=_v)) end = z ;                                                                               
    if not (h.find() or (_d &amp;lt; date or _d = date and _q &amp;gt; qty)) then continue ;                                                          
    date = _d ;                                                                                                                         
    qty  = _q ;                                                                                                                         
    var  = _v ;                                                                                                                         
    h.replace() ;                                                                                                                       
  end ;                                                                                                                                 
  h.output (dataset:"want") ;                                                                                                           
  format date yymmdd10. ;                                                                                                               
run ;                                                       
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Output:&lt;/P&gt;
&lt;PRE&gt;person                                          
  ID      attribute1    qty          date    var
------------------------------------------------
  A           m1         20    2019-02-01      5
  A           m2         20    2019-01-01     10
  A           m3         20    2019-02-01      1
  B           m3         20    2019-02-15      4
  C           m2         20    2019-01-15      6
  D           m1         20    2019-01-15      7
&lt;/PRE&gt;
&lt;P&gt;Frankly, I prefer the brevity of&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;'s MEANS/SUMMARY offering, except for one subtlety: With the hash, bringing another satellite variable like VAR above is a simple business and it works properly. I've tried to do the same by adding the ID statement in SUMMARY; but the values of VAR it generated came out incorrectly in the sense that not all of them aligned with the corresponding output (date,qty) pairs. Hope&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;will chime in on that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Sep 2019 21:51:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588888#M168358</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-09-15T21:51:02Z</dc:date>
    </item>
    <item>
      <title>Re: Good method for selecting observations based on min or max date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588894#M168361</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;wrote:
&lt;P&gt;Frankly, I prefer the brevity of&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;'s MEANS/SUMMARY offering, except for one subtlety: With the hash, bringing another satellite variable like VAR above is a simple business and it works properly. I've tried to do the same by adding the ID statement in SUMMARY; but the values of VAR it generated came out incorrectly in the sense that not all of them aligned with the corresponding output (date,qty) pairs. Hope&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;will chime in on that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Hi Paul,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks. It's true that adding the ID&lt;EM&gt; statement&lt;/EM&gt;&amp;nbsp;to the suggested code can bring in (unwanted) values from a different observation. What I alluded to in my post was adding more ID &lt;EM&gt;variables&lt;/EM&gt; to the existing list &lt;FONT face="courier new,courier"&gt;(date qty)&lt;/FONT&gt; in the IDGRP specification, like &lt;FONT face="courier new,courier"&gt;(date qty var1 var2)&lt;/FONT&gt;. In this case the results were correct at least in the tests I performed before posting my suggestion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Sep 2019 22:29:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588894#M168361</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-09-15T22:29:10Z</dc:date>
    </item>
    <item>
      <title>Re: Good method for selecting observations based on min or max date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588906#M168364</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;Aha! I see. Thanks for the prompt reply!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Sun, 15 Sep 2019 23:41:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/588906#M168364</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-09-15T23:41:33Z</dc:date>
    </item>
    <item>
      <title>Re: Good method for selecting observations based on min or max date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/589016#M168401</link>
      <description>Good catch &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;. I have corrected my oiginal post.</description>
      <pubDate>Mon, 16 Sep 2019 12:26:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Good-method-for-selecting-observations-based-on-min-or-max-date/m-p/589016#M168401</guid>
      <dc:creator>supp</dc:creator>
      <dc:date>2019-09-16T12:26:03Z</dc:date>
    </item>
  </channel>
</rss>

