<?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 How to read date containing unknown characters in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-date-to-character-dates-when-source-date-contain/m-p/303691#M64567</link>
    <description>&lt;P&gt;Dear,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My data contains date variable with the following values. I am using the following the code. I am not able to run the code. Please help. It giving me error message.Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;datex&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;//2011&lt;BR /&gt;00/UNK/UNK&lt;BR /&gt;01/Dec/2014&lt;BR /&gt;02/UNK/2014&lt;BR /&gt;18/jun/2008&lt;BR /&gt;UN/mar/2023&lt;BR /&gt;Un/apr/2013&lt;BR /&gt;uN/sep/2011&lt;/P&gt;&lt;P&gt;un/oct/2009&lt;BR /&gt;Un/UNK/2003&lt;BR /&gt;UN/UNK/2002&lt;BR /&gt;uN/UNK/2015&lt;BR /&gt;Un/UNK/2006&lt;/P&gt;&lt;P&gt;un/jun/2002&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;output needed;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;2011&lt;/P&gt;&lt;P&gt;blank&lt;/P&gt;&lt;P&gt;2014-12-01&lt;BR /&gt;2014--02&lt;BR /&gt;2008-06-18&lt;BR /&gt;2023-03&lt;BR /&gt;2013-04&lt;BR /&gt;2011-09&lt;/P&gt;&lt;P&gt;2009-10&lt;BR /&gt;2003&lt;BR /&gt;2002&lt;BR /&gt;2015&lt;BR /&gt;2006&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;code;(Some one posted this code);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%LET&amp;nbsp;MM =&amp;nbsp;&amp;nbsp;'JAN' ' FEB' 'MAR' 'APR' 'MAY' 'JUN' 'JUL' 'AUG' 'SEP' 'OCT' 'NOV' 'DEC' ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;DATA WANT;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; SET HAVE;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;array mx $ &amp;amp;mm;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;if input(datex, ?? &amp;nbsp;ddmmyy10.) &amp;nbsp;ne . &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; /* valid date given */&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;then date_out = put(input(datex, ddmmyy10.), &amp;nbsp;yymmdd10.);&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;else&lt;/STRONG&gt; do;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; month = scan(date_raw,2);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; year&amp;nbsp;= scan(date_raw,3);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; if &amp;nbsp; length(trim(month)) = 2 and &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/* 2 digits month */&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 le input(month, best2.) le 12 and&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;input(year, best4.) ge 1960 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/* check for reasonable year */&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; then &lt;STRONG&gt;date_out = catx( '-', year,month); &amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; else if&amp;nbsp; length(trim(month)) = 3 and &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; /* 3 characters month */&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;upcase(month) in (&amp;amp;mm) and&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;input(year, best4.) ge 1960 &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;then &lt;STRONG&gt;do&lt;/STRONG&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;do&lt;/STRONG&gt; i=1 to 12;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if mx(i) = upcase(trim(month)) then &lt;STRONG&gt;do&lt;/STRONG&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&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;month = put(i , z2.); &amp;nbsp; &amp;nbsp; /* convert month mmm to its number, 2 digits */&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&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;&lt;STRONG&gt;date_out = catx( '-', year, trim(month));&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&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; leave;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/STRONG&gt; end;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; end; end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;else if &amp;nbsp;input(year, best4.) ge 1960 &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;then &lt;STRONG&gt;date_out = trim(year);&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;end; &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 11 Oct 2016 01:01:08 GMT</pubDate>
    <dc:creator>knveraraju91</dc:creator>
    <dc:date>2016-10-11T01:01:08Z</dc:date>
    <item>
      <title>How to convert date to character dates when source date contain unknown characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-date-to-character-dates-when-source-date-contain/m-p/303622#M64527</link>
      <description>&lt;P&gt;Dear,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My data contains date variable with the following values. I am using the following code to get the output. I got the output except for a few OBS containing 'jun' as the month because I am trying to remove 'un' charcters using tranwrd function which removes 'un' in 'jun' month. Please help me simply the code. Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;date&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;//2011&lt;BR /&gt;00/UNK/UNK&lt;BR /&gt;01/Dec/2014&lt;BR /&gt;02/UNK/2014&lt;BR /&gt;18/jun/2008&lt;BR /&gt;UN/mar/2023&lt;BR /&gt;Un/apr/2013&lt;BR /&gt;uN/sep/2011&lt;/P&gt;&lt;P&gt;un/oct/2009&lt;BR /&gt;Un/UNK/2003&lt;BR /&gt;UN/UNK/2002&lt;BR /&gt;uN/UNK/2015&lt;BR /&gt;Un/UNK/2006&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;code;&lt;/P&gt;&lt;P&gt;date1=tranwrd(date_raw,'/','');&lt;BR /&gt;date2=tranwrd(date1,'UNK','');&lt;BR /&gt;date3=tranwrd(date2,'un','');&lt;BR /&gt;date4=tranwrd(date3,'UN','');&lt;BR /&gt;date5=tranwrd(date4,'Un','');&lt;BR /&gt;date6=compress(tranwrd(date5,'uN',''));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if length(date6)=9 then&amp;nbsp;STDTC2= put(input(date6,date9.),is8601da.);&lt;/P&gt;&lt;P&gt;if length(date6)=7 then STDTC3= put(input(date6,monyy7.),yymmd7.);&lt;BR /&gt;if length(date6)=4 then STDTC4= put(input(date6,best.),10.);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if stdtc2 ^='' then isdate=stdtc2;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;else if stdtc3 ^='' then isdate=stdtc3;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;else if stdtc4 ^='' then isdate=stdtc4;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;output needed;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;2011&lt;/P&gt;&lt;P&gt;blank&lt;/P&gt;&lt;P&gt;2014-12-01&lt;BR /&gt;2014--02&lt;BR /&gt;2008-06-18&lt;BR /&gt;2023-03&lt;BR /&gt;2013-04&lt;BR /&gt;2011-09&lt;/P&gt;&lt;P&gt;2009-10&lt;BR /&gt;2003&lt;BR /&gt;2002&lt;BR /&gt;2015&lt;BR /&gt;2006&lt;/P&gt;</description>
      <pubDate>Mon, 10 Oct 2016 17:46:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-date-to-character-dates-when-source-date-contain/m-p/303622#M64527</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2016-10-10T17:46:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert date to character dates when source date contain unknown characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-date-to-character-dates-when-source-date-contain/m-p/303631#M64533</link>
      <description>&lt;P&gt;Add to your code at the beginning:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;if input(datex, ?? &amp;nbsp;ddmmyy10.) &amp;nbsp;ne .&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;then date_out = put(input(datex, ddmmyy10.), &amp;nbsp;yymmdd10.);&lt;/P&gt;&lt;P&gt;else do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;... your code ...&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The only "problem" that may arouse is that date_out will be in dormat of: yyyy/mm/dd instead yyyy-mm-dd&lt;/P&gt;&lt;P&gt;and that can be corrected by date_out = translate(date_out, '-' , '/' );&lt;/P&gt;</description>
      <pubDate>Mon, 10 Oct 2016 18:34:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-date-to-character-dates-when-source-date-contain/m-p/303631#M64533</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-10-10T18:34:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert date to character dates when source date contain unknown characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-date-to-character-dates-when-source-date-contain/m-p/303652#M64544</link>
      <description>&lt;P&gt;Thank you for help. If I use my code where you mentioned to use. Does it work for values 'Un/jun/2012' . My code removes 'un' in jun. Please help. &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Oct 2016 20:14:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-date-to-character-dates-when-source-date-contain/m-p/303652#M64544</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2016-10-10T20:14:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert date to character dates when source date contain unknown characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-date-to-character-dates-when-source-date-contain/m-p/303672#M64550</link>
      <description>&lt;P&gt;The expression&amp;nbsp;&lt;STRONG&gt; input(datex, ?? &amp;nbsp;ddmmyy10.) &lt;/STRONG&gt;checks for valid date and&lt;/P&gt;&lt;P&gt;assigns a number that is the days passed since 01-01-1960 otherwise it&lt;/P&gt;&lt;P&gt;assingns a missing value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As&amp;nbsp;&lt;SPAN&gt; 'Un/jun/2012' is not a valid date, it will execute the &lt;STRONG&gt;ELSE&lt;/STRONG&gt; part of the &lt;STRONG&gt;IF&lt;/STRONG&gt; statement.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;In oredr to overcome &lt;STRONG&gt;JUN&lt;/STRONG&gt; month, I would prefer a differnt code than yours assuming that&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;date_raw is always in a format of day/month/year .&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I havn't run this code. You may try it:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%LET&amp;nbsp;MM =&amp;nbsp;&amp;nbsp;'JAN' ' FEB' 'MAR' 'APR' 'MAY' 'JUN' 'JUL' 'AUG' 'SEP' 'OCT' 'NOV' 'DEC' ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;DATA WANT; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; SET HAVE;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;array mx $ &amp;amp;mm;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;if input(datex, ?? &amp;nbsp;ddmmyy10.) &amp;nbsp;ne . &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; /* valid date given */&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;then date_out = put(input(datex, ddmmyy10.), &amp;nbsp;yymmdd10.);&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;else&lt;/STRONG&gt; do;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; month = scan(date_raw,2);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; year&amp;nbsp;= scan(date_raw,3);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; if &amp;nbsp; length(trim(month)) = 2 and &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/* 2 digits month */&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 le input(month, best2.) le 12 and &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;input(year, best4.) ge 1960 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/* check for reasonable year */&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; then &lt;STRONG&gt;date_out = catx( '-', year,month); &amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; else if&amp;nbsp; length(trim(month)) = 3 and &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; /* 3 characters month */&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;upcase(month) in (&amp;amp;mm) and&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;input(year, best4.) ge 1960 &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;then &lt;STRONG&gt;do&lt;/STRONG&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;do&lt;/STRONG&gt; i=1 to 12;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if mx(i) = upcase(trim(month)) then &lt;STRONG&gt;do&lt;/STRONG&gt;; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&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;month = put(i , z2.); &amp;nbsp; &amp;nbsp; /* convert month mmm to its number, 2 digits */&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&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;&lt;STRONG&gt;date_out = catx( '-', year, trim(month));&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&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; leave;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/STRONG&gt; end;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; end; end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;else if &amp;nbsp;input(year, best4.) ge 1960 &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;then &lt;STRONG&gt;date_out = trim(year);&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;end; &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;run;&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Oct 2016 21:25:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-date-to-character-dates-when-source-date-contain/m-p/303672#M64550</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-10-10T21:25:05Z</dc:date>
    </item>
    <item>
      <title>How to read date containing unknown characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-date-to-character-dates-when-source-date-contain/m-p/303691#M64567</link>
      <description>&lt;P&gt;Dear,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My data contains date variable with the following values. I am using the following the code. I am not able to run the code. Please help. It giving me error message.Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;datex&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;//2011&lt;BR /&gt;00/UNK/UNK&lt;BR /&gt;01/Dec/2014&lt;BR /&gt;02/UNK/2014&lt;BR /&gt;18/jun/2008&lt;BR /&gt;UN/mar/2023&lt;BR /&gt;Un/apr/2013&lt;BR /&gt;uN/sep/2011&lt;/P&gt;&lt;P&gt;un/oct/2009&lt;BR /&gt;Un/UNK/2003&lt;BR /&gt;UN/UNK/2002&lt;BR /&gt;uN/UNK/2015&lt;BR /&gt;Un/UNK/2006&lt;/P&gt;&lt;P&gt;un/jun/2002&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;output needed;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;2011&lt;/P&gt;&lt;P&gt;blank&lt;/P&gt;&lt;P&gt;2014-12-01&lt;BR /&gt;2014--02&lt;BR /&gt;2008-06-18&lt;BR /&gt;2023-03&lt;BR /&gt;2013-04&lt;BR /&gt;2011-09&lt;/P&gt;&lt;P&gt;2009-10&lt;BR /&gt;2003&lt;BR /&gt;2002&lt;BR /&gt;2015&lt;BR /&gt;2006&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;code;(Some one posted this code);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%LET&amp;nbsp;MM =&amp;nbsp;&amp;nbsp;'JAN' ' FEB' 'MAR' 'APR' 'MAY' 'JUN' 'JUL' 'AUG' 'SEP' 'OCT' 'NOV' 'DEC' ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;DATA WANT;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; SET HAVE;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;array mx $ &amp;amp;mm;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;if input(datex, ?? &amp;nbsp;ddmmyy10.) &amp;nbsp;ne . &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; /* valid date given */&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;then date_out = put(input(datex, ddmmyy10.), &amp;nbsp;yymmdd10.);&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;else&lt;/STRONG&gt; do;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; month = scan(date_raw,2);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; year&amp;nbsp;= scan(date_raw,3);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; if &amp;nbsp; length(trim(month)) = 2 and &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/* 2 digits month */&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 le input(month, best2.) le 12 and&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;input(year, best4.) ge 1960 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/* check for reasonable year */&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; then &lt;STRONG&gt;date_out = catx( '-', year,month); &amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; else if&amp;nbsp; length(trim(month)) = 3 and &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; /* 3 characters month */&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;upcase(month) in (&amp;amp;mm) and&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;input(year, best4.) ge 1960 &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;then &lt;STRONG&gt;do&lt;/STRONG&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;do&lt;/STRONG&gt; i=1 to 12;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if mx(i) = upcase(trim(month)) then &lt;STRONG&gt;do&lt;/STRONG&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&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;month = put(i , z2.); &amp;nbsp; &amp;nbsp; /* convert month mmm to its number, 2 digits */&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&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;&lt;STRONG&gt;date_out = catx( '-', year, trim(month));&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&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; leave;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/STRONG&gt; end;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; end; end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;else if &amp;nbsp;input(year, best4.) ge 1960 &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;then &lt;STRONG&gt;date_out = trim(year);&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;end; &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Oct 2016 01:01:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-date-to-character-dates-when-source-date-contain/m-p/303691#M64567</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2016-10-11T01:01:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert date to character dates when source date contain unknown characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-date-to-character-dates-when-source-date-contain/m-p/303709#M64563</link>
      <description>&lt;P&gt;The mdy() function is quite helpful here. See code below.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines truncover;
input have_str $11.;
datalines;
//2011
00/UNK/UNK
01/Dec/2014
02/UNK/2014
18/jun/2008
UN/mar/2023
Un/apr/2013
uN/sep/2011
un/oct/2009
Un/UNK/2003
UN/UNK/2002
uN/UNK/2015
Un/UNK/2006
  
Un/##/2006
;
run;

options missing=' ';
data want;
  set have;
  length want_str $11.;

  /* valid day, month and year? */
  want_str=put(input(have_str, ?? date11.),yymmdd10.);

  if missing(want_str) and not missing(have_str) then
    do;
      length _day $2 _month $3 _year $4;
      _day  =scan(have_str,1,'/','m');
      _month=put(month(input(scan(have_str,2,'/','m')||'2000', ?? monyy.)),z2.);
      _year =scan(have_str,3,'/','m');

      /* valid day and year? */
      if mdy('01',_day,_year) then want_str=_year||'--'||_day;

      /* valid month and year? */
      else if mdy(_month,'01',_year) then want_str=_year||'-'||_month;

      /* valid year? */
      else if mdy('01','01',_year) then want_str=_year;

    end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Oct 2016 03:57:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-date-to-character-dates-when-source-date-contain/m-p/303709#M64563</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2016-10-11T03:57:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to read date containing unknown characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-date-to-character-dates-when-source-date-contain/m-p/303718#M64568</link>
      <description>&lt;P&gt;Please don't post the same question twice.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here the link to the thread with replies:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/How-to-convert-date-to-character-dates-when-source-date-contain/m-p/303622#U303622&amp;nbsp;" target="_blank"&gt;https://communities.sas.com/t5/Base-SAS-Programming/How-to-convert-date-to-character-dates-when-source-date-contain/m-p/303622#U303622&amp;nbsp;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Oct 2016 04:59:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-date-to-character-dates-when-source-date-contain/m-p/303718#M64568</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2016-10-11T04:59:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert date to character dates when source date contain unknown characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-date-to-character-dates-when-source-date-contain/m-p/303762#M64579</link>
      <description>&lt;P&gt;Thank you for the help. I got the output I need. But I am getting warning message as "invalid argument &amp;nbsp;for OBS where _day='un' 'UN' and _year='UNK'. I added a few additional lines to the code. Is there any other option to simplyfy it. Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;code;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;options&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;missing&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;' '&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; want&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  &lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt; have&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  &lt;SPAN class="token function"&gt;length&lt;/SPAN&gt; want_str &lt;SPAN class="token punctuation"&gt;$&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;11&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;

  &lt;SPAN class="token comment"&gt;/* valid day, month and year? */&lt;/SPAN&gt;
  &lt;FONT color="#FF6600"&gt;want_str1&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;put&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;input&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;have_str&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; ?? date11&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;yymmdd10&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/FONT&gt;

  &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;missing&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;want_str&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; and &lt;SPAN class="token operator"&gt;not&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;missing&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;have_str&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt;
    do&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
      &lt;SPAN class="token function"&gt;length&lt;/SPAN&gt; _day &lt;SPAN class="token punctuation"&gt;$&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;2&lt;/SPAN&gt; _month &lt;SPAN class="token punctuation"&gt;$&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;3&lt;/SPAN&gt; _year &lt;SPAN class="token punctuation"&gt;$&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;4&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
      _day  &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;scan&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;have_str&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'/'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'m'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
      _month&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;put&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;month&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;input&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;scan&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;have_str&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;2&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'/'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'m'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;||&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'2000'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; ?? monyy&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;z2&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
      _year &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;scan&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;have_str&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;3&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'/'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'m'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;FONT color="#FF6600"&gt;/* valid day and year? */&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF6600"&gt;if _day not in ('un' 'UN' 'uN' 'Un') and _year ^='UNK' then do;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF6600"&gt;if mdy('01',_day,_year) then want_str2=_year||'--'||_day;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF6600"&gt;end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF6600"&gt;/* valid month and year? */&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF6600"&gt;if _year ^='UNK' then do;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF6600"&gt;if mdy(_month,'01',_year) then want_str3=_year||'-'||_month;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF6600"&gt;end;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF6600"&gt;/* valid year? */&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF6600"&gt;if _year^='UNK' then do;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF6600"&gt;if mdy('01','01',_year) then want_str4=_year;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF6600"&gt;end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#993300"&gt;if&amp;nbsp;&lt;SPAN&gt;want_str1 ^='' then wantstr=want_str1;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#993300"&gt;else if&amp;nbsp;&lt;SPAN&gt;want_str2 ^='' then&amp;nbsp;wantstr=want_str2;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#993300"&gt;else if&amp;nbsp;&lt;SPAN&gt;want_str3 ^='' then&amp;nbsp;wantstr=want_str3;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#993300"&gt;else if&amp;nbsp;&lt;SPAN&gt;want_str4 ^='' then&amp;nbsp;wantstr=want_str4;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Oct 2016 12:33:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-date-to-character-dates-when-source-date-contain/m-p/303762#M64579</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2016-10-11T12:33:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to read date containing unknown characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-date-to-character-dates-when-source-date-contain/m-p/303828#M64594</link>
      <description>&lt;P&gt;After hard work I simplified the code as:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;infile datalines;&lt;BR /&gt;input datex $11. ;&lt;BR /&gt;datalines;&lt;BR /&gt;//2011&lt;BR /&gt;00/UNK/UNK&lt;BR /&gt;01/Dec/2014&lt;BR /&gt;02/UNK/2014&lt;BR /&gt;18/jun/2008&lt;BR /&gt;UN/mar/2023&lt;BR /&gt;Un/apr/2013&lt;BR /&gt;uN/sep/2011&lt;BR /&gt;un/oct/2009&lt;BR /&gt;Un/UNK/2003&lt;BR /&gt;UN/UNK/2002&lt;BR /&gt;uN/UNK/2015&lt;BR /&gt;Un/UNK/2006&lt;BR /&gt;; run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%LET MM = 'JAN' ' FEB' 'MAR' 'APR' 'MAY' 'JUN' 'JUL' 'AUG' 'SEP' 'OCT' 'NOV' 'DEC' ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA WANT(keep=datex date_out day month year ); &amp;nbsp; /*drop unrelevant variables */&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;length date_out $10 day $2 month $3 year $4;&lt;BR /&gt;&amp;nbsp; SET HAVE;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;array mx {12} $ ( &amp;amp;mm );&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; day = scan(datex,1,'/','m');&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; month = scan(datex,2,'/','m');&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; year = scan(datex,3,'/','m');&lt;BR /&gt;&lt;BR /&gt;if length(strip(month)) = 3 and &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/* 3 characters month */&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;upcase(month) in (&amp;amp;mm) and&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;input(year,?? best4.) ge 1960&lt;BR /&gt;then do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; do i=1 to 12;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if mx(i) = upcase(trim(month)) then do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;month = put(i , z2.); &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/* convert month mmm to its number, 2 digits */&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; leave;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; end;&lt;BR /&gt;end; end;&lt;BR /&gt;&lt;BR /&gt;lday &amp;nbsp; &amp;nbsp; = &amp;nbsp;compress(day,'0123456789');&lt;BR /&gt;lmonth = compress(month,'0123456789');&lt;BR /&gt;lyear &amp;nbsp; &amp;nbsp;= compress(year,'0123456789');&lt;BR /&gt;&lt;BR /&gt;if lday=' ' and lmonth=' ' and lyear=' '&lt;BR /&gt;then date_out = catx('-',year,month); else&lt;BR /&gt;if lday=' ' and lyear=' ' and lmonth ne ' '&lt;BR /&gt;then date_out = cat(year,'--',day); else&lt;BR /&gt;if lday ne ' ' and lmonth=' ' and lyear=' '&lt;BR /&gt;then date_out = catx('-',year,month); else&lt;BR /&gt;if lday ne ' ' and lmonth ne ' ' and lyear=' '&lt;BR /&gt;then date_out = year; else date_out = ' ';&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Oct 2016 16:52:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-date-to-character-dates-when-source-date-contain/m-p/303828#M64594</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-10-11T16:52:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert date to character dates when source date contain unknown characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-date-to-character-dates-when-source-date-contain/m-p/303903#M64629</link>
      <description>&lt;P&gt;I should have checked the log.... mdy() requires numerical variables. Below a code version which creates a bit less Notes in the log about conversion from character to numerical.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines truncover;
input have_str $11.;
datalines;
//2011
00/UNK/UNK
01/Dec/2014
02/UNK/2014
18/jun/2008
UN/mar/2023
Un/apr/2013
uN/sep/2011
un/oct/2009
Un/uNk/2003
 
Un/##/2006
99/999/2006
;
run;

options missing=' ';
data want;
  set have;
  length want_str $11.;

  /* valid day, month and year? */
  want_str=put(input(have_str, ?? date11.),yymmdd10.);

  if missing(want_str) and not missing(have_str) then
    do;
      length _day _month _year 8;
      _day  =input(scan(have_str,1,'/','m'), ?? best32.);
      _month=month(input(scan(have_str,2,'/','m')||'2000', ?? monyy.));
      _year =input(scan(have_str,3,'/','m'),?? best32.);

      if _day&amp;lt;1 or _day&amp;gt;31 then call missing(_day);
      if _month&amp;lt;1 or _month&amp;gt;12 then call missing(_month);

      /* valid day and year? */
      if mdy(01,_day,_year) then want_str=put(_year,z4.)||'--'||put(_day,z2.);

      /* valid month and year? */
      else if mdy(_month,01,_year) then want_str=put(_year,z4.)||'-'||put(_month,z2.);

      /* valid year? */
      else if mdy(01,01,_year) then want_str=put(_year,z4.);

    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you're using my code version then you don't need to add all these checks like&amp;nbsp;&lt;EM&gt;if _day not in ('un' 'UN' 'uN' 'Un') and _year ^='UNK' then do;&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;The code I've posted works fine without it and _day will either contain a number or a missing.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If the number in _day is a valid day then the mdy() function will return a value (if the 2 other parameters are valid as well), else it will return a missing. So that's the check.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;As you can see this code version doesn't only deal with "UN/UNK" but with any string which doesn't represent a valid day, month or year.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Oct 2016 00:19:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-date-to-character-dates-when-source-date-contain/m-p/303903#M64629</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2016-10-12T00:19:27Z</dc:date>
    </item>
  </channel>
</rss>

