DATA Step, Macro, Functions and more

correction of a wrong date

Reply
Frequent Contributor
Posts: 77

correction of a wrong date

Is there any convenient way to correct a wrong date entered by users? For example, if a user enters "31apr2011"d, it will be corrected to "30apr2011"d by the program.


data a;
a="31apr2011"d; /* a_corrected="30apr2011"d */
b="30feb2011"d; /* b_corrected="28feb2011"d */
run;
Super Contributor
Super Contributor
Posts: 3,174

Re: correction of a wrong date

Using a DATA step, you would need to use the INPUT function to test the data -- SAS will return a MISSING value if invalid, then set a DO/END loop decrementing by one and again testing the value using INPUT.

For consideration, the INPUT example below shows how to tell SAS to suppress an invalid-data condition from the SASLOG:

test_date = INPUT(,?? DATE9.);

Scott Barry
SBBWorks, Inc.
Valued Guide
Posts: 634

Re: correction of a wrong date

If, as your sample suggests, you are only worried about the last day of the month, the following uses Scott's suggestion to create a new date value.
[pre]
data a;
a="31apr2011"; output;
a="30feb2011"; output;
a="15sep2011"; output;
run;

data last;
set a;
date = INPUT(a,?? DATE9.);
if date= . then do;
substr(a,1,2)='01';
date=intnx('month',input(a,date9.),0,'e');
end;
format date date9.;
run;
proc print data=last;
run;
[/pre]

Clearly there are other date traps that may concern you.
Ask a Question
Discussion stats
  • 2 replies
  • 160 views
  • 0 likes
  • 3 in conversation