BookmarkSubscribeRSS Feed
MarcTC
Obsidian | Level 7
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;
5 REPLIES 5
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
ArtC
Rhodochrosite | Level 12
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.
PrasadYadla
Calcite | Level 5

Hi,

 

What if the date column has missing values from before and few dates have the wrong day (31& 32) .

 

Thanks,

andreas_lds
Jade | Level 19

@PrasadYadla wrote:

Hi,

 

What if the date column has missing values from before and few dates have the wrong day (31& 32) .

 

Thanks,


Enclosing the correcting code with an appropriate if-statement should help:

data a;
   a="31apr2011";
   output;
   a="30feb2011";
   output;
   a = " ";
   output;
   a="15sep2011";
   output;
run;

data last;
   set a;

   if not missing(a) then
      do;
         date=INPUT(a, ?? DATE9.);

         if date=. then
            do;
               substr(a, 1, 2)='01';
               date=intnx('month', input(a, date9.), 0, 'e');
            end;
      end;
   format date date9.;
run;
PrasadYadla
Calcite | Level 5
Thank you for your reply

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2284 views
  • 0 likes
  • 5 in conversation