<?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 Extract Date and Date time from string in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extract-Date-and-Date-time-from-string/m-p/883285#M348992</link>
    <description>&lt;P&gt;I want to create 2 new variables. Period and DTS from the following example:&lt;/P&gt;&lt;P&gt;999999999K 20APR23:11:39:00 AM&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Desired results:&lt;/P&gt;&lt;P&gt;Period&lt;/P&gt;&lt;P&gt;20/04/2023&lt;/P&gt;&lt;P&gt;DTS&lt;/P&gt;&lt;P&gt;20Apr2023 11:39:00 am&lt;/P&gt;</description>
    <pubDate>Mon, 03 Jul 2023 05:53:39 GMT</pubDate>
    <dc:creator>Haydo</dc:creator>
    <dc:date>2023-07-03T05:53:39Z</dc:date>
    <item>
      <title>Extract Date and Date time from string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Date-and-Date-time-from-string/m-p/883285#M348992</link>
      <description>&lt;P&gt;I want to create 2 new variables. Period and DTS from the following example:&lt;/P&gt;&lt;P&gt;999999999K 20APR23:11:39:00 AM&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Desired results:&lt;/P&gt;&lt;P&gt;Period&lt;/P&gt;&lt;P&gt;20/04/2023&lt;/P&gt;&lt;P&gt;DTS&lt;/P&gt;&lt;P&gt;20Apr2023 11:39:00 am&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2023 05:53:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Date-and-Date-time-from-string/m-p/883285#M348992</guid>
      <dc:creator>Haydo</dc:creator>
      <dc:date>2023-07-03T05:53:39Z</dc:date>
    </item>
    <item>
      <title>Re: Extract Date and Date time from string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Date-and-Date-time-from-string/m-p/883294#M348994</link>
      <description>&lt;P&gt;It's always hard to generalize when we are presented with only one example of such data. There are many questions: is the date and time always preceded by text and a blank space? Is the date and time always in the form 20APR23:11:39:00 AM ? Is there always a space before the AM or PM?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, for this example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    string='999999999K 20APR23:11:39:00 AM';
run;

data want;
    set have;
    second_word=scan(string,2,' ');
    third_word=scan(string,3,' ');
    date=input(second_word,date7.);
    time=input(cats(substr(second_word,9),third_word),time10.);
    format date date9. time time8.;
    drop second_word third_word;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that does not work for you on your larger data set, please provide more examples that represents any real-world variability in formatting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2023 09:23:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Date-and-Date-time-from-string/m-p/883294#M348994</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-07-03T09:23:35Z</dc:date>
    </item>
    <item>
      <title>Re: Extract Date and Date time from string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Date-and-Date-time-from-string/m-p/883304#M348995</link>
      <description>&lt;P&gt;I would parse the data with a regular expression:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  string='999999999K 20APR23:11:39:00 AM';
run;


data want;
prxid=prxparse('/ ([^:]*):(.*)$/');
set have;
if prxmatch(prxid,string);
period=input(prxposn(prxid,1,string),date.);
dts=dhms(period,0,0,input(prxposn(prxid,2,string),time.));
drop prxid;
format period ddmmyy10. dts datetime23.;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The PRX expression looks for a blank, followed by something that is not a colon (the first capture buffer), followed by a colon (not captured), and then the rest of the string ("$" means end of string) in the second capture buffer. Once you have the parts its a piece of cake - I put the time value in the seconds parameter of DHMS(), as a SAS time value is actually the number of seconds since midnight, instead of using 3 function calls to get the hours, minutes and seconds.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course, if your data is in a flat file, and not in SAS data, use INPUT (replace the CARDS with the actual physical infile):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  input somestring : $20.  period date7. +1 time time11.;
  format period ddmmyy10.;
  dts=dhms(period,0,0,time);
  format dts datetime23.;
cards;
999999999K 20APR23:11:39:00 AM
;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 Jul 2023 10:15:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Date-and-Date-time-from-string/m-p/883304#M348995</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-07-03T10:15:16Z</dc:date>
    </item>
    <item>
      <title>Re: Extract Date and Date time from string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Date-and-Date-time-from-string/m-p/883305#M348996</link>
      <description>&lt;P&gt;Use the SCAN function to dissect the string. Convert the 2nd substring to a datetime value with the DATETIME16. informat, and add 12 hours (12 * 3600 seconds) if the 3rd substring is PM.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2023 10:14:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Date-and-Date-time-from-string/m-p/883305#M348996</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-07-03T10:14:23Z</dc:date>
    </item>
    <item>
      <title>Re: Extract Date and Date time from string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Date-and-Date-time-from-string/m-p/883310#M348997</link>
      <description>&lt;P&gt;Same as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;but using an informat that converts a source string with AM|PM directly to a SAS datetime value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover;
  input str $100.;
  datalines;
999999999K 20APR23:11:39:00 AM
;

data want;
  set have;
  length dttm_string $25.;
  dttm_string=substr(compbl(str),length(scan(str,1,' '))+2);

  format dts dateampm22. period ddmmyys10.;
  dts=input(dttm_string,datetime19.);
  period=datepart(dts);
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1688380518328.png" style="width: 621px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/85531iF5D602570122BC94/image-dimensions/621x59?v=v2" width="621" height="59" role="button" title="Patrick_0-1688380518328.png" alt="Patrick_0-1688380518328.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2023 10:35:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Date-and-Date-time-from-string/m-p/883310#M348997</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-07-03T10:35:29Z</dc:date>
    </item>
  </channel>
</rss>

