BookmarkSubscribeRSS Feed
Shmuel
Garnet | Level 18

Here is a code to convert your sample into sas dates.

 

data test;
   infile datalines truncover;
   input datex $12.; /* anydate10.; */
   datex = lowcase(datex);

             /* define your preferenced order of trial */
  if input(datex,?? date9.) > 0
     then date = input(datex,date9.); else
  if input(datex,?? date7.) > 0
     then date = input(datex,date9.); else
 if input(datex,?? ddmmyy10.) > 0
    then date = input(datex,ddmmyy10.); else
 if input(datex,?? yymmdd10.) > 0
    then date = input(datex,yymmdd10.);
else do;
   datex = translate(datex,' ','unk');
   do i=1 to 3;
       num = scan(datex,i,'-/');
       if (0 < num le 12) then mm=num; else
       if (0 < num le 31) then dd=num; else
      if num le 99 or num > 1900 then yy=num; /* year 2 or 4 digits */
   end;
   if yy =. then yy = input(datex, ?? best11.);
   if dd le 0 then dd=01;
   if mm le 0 then mm=01;
   date = mdy(mm,dd,yy);
end;

format date ddmmyy10.;

datalines;
13apr1999
120499
2016-11-19
11-18-2016
2016
un-18-2018
un-unk-2016
;run;

 

 

BUT, as I posted earlier:

how would you interpret input like: 09-05-2015
is it a ddmmyy10. informat or mmddyy10. informat ?
 
and what about: 120499 - is it 12apr1999 or 12apr2099 or 04dec1999 etc?
 
You cannot have a unique result unless you have some more restrictions.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 15 replies
  • 4561 views
  • 1 like
  • 5 in conversation