<?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 Using MIN and MAX date to extract the additional row from master table. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-MIN-and-MAX-date-to-extract-the-additional-row-from-master/m-p/498065#M132246</link>
    <description>&lt;P&gt;Hi Community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here I have a dataset as a Master table(AAA) where I already fetch some data from Master(AAA) to sub_master table(BBB).&lt;/P&gt;&lt;P&gt;(Table1)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data AAA;
input ID date_A ddmmyy10. grade;
cards;
111111 02/07/2013 1
111111 02/04/2014 1
111111 29/12/2014 .
111111 05/04/2014 .
100000 01/10/2012 .
100000 08/10/2015 2
100000 10/01/2017 5
100000 29/05/2018 2
100000 30/12/2019 .
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;In my BBB table I already fetch some data from AAA table using some condition.&amp;nbsp; &amp;nbsp;Now the (Table 2) is look like&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;date_B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;GRD&lt;/P&gt;&lt;P&gt;111111&amp;nbsp; &amp;nbsp;02/07/2013&amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;111111&amp;nbsp; &amp;nbsp; 02/04/2014&amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;100000&amp;nbsp; 10/01/2017&amp;nbsp; &amp;nbsp; 5&lt;BR /&gt;100000&amp;nbsp; 29/05/2018&amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Table 1 = AAA&lt;/P&gt;&lt;P&gt;Table 2 = BBB&lt;/P&gt;&lt;P&gt;Now i have to additionally insert some rows from AAA to BBB.&lt;/P&gt;&lt;P&gt;The condition is&lt;/P&gt;&lt;P&gt;First choose the max(date_B) and find the min(date_A) &amp;gt; max(date_B) and&amp;nbsp;WHERE grade = '.'&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;(Which means 111111 29/12/2014 . should affect)&lt;/P&gt;&lt;P&gt;(Which means 100000 30/12/2019 . should affect)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;How can I edit my query to get the result? Any help will be much appreciated!&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Sat, 22 Sep 2018 12:21:18 GMT</pubDate>
    <dc:creator>Sathish_jammy</dc:creator>
    <dc:date>2018-09-22T12:21:18Z</dc:date>
    <item>
      <title>Using MIN and MAX date to extract the additional row from master table.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-MIN-and-MAX-date-to-extract-the-additional-row-from-master/m-p/498065#M132246</link>
      <description>&lt;P&gt;Hi Community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here I have a dataset as a Master table(AAA) where I already fetch some data from Master(AAA) to sub_master table(BBB).&lt;/P&gt;&lt;P&gt;(Table1)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data AAA;
input ID date_A ddmmyy10. grade;
cards;
111111 02/07/2013 1
111111 02/04/2014 1
111111 29/12/2014 .
111111 05/04/2014 .
100000 01/10/2012 .
100000 08/10/2015 2
100000 10/01/2017 5
100000 29/05/2018 2
100000 30/12/2019 .
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;In my BBB table I already fetch some data from AAA table using some condition.&amp;nbsp; &amp;nbsp;Now the (Table 2) is look like&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;date_B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;GRD&lt;/P&gt;&lt;P&gt;111111&amp;nbsp; &amp;nbsp;02/07/2013&amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;111111&amp;nbsp; &amp;nbsp; 02/04/2014&amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;100000&amp;nbsp; 10/01/2017&amp;nbsp; &amp;nbsp; 5&lt;BR /&gt;100000&amp;nbsp; 29/05/2018&amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Table 1 = AAA&lt;/P&gt;&lt;P&gt;Table 2 = BBB&lt;/P&gt;&lt;P&gt;Now i have to additionally insert some rows from AAA to BBB.&lt;/P&gt;&lt;P&gt;The condition is&lt;/P&gt;&lt;P&gt;First choose the max(date_B) and find the min(date_A) &amp;gt; max(date_B) and&amp;nbsp;WHERE grade = '.'&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;(Which means 111111 29/12/2014 . should affect)&lt;/P&gt;&lt;P&gt;(Which means 100000 30/12/2019 . should affect)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;How can I edit my query to get the result? Any help will be much appreciated!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Sep 2018 12:21:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-MIN-and-MAX-date-to-extract-the-additional-row-from-master/m-p/498065#M132246</guid>
      <dc:creator>Sathish_jammy</dc:creator>
      <dc:date>2018-09-22T12:21:18Z</dc:date>
    </item>
    <item>
      <title>Re: Using MIN and MAX date to extract the additional row from master table.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-MIN-and-MAX-date-to-extract-the-additional-row-from-master/m-p/498085#M132254</link>
      <description>Use nested SQL queries.&lt;BR /&gt;&lt;BR /&gt;select * form A &lt;BR /&gt;where date_A GT (select max(date_B) from B)&lt;BR /&gt;having date_A=min(date_A) AND grade = '.'&lt;BR /&gt;&lt;BR /&gt;You probably meant to group by ID because the last condition does not make sense other wise.</description>
      <pubDate>Sat, 22 Sep 2018 16:14:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-MIN-and-MAX-date-to-extract-the-additional-row-from-master/m-p/498085#M132254</guid>
      <dc:creator>Haris</dc:creator>
      <dc:date>2018-09-22T16:14:09Z</dc:date>
    </item>
    <item>
      <title>Re: Using MIN and MAX date to extract the additional row from master table.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-MIN-and-MAX-date-to-extract-the-additional-row-from-master/m-p/498086#M132255</link>
      <description>&lt;P&gt;First choose the max(date_B) and find the min(date_A) &amp;gt; max(date_B) and&amp;nbsp;WHERE grade = '.'&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;(Which means 111111 29/12/2014 . should affect)&lt;/P&gt;&lt;P&gt;(Which means 100000 30/12/2019 . should affect)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;are you sure this statement is true for ID 111111 results, would record dated 4-5-2014 be less than 12-29-2014?&lt;/P&gt;&lt;P&gt;I believe this code does what you are asking for correctly.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Master;
input ID dt : ddmmyy10. grade;
cards;
111111 02/07/2013 1
111111 02/04/2014 1
111111 29/12/2014 .
111111 05/04/2014 .
100000 01/10/2012 .
100000 08/10/2015 .
100000 10/01/2017 5
100000 29/05/2018 1
100000 30/12/2019 .
111122 05/01/1922 .
111122 07/12/1932 .
111122 05/01/1933 .
111122 05/01/1935 .
111122 05/01/2018 1
111122 05/02/2018 .
;
run;
proc sort data=master;
	by id dt;
run;
data sub_master;
input
ID           dt : ddmmyy10.           GRD;
cards;
111111  02/07/2013    1
111111  02/04/2014    1
100000  10/01/2017    5
100000  29/05/2018    2
111122  05/01/2018    1
;
proc sort data=sub_master;
	by id dt;
run;

proc sql;
	create table min_master as
	select distinct a.id, max(a.dt), a.dt, a.grade as grd 
	from master as a,
		 sub_master as b
	where a.grade = . and a.id = b.id and a.dt &amp;gt; b.dt
	group by a.ID, b.id
	having a.dt = min(a.dt)	and b.dt = max(b.dt)
; 
 
quit;

data want;
	set sub_master
		min_master(drop=_TEMG001);
		format dt date10.;
run;

proc sort data=want;
	by id dt;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 22 Sep 2018 16:16:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-MIN-and-MAX-date-to-extract-the-additional-row-from-master/m-p/498086#M132255</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2018-09-22T16:16:56Z</dc:date>
    </item>
    <item>
      <title>Re: Using MIN and MAX date to extract the additional row from master table.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-MIN-and-MAX-date-to-extract-the-additional-row-from-master/m-p/498285#M132378</link>
      <description>&lt;P&gt;Thanks you so much for your suggestions!&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/122002"&gt;@VDD&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13995"&gt;@Haris&lt;/a&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Sep 2018 05:39:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-MIN-and-MAX-date-to-extract-the-additional-row-from-master/m-p/498285#M132378</guid>
      <dc:creator>Sathish_jammy</dc:creator>
      <dc:date>2018-09-24T05:39:13Z</dc:date>
    </item>
    <item>
      <title>Re: Using MIN and MAX date to extract the additional row from master table.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-MIN-and-MAX-date-to-extract-the-additional-row-from-master/m-p/498370#M132427</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/122002"&gt;@VDD&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your code works well.&lt;/P&gt;&lt;P&gt;But I have a reverse task from the dataset and I tried but it endup up with partial output.&lt;/P&gt;&lt;P&gt;Task 1&amp;nbsp; (COMPLETED)&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;First choose the MAX(date_B) and find the MIN(date_A) &amp;gt; MAX(date_B) and&amp;nbsp;WHERE grade = '.'&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;TASK 2 (PARTIAL OP)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;-----&amp;nbsp; choose the MIN(date_B) and find the MAX(date_A) &amp;lt; MIN(date_B) and&amp;nbsp;WHERE grade = '.'&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The example dataset which I mention in earlier post performs well with TASK2. But not works in my original dataset.&amp;nbsp;&lt;/SPAN&gt;&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;/*****    TASK 1   **********/ /* You suggested*/ /** COMPLETED**/
Proc Sql;
create table Mater_min as
select distinct a.P_IDA, 
	max(a.Record_Date_A),a.Record_Date_A,a.Grade_A
	from Master as a, SubMaster as b
	where a.Grade_A = '' and a.P_IDA = b.P_IDB and a.Record_Date_A &amp;gt; b.Record_date_B
	group by a.P_IDA,b.P_IDB
	having a.Record_Date_A = min(a.Record_Date_A) and b.Record_Date_B = max(b.Record_date_B);
	quit;

/*******  TASK 2****/  /*What I edited for reverse method*/ /*Results PArtial Output*******/
Proc Sql;
create table Mater_max as
select distinct a.P_IDA, 
	min(a.Record_Date_A),a.Record_Date_A,a.Grade_A
	from Master as a, SubMaster as b
	where a.Grade_A = '' and a.P_IDA = b.P_IDB and a.Record_Date_A &amp;lt; b.Record_Date_B
	group by a.P_IDA,b.P_IDB
	having a.Record_Date_A = max(a.Record_Date_A) and b.Record_Date_B = min(b.Record_date_B);
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;HERE I ATTACH the original dataset Please gothrough it and let me know my error.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks in Advance!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Sep 2018 12:26:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-MIN-and-MAX-date-to-extract-the-additional-row-from-master/m-p/498370#M132427</guid>
      <dc:creator>Sathish_jammy</dc:creator>
      <dc:date>2018-09-24T12:26:40Z</dc:date>
    </item>
  </channel>
</rss>

