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

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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