<?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: convert character field to date and compare in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/convert-character-field-to-date-and-compare/m-p/805014#M317077</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data TEST;
   input date_have :$20. date_compare : $20.;
   datalines;
00JAN2020 10NOV2020
UNJAN2020 05DEC2020
UNUNK2020 16DEC2019
UNJUN2020 14FEB2021
01JAN2020 01MAY2020
20JUN2020 01JAN2020
;
run;
DATA TEST1;
SET TEST;
/* DELETE 00, UN, UNK BY USING PRXCHANGE FUNCTION */
NEW_have=prxchange('s/^UN|^00|UNK//i',-1,date_have);

if length(NEW_have)=9 then do;
 base=input(strip(NEW_have),date9.);
 compare=input(strip(date_compare),date9.);
end;

if length(NEW_have)=7 then do;
 base=input(strip(NEW_have),monyy7.);
 compare=input(strip(substr(date_compare,3)),monyy7.);
end;

if length(NEW_have)=4 then do;
 base=input(strip(NEW_have),best.);
 compare=input(strip(substr(date_compare,6)),best.);
end;

flag=(base&amp;lt;=compare) ;

drop base compare;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 30 Mar 2022 11:59:42 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2022-03-30T11:59:42Z</dc:date>
    <item>
      <title>convert character field to date and compare</title>
      <link>https://communities.sas.com/t5/SAS-Programming/convert-character-field-to-date-and-compare/m-p/804896#M316997</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How to compare partial date with actual date in sas.&lt;/P&gt;&lt;P&gt;First I need to clean the date_have variable - by removing UN UNK and 00.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data TEST;
   input date_have $ 9. date_compare $ 9.;
   datalines;
00JAN2020 10NOV2020
UNJAN2020 05DEC2020
UNUNK2020 16DEC2019
UNJUN2020 14FEB2021
01JAN2020 01MAY2020
20JUN2020 01JAN2020
;
run;
*Clean the date_have variable - by removing UN UNK and 00.&amp;nbsp;
DATA TEST1;
SET TEST;
/* DELETE 00, UN, UNK BY USING PRXCHANGE FUNCTION */
NEW_have=prxchange('s/^UN|^00|UNK//i',-1,date_have);
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I need to compare new_have variable to date_compare variable.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;Compare Criteria:&amp;nbsp;&lt;/U&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;new_have &amp;lt;=date_compare&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;When I run above code, I need to remove Observation 3 and Observation 6.&lt;/P&gt;&lt;P&gt;Observation 3 - compare date is in 2019 and our original date is in 2020.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Observation 6 - compare date is in JAN2020 and our original date is in JUN2020.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dht115_0-1648588604037.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/69936iCD3916CEAFE82B13/image-size/medium?v=v2&amp;amp;px=400" role="button" title="dht115_0-1648588604037.png" alt="dht115_0-1648588604037.png" /&gt;&lt;/span&gt;&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>Tue, 29 Mar 2022 21:17:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/convert-character-field-to-date-and-compare/m-p/804896#M316997</guid>
      <dc:creator>dht115</dc:creator>
      <dc:date>2022-03-29T21:17:53Z</dc:date>
    </item>
    <item>
      <title>Re: convert character field to date and compare</title>
      <link>https://communities.sas.com/t5/SAS-Programming/convert-character-field-to-date-and-compare/m-p/804907#M317003</link>
      <description>&lt;P&gt;Just what do you compare a value of 2020 to?&lt;/P&gt;
&lt;P&gt;Jun2020?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We need the rules for all of the possible malformed "date_have" and how you intend to consider the value less than or equal.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First step: Your "date_compare" needs to be an actual SAS date value. If not then the first step is to convert it. Informat DATE9.&lt;/P&gt;
&lt;P&gt;NEXT will be to pull pieces out of the remaining malformed values. But without knowing how you determine of Jun2020 &amp;lt;= 10Jun2020 I'm not going to bother to guess.Is your compare criteria to KEEP records when New_have &amp;lt;= Date_Compare or REMOVE them?&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2022 22:27:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/convert-character-field-to-date-and-compare/m-p/804907#M317003</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-03-29T22:27:20Z</dc:date>
    </item>
    <item>
      <title>Re: convert character field to date and compare</title>
      <link>https://communities.sas.com/t5/SAS-Programming/convert-character-field-to-date-and-compare/m-p/804921#M317014</link>
      <description>&lt;P&gt;If date_have is 9 character long (DDMMMYYYY) than need to compare date_have with&amp;nbsp;date_compare&lt;/P&gt;&lt;P&gt;If date_have is 7 character long (MMMYYYY) than need to compare date_have with date_compare (only MMMYYYY) portion.&lt;/P&gt;&lt;P&gt;If date_have is 4 character long (YYYY) than need to compare date_have with date_compare (only YYYY) portion.&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>Wed, 30 Mar 2022 00:33:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/convert-character-field-to-date-and-compare/m-p/804921#M317014</guid>
      <dc:creator>dht115</dc:creator>
      <dc:date>2022-03-30T00:33:18Z</dc:date>
    </item>
    <item>
      <title>Re: convert character field to date and compare</title>
      <link>https://communities.sas.com/t5/SAS-Programming/convert-character-field-to-date-and-compare/m-p/804996#M317068</link>
      <description>&lt;P&gt;What do you mean by "compare". According to these rules:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/324991"&gt;@dht115&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;If date_have is 9 character long (DDMMMYYYY) than need to compare date_have with&amp;nbsp;date_compare&lt;/P&gt;
&lt;P&gt;If date_have is 7 character long (MMMYYYY) than need to compare date_have with date_compare (only MMMYYYY) portion.&lt;/P&gt;
&lt;P&gt;If date_have is 4 character long (YYYY) than need to compare date_have with date_compare (only YYYY) portion.&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;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;all your observations would need to be removed, as complete dates do not match exactly, and the partial dates do also not match with their counterparts.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Mar 2022 09:50:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/convert-character-field-to-date-and-compare/m-p/804996#M317068</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-03-30T09:50:46Z</dc:date>
    </item>
    <item>
      <title>Re: convert character field to date and compare</title>
      <link>https://communities.sas.com/t5/SAS-Programming/convert-character-field-to-date-and-compare/m-p/805014#M317077</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data TEST;
   input date_have :$20. date_compare : $20.;
   datalines;
00JAN2020 10NOV2020
UNJAN2020 05DEC2020
UNUNK2020 16DEC2019
UNJUN2020 14FEB2021
01JAN2020 01MAY2020
20JUN2020 01JAN2020
;
run;
DATA TEST1;
SET TEST;
/* DELETE 00, UN, UNK BY USING PRXCHANGE FUNCTION */
NEW_have=prxchange('s/^UN|^00|UNK//i',-1,date_have);

if length(NEW_have)=9 then do;
 base=input(strip(NEW_have),date9.);
 compare=input(strip(date_compare),date9.);
end;

if length(NEW_have)=7 then do;
 base=input(strip(NEW_have),monyy7.);
 compare=input(strip(substr(date_compare,3)),monyy7.);
end;

if length(NEW_have)=4 then do;
 base=input(strip(NEW_have),best.);
 compare=input(strip(substr(date_compare,6)),best.);
end;

flag=(base&amp;lt;=compare) ;

drop base compare;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 30 Mar 2022 11:59:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/convert-character-field-to-date-and-compare/m-p/805014#M317077</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-03-30T11:59:42Z</dc:date>
    </item>
  </channel>
</rss>

