<?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: Compare dates within the same record in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Compare-dates-within-the-same-record/m-p/636592#M78124</link>
    <description>&lt;P&gt;I'd approach in this way. This assumes all date columns are named with suffix "date_id":&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id (date_id another_date_id comparison_date_id) (:date9.);
format date_id another_date_id comparison_date_id mmddyy10.;
datalines;
1 01apr2020 02apr2020 03apr2020
2 01mar2020 15jan2020 10sep2020
3 01apr2020 02apr2020 03apr2020
4 01apr2020 02apr2020 31mar2020
;
run;

*Get date cols into a macro variable for later array reference;
proc sql noprint;
	select 
		name into :datecols 
		separated by " " 
	from dictionary.columns 
	where 
	libname='WORK' and 
	memname='HAVE' and
	upcase(name) like '%DATE_ID' and
	upcase(name) &amp;lt;&amp;gt; 'COMPARISON_DATE_ID'	
	;
quit;

data want;
	set have;
	flag=0;
	array dates &amp;amp;datecols;
	do over dates; /*Define array to search over*/
		if comparison_date_id &amp;gt; dates then do; /*do comparison*/
			flag=1; /*if TRUE then flag*/
			leave; /*leave loop on first TRUE*/
		end;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 01 Apr 2020 15:52:14 GMT</pubDate>
    <dc:creator>unison</dc:creator>
    <dc:date>2020-04-01T15:52:14Z</dc:date>
    <item>
      <title>Compare dates within the same record</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Compare-dates-within-the-same-record/m-p/636585#M78119</link>
      <description>&lt;P&gt;After some guidance as to how best to get started.&amp;nbsp; I will have a dataset similar to below where I will have approximately 200 date fields and 100 IDs:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;date_id&lt;/TD&gt;&lt;TD&gt;another_date_id&lt;/TD&gt;&lt;TD&gt;yet_another_date_id&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;01/01/2019&lt;/TD&gt;&lt;TD&gt;02/01/2019&lt;/TD&gt;&lt;TD&gt;05/01/2019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;02/01/2019&lt;/TD&gt;&lt;TD&gt;05/01/2019&lt;/TD&gt;&lt;TD&gt;06/01/2019&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am wanting to know if there is a simpler way to do logical calculations.&lt;/P&gt;&lt;P&gt;e.g. if yet_another_date_id &amp;gt; all other date_ids without typing them all out?&lt;/P&gt;</description>
      <pubDate>Wed, 01 Apr 2020 15:29:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Compare-dates-within-the-same-record/m-p/636585#M78119</guid>
      <dc:creator>ChrisTurtle</dc:creator>
      <dc:date>2020-04-01T15:29:05Z</dc:date>
    </item>
    <item>
      <title>Re: Compare dates within the same record</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Compare-dates-within-the-same-record/m-p/636588#M78121</link>
      <description>&lt;P&gt;Is there any pattern to the names? If so, this is easier. If not, you can create a list of the variable names if you include all date variables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So it depends....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if another_date &amp;gt; max(of date_id--yet_another_date_id) then flag=1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The question is how to define that list after the OF.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a reference that illustrates how to refer to variables and datasets in a short cut list:&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/228877"&gt;@ChrisTurtle&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;After some guidance as to how best to get started.&amp;nbsp; I will have a dataset similar to below where I will have approximately 200 date fields and 100 IDs:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;ID&lt;/TD&gt;
&lt;TD&gt;date_id&lt;/TD&gt;
&lt;TD&gt;another_date_id&lt;/TD&gt;
&lt;TD&gt;yet_another_date_id&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;01/01/2019&lt;/TD&gt;
&lt;TD&gt;02/01/2019&lt;/TD&gt;
&lt;TD&gt;05/01/2019&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;02/01/2019&lt;/TD&gt;
&lt;TD&gt;05/01/2019&lt;/TD&gt;
&lt;TD&gt;06/01/2019&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am wanting to know if there is a simpler way to do logical calculations.&lt;/P&gt;
&lt;P&gt;e.g. if yet_another_date_id &amp;gt; all other date_ids without typing them all out?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Apr 2020 15:34:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Compare-dates-within-the-same-record/m-p/636588#M78121</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-04-01T15:34:45Z</dc:date>
    </item>
    <item>
      <title>Re: Compare dates within the same record</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Compare-dates-within-the-same-record/m-p/636590#M78123</link>
      <description>Thank you I will look at the blog post as there will be no pattern to the names I'm afraid</description>
      <pubDate>Wed, 01 Apr 2020 15:37:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Compare-dates-within-the-same-record/m-p/636590#M78123</guid>
      <dc:creator>ChrisTurtle</dc:creator>
      <dc:date>2020-04-01T15:37:47Z</dc:date>
    </item>
    <item>
      <title>Re: Compare dates within the same record</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Compare-dates-within-the-same-record/m-p/636592#M78124</link>
      <description>&lt;P&gt;I'd approach in this way. This assumes all date columns are named with suffix "date_id":&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id (date_id another_date_id comparison_date_id) (:date9.);
format date_id another_date_id comparison_date_id mmddyy10.;
datalines;
1 01apr2020 02apr2020 03apr2020
2 01mar2020 15jan2020 10sep2020
3 01apr2020 02apr2020 03apr2020
4 01apr2020 02apr2020 31mar2020
;
run;

*Get date cols into a macro variable for later array reference;
proc sql noprint;
	select 
		name into :datecols 
		separated by " " 
	from dictionary.columns 
	where 
	libname='WORK' and 
	memname='HAVE' and
	upcase(name) like '%DATE_ID' and
	upcase(name) &amp;lt;&amp;gt; 'COMPARISON_DATE_ID'	
	;
quit;

data want;
	set have;
	flag=0;
	array dates &amp;amp;datecols;
	do over dates; /*Define array to search over*/
		if comparison_date_id &amp;gt; dates then do; /*do comparison*/
			flag=1; /*if TRUE then flag*/
			leave; /*leave loop on first TRUE*/
		end;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Apr 2020 15:52:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Compare-dates-within-the-same-record/m-p/636592#M78124</guid>
      <dc:creator>unison</dc:creator>
      <dc:date>2020-04-01T15:52:14Z</dc:date>
    </item>
    <item>
      <title>Re: Compare dates within the same record</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Compare-dates-within-the-same-record/m-p/636595#M78125</link>
      <description>&lt;P&gt;Once again, the advantage of a long dataset format comes into play:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=long name=datename;
by id;
var _numeric_;
run;

data want;
flag = 1;
do until (last.id);
  merge
    long (
      rename=(col1=_ref)
      where=(datename = "yet_another_date_id")
    )
    long (where=(datename ne "yet_another_date_id"))
  ;
  by id;
  if _ref &amp;lt; col1 then flag = 0;
end;
do until (last.id);
  set long;
  by id;
  if flag then output;
end;
drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you need only write &lt;EM&gt;one&lt;/EM&gt;&amp;nbsp;"variable name".&lt;/P&gt;</description>
      <pubDate>Wed, 01 Apr 2020 15:57:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Compare-dates-within-the-same-record/m-p/636595#M78125</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-01T15:57:36Z</dc:date>
    </item>
  </channel>
</rss>

