<?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: Clean messy date variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Clean-messy-date-variables/m-p/740233#M231183</link>
    <description>&lt;P&gt;Hi guys. I really appreciated that you guys are trying to help me with this problem. It's quite messy but it's what I have to deal with. When I posted this question, I didn't realize I provided less information than my dataset had given to me. Anyways, I wrote some code that worked fine for me. For anyone who's interested, that's what I used:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;reformat_date = input(compress(date, '.'), anydtdte13.);&lt;/P&gt;&lt;P&gt;reformat_date_2 = input(date, yymmdd10,);&lt;/P&gt;&lt;P&gt;format reformat_date reformat_date_2 date9.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It didn't convert all dates but it did most of the work. I did other modifications too.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 10 May 2021 17:24:33 GMT</pubDate>
    <dc:creator>di_niu0</dc:creator>
    <dc:date>2021-05-10T17:24:33Z</dc:date>
    <item>
      <title>Clean messy date variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Clean-messy-date-variables/m-p/739991#M231066</link>
      <description>&lt;P&gt;Hello All,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have been cleaning a dataset with a very messy date variable. Having trouble in converting the variable into a uniform date format. Here is what I have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; date&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SEPT01/2001&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9-Apr-10&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20-JAN-18&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Jan 7-2010&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;oct1/2018&lt;/P&gt;&lt;P&gt;6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;NOV18 2010&lt;/P&gt;&lt;P&gt;7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;FEB. 2, 2016&lt;/P&gt;&lt;P&gt;8&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;6/1/2010&lt;/P&gt;&lt;P&gt;9&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2013/02/01 lost to fup&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks!&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;&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>Sat, 08 May 2021 19:17:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Clean-messy-date-variables/m-p/739991#M231066</guid>
      <dc:creator>di_niu0</dc:creator>
      <dc:date>2021-05-08T19:17:07Z</dc:date>
    </item>
    <item>
      <title>Re: Clean messy date variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Clean-messy-date-variables/m-p/740018#M231084</link>
      <description>&lt;P&gt;Is 6/1/2010&amp;nbsp; - 6 Jan or 1 Jun?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is 2013/02/01 - 1 Feb or 2 Jan?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let me guess this has come from a spreadsheet which will accept any old rubbish...&lt;/P&gt;</description>
      <pubDate>Sun, 09 May 2021 00:44:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Clean-messy-date-variables/m-p/740018#M231084</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2021-05-09T00:44:05Z</dc:date>
    </item>
    <item>
      <title>Re: Clean messy date variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Clean-messy-date-variables/m-p/740024#M231089</link>
      <description>&lt;P&gt;I guess you will have to go through some "trial and error" process to read the data into SAS as desired.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below some approach you could take. Any string that you can't convert into a SAS Date value will end up in table investigate. You then need to further add to your code or fix the data directly in your source.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines dsd dlm='|' truncover;
  input id string $30.;
  datalines;
1|SEPT01/2001
2|9-Apr-10
3|20-JAN-18
4|Jan 7-2010
5|oct1/2018
6|NOV18 2010
7|FEB. 2, 2016
8|6/1/2010
9|2013/02/01 lost to fup
;

%let sv_datestye=%sysfunc(getoption(datestyle,,keyexpand));
options datestyle=mdy;

data want investigate;
  set have;
  length sas_date 8;
  format sas_date date9.;

  if _n_=0 then _string2=string;

  if missing(string) then 
    do;
      output want;
      return;
    end;

  sas_date=input(string, ?? anydtdte.);
  if not missing(sas_date) then
    do;
      output want;
      return;
    end;

  _string2=prxchange('s/^([a-z]+)(\d+)[^\d](\d+)/\2\1\3/oi',-1,strip(string));
  sas_date=input(compress(_string2), ?? anydtdte.);
  if not missing(sas_date) then
    do;
      output want;
      return;
    end;

  /*** and here more string manipulations until no rows remain in Investigate ***/

  else output investigate;

run;

options &amp;amp;sv_datestye;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 09 May 2021 01:58:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Clean-messy-date-variables/m-p/740024#M231089</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-05-09T01:58:51Z</dc:date>
    </item>
    <item>
      <title>Re: Clean messy date variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Clean-messy-date-variables/m-p/740105#M231129</link>
      <description>&lt;P&gt;Your task is logically impossible.&lt;/P&gt;
&lt;P&gt;At least a date like this&lt;/P&gt;
&lt;PRE&gt;20-JAN-18&lt;/PRE&gt;
&lt;P&gt;is ambiguous (could be 2018-01-20 or 2020-01-18)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;lt;RANT&amp;gt;Anybody who still uses 2-digit years is an idiot with terminal brain damage. Such poor creatures have no place near a computer except under adult supervision.&amp;lt;/RANT&amp;gt;&lt;/P&gt;</description>
      <pubDate>Mon, 10 May 2021 07:14:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Clean-messy-date-variables/m-p/740105#M231129</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-10T07:14:28Z</dc:date>
    </item>
    <item>
      <title>Re: Clean messy date variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Clean-messy-date-variables/m-p/740132#M231138</link>
      <description>&lt;P&gt;Summing up what others already said: 4 of 9 strings can't translated into dates, because the available information is not sufficient to decide which number is day and which is year (IDs 2, 3) or day and month (IDs 8, 9). So either you have the knowledge, so that it can be coded, or the mission will fail.&lt;/P&gt;</description>
      <pubDate>Mon, 10 May 2021 10:55:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Clean-messy-date-variables/m-p/740132#M231138</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-05-10T10:55:03Z</dc:date>
    </item>
    <item>
      <title>Re: Clean messy date variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Clean-messy-date-variables/m-p/740230#M231181</link>
      <description>I looked at other values. It should be June 1, 2010.</description>
      <pubDate>Mon, 10 May 2021 17:15:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Clean-messy-date-variables/m-p/740230#M231181</guid>
      <dc:creator>di_niu0</dc:creator>
      <dc:date>2021-05-10T17:15:22Z</dc:date>
    </item>
    <item>
      <title>Re: Clean messy date variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Clean-messy-date-variables/m-p/740231#M231182</link>
      <description>That's what I get. It's quit difficult to interpret the exact time. However, when I look at other values in the dataset, it should be interpreted as Jan 20, 2018.</description>
      <pubDate>Mon, 10 May 2021 17:17:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Clean-messy-date-variables/m-p/740231#M231182</guid>
      <dc:creator>di_niu0</dc:creator>
      <dc:date>2021-05-10T17:17:01Z</dc:date>
    </item>
    <item>
      <title>Re: Clean messy date variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Clean-messy-date-variables/m-p/740233#M231183</link>
      <description>&lt;P&gt;Hi guys. I really appreciated that you guys are trying to help me with this problem. It's quite messy but it's what I have to deal with. When I posted this question, I didn't realize I provided less information than my dataset had given to me. Anyways, I wrote some code that worked fine for me. For anyone who's interested, that's what I used:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;reformat_date = input(compress(date, '.'), anydtdte13.);&lt;/P&gt;&lt;P&gt;reformat_date_2 = input(date, yymmdd10,);&lt;/P&gt;&lt;P&gt;format reformat_date reformat_date_2 date9.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It didn't convert all dates but it did most of the work. I did other modifications too.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 May 2021 17:24:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Clean-messy-date-variables/m-p/740233#M231183</guid>
      <dc:creator>di_niu0</dc:creator>
      <dc:date>2021-05-10T17:24:33Z</dc:date>
    </item>
  </channel>
</rss>

