<?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 Compare dates but only up to the same length in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602881#M16865</link>
    <description>&lt;P&gt;Is there a quick way to compare two dates but only up to the same lengths, whichever the shorter length is between the two?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, if we compare date A and date B, and if date A is 2019-07-08 and date B is 2019-07-08T10:30, then SAS would say date B is bigger after comparing 2019-07-08 and 2019-07-08T10:30. Can we ask SAS to compare only up to length=10 (compare 2019-07-08 vs. 2019-07-08 instead of&amp;nbsp;2019-07-08 vs. 2019-07-08T10:30)?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This rule would apply to any lengths of the two dates in the same format: if we have two dates 2019-07-08T08:09:11 and 2019-07-08T08:08, then SAS would drop seconds for the first date and compare&amp;nbsp;2019-07-08T08:09 vs&amp;nbsp;2019-07-08T08:08.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 08 Nov 2019 21:19:18 GMT</pubDate>
    <dc:creator>gsk</dc:creator>
    <dc:date>2019-11-08T21:19:18Z</dc:date>
    <item>
      <title>Compare dates but only up to the same length</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602881#M16865</link>
      <description>&lt;P&gt;Is there a quick way to compare two dates but only up to the same lengths, whichever the shorter length is between the two?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, if we compare date A and date B, and if date A is 2019-07-08 and date B is 2019-07-08T10:30, then SAS would say date B is bigger after comparing 2019-07-08 and 2019-07-08T10:30. Can we ask SAS to compare only up to length=10 (compare 2019-07-08 vs. 2019-07-08 instead of&amp;nbsp;2019-07-08 vs. 2019-07-08T10:30)?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This rule would apply to any lengths of the two dates in the same format: if we have two dates 2019-07-08T08:09:11 and 2019-07-08T08:08, then SAS would drop seconds for the first date and compare&amp;nbsp;2019-07-08T08:09 vs&amp;nbsp;2019-07-08T08:08.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Nov 2019 21:19:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602881#M16865</guid>
      <dc:creator>gsk</dc:creator>
      <dc:date>2019-11-08T21:19:18Z</dc:date>
    </item>
    <item>
      <title>Re: Compare dates but only up to the same length</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602882#M16866</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/172057"&gt;@gsk&lt;/a&gt;&amp;nbsp; Are your date and datetime variables numeric variables formatted in the way you have shown or those are character variables. Can you check using proc contents plz&lt;/P&gt;</description>
      <pubDate>Fri, 08 Nov 2019 21:22:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602882#M16866</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-11-08T21:22:51Z</dc:date>
    </item>
    <item>
      <title>Re: Compare dates but only up to the same length</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602885#M16867</link>
      <description>&lt;P&gt;Use the colon modifier in your comparison, assuming you have character variables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if date1 =: date2 then do;

.....


end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;You can compare only a specified prefix of a character expression by using a colon (:) after the comparison operator. SAS truncates the longer value to the length of the shorter value during the comparison. In the following example, the colon modifier after the equal sign tells SAS to look at only the first character of values of the variable LastName and to select the observations with names beginning with the letter&lt;/SPAN&gt;&lt;CODE class="xis-codeDefaultStyle"&gt;S&lt;/CODE&gt;&lt;SPAN&gt;:&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://documentation.sas.com/?docsetId=lrcon&amp;amp;docsetTarget=p00iah2thp63bmn1lt20esag14lh.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en#p1vu0ts68u42xun141p7vt4ne29k"&gt;https://documentation.sas.com/?docsetId=lrcon&amp;amp;docsetTarget=p00iah2thp63bmn1lt20esag14lh.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en#p1vu0ts68u42xun141p7vt4ne29k&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Nov 2019 21:26:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602885#M16867</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-11-08T21:26:35Z</dc:date>
    </item>
    <item>
      <title>Re: Compare dates but only up to the same length</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602886#M16868</link>
      <description>&lt;P&gt;Make sure that your dates, times and datetime variables are actual SAS date, time or datetime variables. These would show in proc contents or the data set description as 1) numeric values and 2) a format similar to Date9, MMDDYY10, YYMMDD10 for dates, TIME. for time values and DATETIME or E8601 and such.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then when you need to compare "bits" of the values there are functions such as&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If datevariable = datepart (datetimevariable)&lt;/P&gt;
&lt;P&gt;if timevariable = timepart (datetimevariable)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;or even use of the date functions:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If 12 = month(datepart(datetimevariable)); To get December records.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Nov 2019 23:22:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602886#M16868</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-11-08T23:22:35Z</dc:date>
    </item>
    <item>
      <title>Re: Compare dates but only up to the same length</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602887#M16869</link>
      <description>&lt;P&gt;Thank you for the response! They are both character variables with length=20. Dates are in 2019-07-12 format, and hour, min, and sec come after T with colons (i.e. T14:29:30) if there is any. There doesn't need to be all components of year, month, date, hour, min or sec. A date could have only the year and month or can be missing.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Nov 2019 21:31:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602887#M16869</guid>
      <dc:creator>gsk</dc:creator>
      <dc:date>2019-11-08T21:31:48Z</dc:date>
    </item>
    <item>
      <title>Re: Compare dates but only up to the same length</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602893#M16870</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/172057"&gt;@gsk&lt;/a&gt;&amp;nbsp; &amp;nbsp;Why not doing it clean with a yymmdd10. informat?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
date_A='2019-07-08';
date_B= '2019-07-08T10:30';
if input(date_A,yymmdd10.)=input(date_B,yymmdd10.) then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Nov 2019 21:34:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602893#M16870</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-11-08T21:34:38Z</dc:date>
    </item>
    <item>
      <title>Re: Compare dates but only up to the same length</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602899#M16871</link>
      <description>But there could be cases like 2019-07-08T03 vs 2019-07-08T03:08 or 2017-03-14T21:33:20 vs 2017-03-14T21:33.</description>
      <pubDate>Fri, 08 Nov 2019 21:46:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602899#M16871</guid>
      <dc:creator>gsk</dc:creator>
      <dc:date>2019-11-08T21:46:27Z</dc:date>
    </item>
    <item>
      <title>Re: Compare dates but only up to the same length</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602901#M16872</link>
      <description>Did you try my solution?</description>
      <pubDate>Fri, 08 Nov 2019 21:48:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602901#M16872</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-11-08T21:48:03Z</dc:date>
    </item>
    <item>
      <title>Re: Compare dates but only up to the same length</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602905#M16873</link>
      <description>Sorry, not yet! I will in a bit!</description>
      <pubDate>Fri, 08 Nov 2019 21:49:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602905#M16873</guid>
      <dc:creator>gsk</dc:creator>
      <dc:date>2019-11-08T21:49:47Z</dc:date>
    </item>
    <item>
      <title>Re: Compare dates but only up to the same length</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602911#M16876</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/172057"&gt;@gsk&lt;/a&gt;&amp;nbsp; So what. You are only comparing the first 10 chars in your words length=10 and I would call them a width of 10 in a informat, Therefore what is after 10 chars in my opinion is garbage for SAS's informat yymmdd10.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And the date check becomes &lt;STRONG&gt;real and safe&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
date_A='2019-07-08T03 ';
date_B= '2019-07-08T03:08 ';
if input(date_A,yymmdd10.)=input(date_B,yymmdd10.) then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Nov 2019 22:07:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602911#M16876</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-11-08T22:07:25Z</dc:date>
    </item>
    <item>
      <title>Re: Compare dates but only up to the same length</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602921#M16879</link>
      <description>&lt;P&gt;I agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;'s suggestion but, to account for leading and trailing blanks, I think you will want to add the strip function to the variables you're comparing. E.g., try the following code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input (date1 date2) ($20.);
  if _n_ eq 3 then date1=' '||trim(date1);
  if date1 =: date2 then x=1;
  if trim(date1) =: trim(date2) then y=1;
  if strip(date1) =: strip(date2) then z=1;
  cards;
2019-07-08T08:08:11 2019-07-08T08:08
2019-07-08T08:08    2019-07-08T08:08:11
2019-07-08T08:08    2019-07-08T08:08:11
;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Nov 2019 22:23:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602921#M16879</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2019-11-08T22:23:26Z</dc:date>
    </item>
    <item>
      <title>Re: Compare dates but only up to the same length</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602987#M16889</link>
      <description>&lt;P&gt;Thank you everyone!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Either&amp;nbsp;input(dateA, yymmdd10.)=input(dateB,yymmdd10.) or dateA=:dateB works&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the end, I did strip(dateA)=:strip(dateB). Thanks a lot!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 09 Nov 2019 15:14:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Compare-dates-but-only-up-to-the-same-length/m-p/602987#M16889</guid>
      <dc:creator>gsk</dc:creator>
      <dc:date>2019-11-09T15:14:25Z</dc:date>
    </item>
  </channel>
</rss>

