Dear community,
I am trying to adapt the date conversion function [1] to use regular expressions:
proc fcmp outlib=work.functions.conversions; /*[1]*/
function datecdash2sdtm(indate $) $; /*[2]*/
length outdate $10; /*[3]*/
cdash_date_regex = prxparse("/(UN|\d{1,2}) (\w{3}) (\d{4})/");
/* if indate ne ' ' then*/
if prxmatch(cdash_date_regex, indate) then
do;
yyyy = prxposn(cdash_date_regex, 3, indate);
mmm = prxposn(cdash_date_regex, 2, indate);
dd = prxposn(cdash_date_regex, 1, indate);
/*yyyy = substr(indate, 8, 4); *//*[4]*/
/* mmm = upcase(substr(indate, 4, 3));*/
/* dd = substr(indate, 1, 2);*/
/* if year not missing */
if notdigit(yyyy) = 0 then
do;
/*[5]*/
mm = put(mmm, $month.); /*[6]*/
/* if month not missing */
if mm ne ' ' then
do;
/* if day not missing */
if notdigit(dd) = 0 then
do;
/*[5]*/
outdate = yyyy || '-' || strip(mm) || '-' || dd; /*[7]*/
end; /* if notdigit(dd) = 0 */
else outdate = yyyy || '-' || strip(mm);
end; /* if mm ne ' ' */
else outdate = yyyy;
end; /* if notdigit(yyyy) = 0 */
else outdate = ' ';
end; /* if indate ne ' ' */
else outdate = ' ';
return(outdate); /*[8]*/
endsub; /*[9]*/
run;
Unfortunately the function is not working and produces empty output. If I try to just parse the dates in plain data step
data mh_crf;
set ecrf.mh1b ecrf.mh1a;
MHSTDTC = datecdash2sdtm(MHTRSTDAT);
MHENDTC = datecdash2sdtm(MHTRENDAT);
cdash_date_regex = prxparse("/(UN|\d{1,2}) (\w{3}) (\d{4})/");
if prxmatch(cdash_date_regex, MHTRENDAT) then
do;
yyyy = prxposn(cdash_date_regex, 3, MHTRENDAT);
mmm = prxposn(cdash_date_regex, 2, MHTRENDAT);
dd = prxposn(cdash_date_regex, 1, MHTRENDAT);
end;
run;
then yyyy, mmm and dd variables are populated as expected. Are there any particular things one needs to keep in mind when mixing proc fcmp with regular expressions? Thank you!
[1] https://www.lexjansen.com/pharmasug/2017/TT/PharmaSUG-2017-TT07.pdf
It turns out that spaces were causing notdigit conditions to no longer be satisfied. Here is the working code:
proc fcmp outlib=work.functions.conversions; /*[1]*/
function datecdash2sdtm(indate $) $; /*[2]*/
length outdate $10; /*[3]*/
cdash_date_regex = prxparse("/(UN|\d{1,2}) (\w{3}) (\d{4})/");
if prxmatch(cdash_date_regex, indate) then
do;
yyyy = prxposn(cdash_date_regex, 3, indate); /*[4]*/
mmm = prxposn(cdash_date_regex, 2, indate);
dd = prxposn(cdash_date_regex, 1, indate);
/* if year not missing */
if notdigit(strip(yyyy)) = 0 then
do;
/*[5]*/
mm = put(mmm, $month.); /*[6]*/
/* if month not missing */
if mm ne ' ' then
do;
/* if day not missing */
if notdigit(strip(dd)) = 0 then
do;
dd = put(input(dd, 2.), z2.);
/*[5]*/
outdate = catx("-", yyyy, mm, dd); /*[7]*/
end; /* if notdigit(dd) = 0 */
else outdate = catx("-", yyyy, mm);
end; /* if mm ne ' ' */
else outdate = strip(yyyy);
end; /* if notdigit(yyyy) = 0 */
else outdate = ' ';
end; /* if indate ne ' ' */
else outdate = ' ';
return(outdate); /*[8]*/
endsub; /*[9]*/
run;
It turns out that spaces were causing notdigit conditions to no longer be satisfied. Here is the working code:
proc fcmp outlib=work.functions.conversions; /*[1]*/
function datecdash2sdtm(indate $) $; /*[2]*/
length outdate $10; /*[3]*/
cdash_date_regex = prxparse("/(UN|\d{1,2}) (\w{3}) (\d{4})/");
if prxmatch(cdash_date_regex, indate) then
do;
yyyy = prxposn(cdash_date_regex, 3, indate); /*[4]*/
mmm = prxposn(cdash_date_regex, 2, indate);
dd = prxposn(cdash_date_regex, 1, indate);
/* if year not missing */
if notdigit(strip(yyyy)) = 0 then
do;
/*[5]*/
mm = put(mmm, $month.); /*[6]*/
/* if month not missing */
if mm ne ' ' then
do;
/* if day not missing */
if notdigit(strip(dd)) = 0 then
do;
dd = put(input(dd, 2.), z2.);
/*[5]*/
outdate = catx("-", yyyy, mm, dd); /*[7]*/
end; /* if notdigit(dd) = 0 */
else outdate = catx("-", yyyy, mm);
end; /* if mm ne ' ' */
else outdate = strip(yyyy);
end; /* if notdigit(yyyy) = 0 */
else outdate = ' ';
end; /* if indate ne ' ' */
else outdate = ' ';
return(outdate); /*[8]*/
endsub; /*[9]*/
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.