From: Ya Huang on
I test this, and it seems to work:

data _null_;
a='25MAY2OO6';
d=input(a,date.);
c=translate(a,'----------
****************************************************','0123456789ABCDEFGHIJK
LMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz');
put a d date. ' ' c;
if c in ('--***----','--***--') and ^missing(input(a,date.)) then
put 'valid'; else put 'invalid';
run;

25MAY2OO6 25MAY02 --***-**-
invalid


On Tue, 16 Jan 2007 12:15:59 -0500, Ya Huang <ya.huang(a)AMYLIN.COM> wrote:

>Hi there,
>
>I used to test if a character date is valid by testing with
>a input function. If after input function I get a nonmissing value,
>then the character date is valid. Until I saw a post by Paul Dorfman, a
>while ago, showing that this is very dangerous:
>
>data _null_;
>a='25MAY2OO6'; /* note here is OO, not 00 */
>d=input(a,date.);
>put a d date.;
>run;
>
>25MAY2OO6 25MAY02
>
>Now I'm wondering what else I can do to test if a character date
>is valid without V9 funcy function, since I'm still using v8.12.
>Another assumption is that character date is like DDMMMYYYY.
>
>Thanks
>
>Ya
From: "data _null_;" on
This "old style" regex will verify the date is DDMMMYYYY but not if it
is a valid SAS date. Perhaps a combination of this with INPUT DATE9
will suffice.

84 data _null_;
85 rx = rxparse('$d$d
(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec) $d$d$d$d');

86
87 do a='25MAY2OO6','25MAY2006','25APR2OO6','29FEB2003';
88 valid = rxmatch(rx,trim(a));
89 d=input(a,?date9.);
90 put _all_;
91 format d date.;
92 end;
93 call rxfree(rx);
94 run;

rx=1 a=25MAY2OO6 valid=0 d=25MAY02 _ERROR_=0 _N_=1
rx=1 a=25MAY2006 valid=1 d=25MAY06 _ERROR_=0 _N_=1
rx=1 a=25APR2OO6 valid=0 d=25APR02 _ERROR_=0 _N_=1
rx=1 a=29FEB2003 valid=1 d=. _ERROR_=1 _N_=1
rx=. a=29FEB2003 valid=1 d=. _ERROR_=1 _N_=1


On 1/16/07, Ya Huang <ya.huang(a)amylin.com> wrote:
> Hi there,
>
> I used to test if a character date is valid by testing with
> a input function. If after input function I get a nonmissing value,
> then the character date is valid. Until I saw a post by Paul Dorfman, a
> while ago, showing that this is very dangerous:
>
> data _null_;
> a='25MAY2OO6'; /* note here is OO, not 00 */
> d=input(a,date.);
> put a d date.;
> run;
>
> 25MAY2OO6 25MAY02
>
> Now I'm wondering what else I can do to test if a character date
> is valid without V9 funcy function, since I'm still using v8.12.
> Another assumption is that character date is like DDMMMYYYY.
>
> Thanks
>
> Ya
>
From: "Huang, Ya" on
Thanks data _null_! I know someone will post a regex solution. I was
waiting for it.
It's a shame after promising myself to learn it so many times, I still
haven't started it.


-----Original Message-----
From: data _null_; [mailto:datanull(a)gmail.com]
Sent: Tuesday, January 16, 2007 9:39 AM
To: Huang, Ya
Cc: SAS-L(a)listserv.uga.edu
Subject: Re: Safe way to test if a date is valid ?

This "old style" regex will verify the date is DDMMMYYYY but not if it
is a valid SAS date. Perhaps a combination of this with INPUT DATE9
will suffice.

84 data _null_;
85 rx = rxparse('$d$d
(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec) $d$d$d$d');

86
87 do a='25MAY2OO6','25MAY2006','25APR2OO6','29FEB2003';
88 valid = rxmatch(rx,trim(a));
89 d=input(a,?date9.);
90 put _all_;
91 format d date.;
92 end;
93 call rxfree(rx);
94 run;

rx=1 a=25MAY2OO6 valid=0 d=25MAY02 _ERROR_=0 _N_=1
rx=1 a=25MAY2006 valid=1 d=25MAY06 _ERROR_=0 _N_=1
rx=1 a=25APR2OO6 valid=0 d=25APR02 _ERROR_=0 _N_=1
rx=1 a=29FEB2003 valid=1 d=. _ERROR_=1 _N_=1 rx=. a=29FEB2003 valid=1
d=. _ERROR_=1 _N_=1


On 1/16/07, Ya Huang <ya.huang(a)amylin.com> wrote:
> Hi there,
>
> I used to test if a character date is valid by testing with a input
> function. If after input function I get a nonmissing value, then the
> character date is valid. Until I saw a post by Paul Dorfman, a while
> ago, showing that this is very dangerous:
>
> data _null_;
> a='25MAY2OO6'; /* note here is OO, not 00 */ d=input(a,date.); put a d

> date.; run;
>
> 25MAY2OO6 25MAY02
>
> Now I'm wondering what else I can do to test if a character date is
> valid without V9 funcy function, since I'm still using v8.12.
> Another assumption is that character date is like DDMMMYYYY.
>
> Thanks
>
> Ya
>
From: "data _null_;" on
If you want to allow one digit days. Like 9feb2006 change regex as

rx = rxparse('$d[$d] (jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)
$d$d$d$d');

Notice the [$d]. Square brackets for "optional" digit.

You may want to wait for version 9 and learn the PRX functions. so as
not upset anyone. I am often amused by the disdain for RX as
implemented in V8.

Also, in your real program RETAIN the value returned by RXPARSE and
just execute it one time to be efficient.

On 1/16/07, Huang, Ya <Ya.Huang(a)amylin.com> wrote:
> Thanks data _null_! I know someone will post a regex solution. I was
> waiting for it.
> It's a shame after promising myself to learn it so many times, I still
> haven't started it.
>
>
> -----Original Message-----
> From: data _null_; [mailto:datanull(a)gmail.com]
> Sent: Tuesday, January 16, 2007 9:39 AM
> To: Huang, Ya
> Cc: SAS-L(a)listserv.uga.edu
> Subject: Re: Safe way to test if a date is valid ?
>
> This "old style" regex will verify the date is DDMMMYYYY but not if it
> is a valid SAS date. Perhaps a combination of this with INPUT DATE9
> will suffice.
>
> 84 data _null_;
> 85 rx = rxparse('$d$d
> (jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec) $d$d$d$d');
>
> 86
> 87 do a='25MAY2OO6','25MAY2006','25APR2OO6','29FEB2003';
> 88 valid = rxmatch(rx,trim(a));
> 89 d=input(a,?date9.);
> 90 put _all_;
> 91 format d date.;
> 92 end;
> 93 call rxfree(rx);
> 94 run;
>
> rx=1 a=25MAY2OO6 valid=0 d=25MAY02 _ERROR_=0 _N_=1
> rx=1 a=25MAY2006 valid=1 d=25MAY06 _ERROR_=0 _N_=1
> rx=1 a=25APR2OO6 valid=0 d=25APR02 _ERROR_=0 _N_=1
> rx=1 a=29FEB2003 valid=1 d=. _ERROR_=1 _N_=1 rx=. a=29FEB2003 valid=1
> d=. _ERROR_=1 _N_=1
>
>
> On 1/16/07, Ya Huang <ya.huang(a)amylin.com> wrote:
> > Hi there,
> >
> > I used to test if a character date is valid by testing with a input
> > function. If after input function I get a nonmissing value, then the
> > character date is valid. Until I saw a post by Paul Dorfman, a while
> > ago, showing that this is very dangerous:
> >
> > data _null_;
> > a='25MAY2OO6'; /* note here is OO, not 00 */ d=input(a,date.); put a d
>
> > date.; run;
> >
> > 25MAY2OO6 25MAY02
> >
> > Now I'm wondering what else I can do to test if a character date is
> > valid without V9 funcy function, since I'm still using v8.12.
> > Another assumption is that character date is like DDMMMYYYY.
> >
> > Thanks
> >
> > Ya
> >
>
From: NordlDJ on
> -----Original Message-----
> From: SAS(r) Discussion [mailto:SAS-L(a)LISTSERV.UGA.EDU] On Behalf Of Joyce
> Sent: Tuesday, January 16, 2007 12:05 PM
> To: SAS-L(a)LISTSERV.UGA.EDU
> Subject: Re: Safe way to test if a date is valid ?
>
> data _null_;
> a='25MAY2OO6'; /* note here is OO, not 00 */
>
> d=input(a,date9.);
> if trim(left(a)) ^= put(d, date9.) then put 'ERROR: ' a d date.;
>
> run;
>
>
> On 1/16/07, Ya Huang <ya.huang(a)amylin.com> wrote:
> >
> > Hi there,
> >
> > I used to test if a character date is valid by testing with
> > a input function. If after input function I get a nonmissing value,
> > then the character date is valid. Until I saw a post by Paul Dorfman, a
> > while ago, showing that this is very dangerous:
> >
> > data _null_;
> > a='25MAY2OO6'; /* note here is OO, not 00 */
> > d=input(a,date.);
> > put a d date.;
> > run;
> >
> > 25MAY2OO6 25MAY02
> >
> > Now I'm wondering what else I can do to test if a character date
> > is valid without V9 funcy function, since I'm still using v8.12.
> > Another assumption is that character date is like DDMMMYYYY.
> >
> > Thanks
> >
> > Ya
> >

Joyce,

Your suggestion will incorrectly raise an error when a date like '9MAY2006' (single-digit day value) or '25may2006' (lowercase month) is encountered.

Dan

Daniel J. Nordlund
Research and Data Analysis
Washington State Department of Social and Health Services
Olympia, WA 98504-5204