<?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: Extract date from a string in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834232#M329835</link>
    <description>This works for some records but not for a record like this:&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;   string = 'TST_NoRecs_BBBGRADE12BEFE20210701_220019.doc';&lt;BR /&gt;   c      = prxchange('s/.*_(\d{8})_.*/$1/', -1, string);&lt;BR /&gt;   date   = input(c, yymmdd8.);&lt;BR /&gt;   format date yymmdd10.;&lt;BR /&gt;run;</description>
    <pubDate>Tue, 20 Sep 2022 08:28:18 GMT</pubDate>
    <dc:creator>Citrine10</dc:creator>
    <dc:date>2022-09-20T08:28:18Z</dc:date>
    <item>
      <title>Extract date from a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834223#M329828</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;
&lt;P&gt;I have a column containing really long test and I am struggling to extract the date portion in the string. The date format is yyyymmdd.&lt;/P&gt;
&lt;P&gt;An example of the string:&lt;/P&gt;
&lt;P&gt;Schoolrecords_class1B_20190809_2209.doc&lt;/P&gt;
&lt;P&gt;The character strings vary and I therefore cannot specify a position of the string.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Sep 2022 07:45:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834223#M329828</guid>
      <dc:creator>Citrine10</dc:creator>
      <dc:date>2022-09-20T07:45:50Z</dc:date>
    </item>
    <item>
      <title>Re: Extract date from a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834226#M329831</link>
      <description>&lt;P&gt;But the format is always 8 digits, correct?&lt;/P&gt;</description>
      <pubDate>Tue, 20 Sep 2022 08:11:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834226#M329831</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-09-20T08:11:22Z</dc:date>
    </item>
    <item>
      <title>Re: Extract date from a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834227#M329832</link>
      <description>correct</description>
      <pubDate>Tue, 20 Sep 2022 08:12:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834227#M329832</guid>
      <dc:creator>Citrine10</dc:creator>
      <dc:date>2022-09-20T08:12:00Z</dc:date>
    </item>
    <item>
      <title>Re: Extract date from a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834228#M329833</link>
      <description>&lt;P&gt;Ok. Try this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   string = 'Schoolrecords_class1B_20190809_2209.doc';
   c      = prxchange('s/.*_(\d{8})_.*/$1/', -1, string);
   date   = input(c, yymmdd8.);
   format date yymmdd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Sep 2022 08:15:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834228#M329833</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-09-20T08:15:30Z</dc:date>
    </item>
    <item>
      <title>Re: Extract date from a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834229#M329834</link>
      <description>&lt;P&gt;With scan function you can pick third "word" from original string.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;Attrib Datevar format=yymmdd10.;&lt;BR /&gt;dateVar = input(Scan(DocName,3,'_'),yymmdd10.);&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Sep 2022 08:16:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834229#M329834</guid>
      <dc:creator>Aku</dc:creator>
      <dc:date>2022-09-20T08:16:34Z</dc:date>
    </item>
    <item>
      <title>Re: Extract date from a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834232#M329835</link>
      <description>This works for some records but not for a record like this:&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;   string = 'TST_NoRecs_BBBGRADE12BEFE20210701_220019.doc';&lt;BR /&gt;   c      = prxchange('s/.*_(\d{8})_.*/$1/', -1, string);&lt;BR /&gt;   date   = input(c, yymmdd8.);&lt;BR /&gt;   format date yymmdd10.;&lt;BR /&gt;run;</description>
      <pubDate>Tue, 20 Sep 2022 08:28:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834232#M329835</guid>
      <dc:creator>Citrine10</dc:creator>
      <dc:date>2022-09-20T08:28:18Z</dc:date>
    </item>
    <item>
      <title>Re: Extract date from a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834234#M329836</link>
      <description>&lt;P&gt;Small change&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   string = 'TST_NoRecs_BBBGRADE12BEFE20210701_220019.doc';
   c      = prxchange('s/.*(\d{8}).*/$1/', -1, string);
   date   = input(c, yymmdd8.);
   format date yymmdd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Sep 2022 08:32:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834234#M329836</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-09-20T08:32:06Z</dc:date>
    </item>
    <item>
      <title>Re: Extract date from a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834237#M329837</link>
      <description>I think we are almost there. Its just a few odd ones. I thought it was the space and used compress however that didn't work:&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;	string = "TST_NoRecs_ BBBGRADE22010508_24-T123456789.doc";&lt;BR /&gt;    c      = compress(prxchange('s/.*(\d{8}).*/$1/', -1, string));&lt;BR /&gt;   date   = input(c, yymmdd8.);&lt;BR /&gt;   format date yymmdd10.;&lt;BR /&gt;run;</description>
      <pubDate>Tue, 20 Sep 2022 09:01:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834237#M329837</guid>
      <dc:creator>Citrine10</dc:creator>
      <dc:date>2022-09-20T09:01:15Z</dc:date>
    </item>
    <item>
      <title>Re: Extract date from a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834242#M329842</link>
      <description>&lt;P&gt;Changed the regular expression, so it doesn't catch 12345678 etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   string = 'TST_NoRecs_ BBBGRADE22010508_24-T123456789.doc';
   c      = prxchange('s/.*([12]\d{3}[01]\d[0123]\d).*/$1/', -1, string);
   date   = input(c, yymmdd8.);
   format date yymmdd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Sep 2022 09:49:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834242#M329842</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-09-20T09:49:09Z</dc:date>
    </item>
    <item>
      <title>Re: Extract date from a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834244#M329844</link>
      <description>Thank you so much, This worked. Appreciate your help.&lt;BR /&gt;&lt;BR /&gt;One last thing, how do I do it in the case where I only want the date after the DDD&lt;BR /&gt;GR1_ClassGr_ DDD1DDD1245620210104_23?&lt;BR /&gt;e.g.  I would need the 2021-01-04 from the above&lt;BR /&gt;This is another dataset where the date needs to be extracted.</description>
      <pubDate>Tue, 20 Sep 2022 10:08:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834244#M329844</guid>
      <dc:creator>Citrine10</dc:creator>
      <dc:date>2022-09-20T10:08:42Z</dc:date>
    </item>
    <item>
      <title>Re: Extract date from a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834245#M329845</link>
      <description>&lt;P&gt;Glad to help &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you describe the exact logic of the string / date you want to extract?&lt;/P&gt;</description>
      <pubDate>Tue, 20 Sep 2022 10:14:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834245#M329845</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-09-20T10:14:07Z</dc:date>
    </item>
    <item>
      <title>Re: Extract date from a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834252#M329848</link>
      <description>for example if the string is: GR1_ClassGr_ DDD1DDD1245620210104_23?&lt;BR /&gt;e.g. I would need the date: 2021-01-04</description>
      <pubDate>Tue, 20 Sep 2022 10:42:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834252#M329848</guid>
      <dc:creator>Citrine10</dc:creator>
      <dc:date>2022-09-20T10:42:43Z</dc:date>
    </item>
    <item>
      <title>Re: Extract date from a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834263#M329852</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input have $80.;
pid=prxparse('/\d{8}/');
call prxsubstr(pid,have,p,l);
if p then want=substr(have,p,l);
drop pid p l;
cards;
Schoolrecords_class1B_20190809_2209.doc
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Sep 2022 11:32:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-date-from-a-string/m-p/834263#M329852</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-09-20T11:32:04Z</dc:date>
    </item>
  </channel>
</rss>

