<?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: Help needed in selecting the observation with the latest date (from 5 date variables, no hierarc in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-needed-in-selecting-the-observation-with-the-latest-date/m-p/584293#M166384</link>
    <description>&lt;P&gt;Welcome to the SAS Community &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have(drop=i);
	array VarDate{5};
	do ID="A", "B", "C";
		do varID=1 to 5;
			do i=1 to 5;
				VarDate[i]=rand('integer', '01jan2019'd, today());
			end;
			output;
		end;
	end;
	format VarDate: date9.;
run;

data want;
	do until (last.ID);
		set have;
		by ID;
		if max(of VarDate:) ge _iorc_ then _iorc_=max(of VarDate:);
	end;
	do until (last.ID);
		set have;
		by ID;
		if max(of VarDate:)=_iorc_ then output;
	end;
	_iorc_=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 27 Aug 2019 16:27:55 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2019-08-27T16:27:55Z</dc:date>
    <item>
      <title>Help needed in selecting the observation with the latest date (from 5 date variables, no hierarchy)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-needed-in-selecting-the-observation-with-the-latest-date/m-p/584285#M166376</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As per title, I need help in selecting the observation with the latest date (from 5 date variables, no hierarchy) and from there selecting the one with the highest VarId by group.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example Data:&lt;/P&gt;&lt;P&gt;Group,VarID,VarDate1,VarDate2,VarDate3,VarDate4,VarDate5&lt;/P&gt;&lt;P&gt;A,15,Oct2018,Oct2018,.,.,Oct2018&lt;/P&gt;&lt;P&gt;A,2,.,.,.,.,Jan2019&lt;/P&gt;&lt;P&gt;A,3,.,.,Sept2018,.,.,&lt;/P&gt;&lt;P&gt;A,14,.,.,.,.,Dec2018&lt;/P&gt;&lt;P&gt;B,45,July2018,.,.,.,July2018&lt;/P&gt;&lt;P&gt;B,46,.,.,.,.,.July2018&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Result Data should be:&lt;/P&gt;&lt;P&gt;A,2,.,.,.,.,Jan2019&lt;/P&gt;&lt;P&gt;B,46,.,.,.,.,.July2018&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 27 Aug 2019 16:02:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-needed-in-selecting-the-observation-with-the-latest-date/m-p/584285#M166376</guid>
      <dc:creator>ohemgee</dc:creator>
      <dc:date>2019-08-27T16:02:42Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed in selecting the observation with the latest date (from 5 date variables, no hierarc</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-needed-in-selecting-the-observation-with-the-latest-date/m-p/584286#M166377</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/286856"&gt;@ohemgee&lt;/a&gt;&amp;nbsp; Welcome to SAS communities. Assuming those are all SAS numeric dates, the following may work for your requirement though untested&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do _n_=1 by 1 until(last.group);
 set have;
 by group;
 array t(*) VarDate1--VarDate5;
 max=max(max,max(of t(*)));
end;
do _n_=1 to _n_;
 set have;
 if max in t then output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Aug 2019 16:07:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-needed-in-selecting-the-observation-with-the-latest-date/m-p/584286#M166377</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-27T16:07:38Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed in selecting the observation with the latest date (from 5 date variables, no hierarc</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-needed-in-selecting-the-observation-with-the-latest-date/m-p/584288#M166379</link>
      <description>&lt;P&gt;Assuming that you have actual dates in those variables and not the strings you are showing then you can just make new variable with the maximum of the individual date variables.&amp;nbsp; If you just have strings then you will need to convert them to actual dates first.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can then use the new variable to order the data and find the last value per group.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data step1;
  set have ;
  max_date=max(of vardate1-vardate5);
  format max_date yymmdd10.;
run;
proc sort data=step1 out=want ;
   by group max_date varid ;
run;
data want;
  set want;
  by group;
  if last.group;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Aug 2019 16:12:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-needed-in-selecting-the-observation-with-the-latest-date/m-p/584288#M166379</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-08-27T16:12:27Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed in selecting the observation with the latest date (from 5 date variables, no hierarc</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-needed-in-selecting-the-observation-with-the-latest-date/m-p/584289#M166380</link>
      <description>&lt;P&gt;A data step works here as well:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;

by group;

retain _temp;
if first.group then call missing(_temp);

_temp = max(of vardate1-vardate5);
_current_max = max(_temp, _current_max);

*keep right varID;
if _current_max = _temp then newVarID = varID;


if last.group then output;

keep group newvarID _current_max;
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/286856"&gt;@ohemgee&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As per title, I need help in selecting the observation with the latest date (from 5 date variables, no hierarchy) and from there selecting the one with the highest VarId by group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example Data:&lt;/P&gt;
&lt;P&gt;Group,VarID,VarDate1,VarDate2,VarDate3,VarDate4,VarDate5&lt;/P&gt;
&lt;P&gt;A,15,Oct2018,Oct2018,.,.,Oct2018&lt;/P&gt;
&lt;P&gt;A,2,.,.,.,.,Jan2019&lt;/P&gt;
&lt;P&gt;A,3,.,.,Sept2018,.,.,&lt;/P&gt;
&lt;P&gt;A,14,.,.,.,.,Dec2018&lt;/P&gt;
&lt;P&gt;B,45,July2018,.,.,.,July2018&lt;/P&gt;
&lt;P&gt;B,46,.,.,.,.,.July2018&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result Data should be:&lt;/P&gt;
&lt;P&gt;A,2,.,.,.,.,Jan2019&lt;/P&gt;
&lt;P&gt;B,46,.,.,.,.,.July2018&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Aug 2019 16:12:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-needed-in-selecting-the-observation-with-the-latest-date/m-p/584289#M166380</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-08-27T16:12:31Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed in selecting the observation with the latest date (from 5 date variables, no hierarc</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-needed-in-selecting-the-observation-with-the-latest-date/m-p/584293#M166384</link>
      <description>&lt;P&gt;Welcome to the SAS Community &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have(drop=i);
	array VarDate{5};
	do ID="A", "B", "C";
		do varID=1 to 5;
			do i=1 to 5;
				VarDate[i]=rand('integer', '01jan2019'd, today());
			end;
			output;
		end;
	end;
	format VarDate: date9.;
run;

data want;
	do until (last.ID);
		set have;
		by ID;
		if max(of VarDate:) ge _iorc_ then _iorc_=max(of VarDate:);
	end;
	do until (last.ID);
		set have;
		by ID;
		if max(of VarDate:)=_iorc_ then output;
	end;
	_iorc_=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Aug 2019 16:27:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-needed-in-selecting-the-observation-with-the-latest-date/m-p/584293#M166384</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-08-27T16:27:55Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed in selecting the observation with the latest date (from 5 date variables, no hierarc</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-needed-in-selecting-the-observation-with-the-latest-date/m-p/584448#M166467</link>
      <description>&lt;P&gt;Thanks a lot for all your help. I still can't make it work though. Modifying a bit so that it sort of represents the data wherein Group is actually 3 variables. And yes, those are SAS numeric dates. Also looking for the most efficient way as this will run through millions of records.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example Data:&lt;/P&gt;&lt;P&gt;Group1, Group 2, Group3, VarID,VarDate1,VarDate2,VarDate3,VarDate4,VarDate5&lt;/P&gt;&lt;P&gt;A,AA,AAA,15,Oct2018,Oct2018,.,.,Oct2018&lt;/P&gt;&lt;P&gt;A,AA,BBB,2,.,.,.,.,Jan2019&lt;/P&gt;&lt;P&gt;A,AA,CCC,3,.,.,Sept2018,.,.,&lt;/P&gt;&lt;P&gt;A,AA,DDD,14,.,.,.,.,Dec2018&lt;/P&gt;&lt;P&gt;B,AA,AAA,45,July2018,.,.,.,July2018&lt;/P&gt;&lt;P&gt;B,AA,BBB,46,.,.,.,.,.July2018&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Result Data should be:&lt;/P&gt;&lt;P&gt;Group1, Group 2, Group3, VarID,VarDate1,VarDate2,VarDate3,VarDate4,VarDate5&lt;/P&gt;&lt;P&gt;A,AA,BBB,2,.,.,.,.,Jan2019&lt;/P&gt;&lt;P&gt;B,AA,BBB,46,.,.,.,.,.July2018&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 07:35:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-needed-in-selecting-the-observation-with-the-latest-date/m-p/584448#M166467</guid>
      <dc:creator>ohemgee</dc:creator>
      <dc:date>2019-08-28T07:35:46Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed in selecting the observation with the latest date (from 5 date variables, no hierarc</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-needed-in-selecting-the-observation-with-the-latest-date/m-p/584457#M166473</link>
      <description>&lt;P&gt;If your data is representable of your actual data, which means that your data is sorted by Group1, Group2, Group3, then you can do something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have(drop=i);
	array VarDate{5};
	do Group1="A", "B", "C", "D";
       do Group2="AA", "BB", "CC", "DD";
	      do Group3="AAA", "BBB", "CCC", "DDD";
             VarID=ceil(rand('Uniform')*100);
             do i=1 to 5;
		        VarDate[i]=ceil(rand('Uniform', '01jan2019'd, today()));
             end;
             output;
	      end;
	   end;		
	end;
	format VarDate: date9.;
run;

data want(drop=max);
	do until (last.Group1);
		set have;
		by Group1;
        max=max(of VarDate:);
		if max gt _iorc_ then _iorc_=max;
	end;
	do until (last.Group1);
		set have;
		by Group1;
		if max(of VarDate:)=_iorc_ then output;
	end;
	_iorc_=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 28 Aug 2019 08:01:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-needed-in-selecting-the-observation-with-the-latest-date/m-p/584457#M166473</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-08-28T08:01:17Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed in selecting the observation with the latest date (from 5 date variables, no hierarc</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-needed-in-selecting-the-observation-with-the-latest-date/m-p/584582#M166529</link>
      <description>&lt;P&gt;Went with the ff. code: Let me know what you think and a more efficient way is much appreciated. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;max_dte = max(of vardate1-vardate5);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sort data=want out=want2;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;by group1 group2 group3 descending max_dte descending varid;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want3;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;set want2;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;by group1 group2 group3 descending max_dte descending varid;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;if first.max_dte;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sort nodupkey data=want3&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;out=want4 (index=(indx = (group 1 group2 group3)/unique));&lt;/P&gt;&lt;P&gt;by group1 group2 group3;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 14:34:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-needed-in-selecting-the-observation-with-the-latest-date/m-p/584582#M166529</guid>
      <dc:creator>ohemgee</dc:creator>
      <dc:date>2019-08-28T14:34:28Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed in selecting the observation with the latest date (from 5 date variables, no hierarc</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-needed-in-selecting-the-observation-with-the-latest-date/m-p/584601#M166538</link>
      <description>Toms solution is shorter and will have the same results as your code.</description>
      <pubDate>Wed, 28 Aug 2019 15:19:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-needed-in-selecting-the-observation-with-the-latest-date/m-p/584601#M166538</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-08-28T15:19:51Z</dc:date>
    </item>
  </channel>
</rss>

