Here is a commented version of the code:
data test;
char = "this_is_a_example_with_date_20230331";
/*
The first parameter of the prxmatch function is a
Perl regular expression. It is interpreted as follows
Find a substring that:
starts with 19 or 20, followed by two digits, followed by 0 or 1,
followed by a digit, followed by 0, 1, 2, or 3, followed by a digit.
If found, return the position of the beginning the matching substring.
*/
pos = prxmatch("/(19|20)\d\d[01]\d[0123]\d/o", char);
/*
If a match is found, extract the substring, read it as a SAS date with
informat YYMMDD8.
*/
if pos > 0 then validDate = input(substr(char,pos,8),yymmdd8.);
/*
When displaying the SAS date, use the format YYMMDD10.
*/
format validDate yymmdd10.;
drop pos;
run;
Perl regular expression syntax is described here.
To display the date as year and month, use format YYMMD.
To display the date as year only, use format YEAR4.
Note that the period is part of the format name.
hth
... View more