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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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