BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
slolay
Fluorite | Level 6

Hi

I have a text variable containg dates in the format:

YYYY-MMM-DD

2011-FEB-14

2002-SEP-22

etc

I'm trying to convert these to SAS dates using input statement but get invalid data message for the INPUT statement

Any help on how to get this working

eg

data test;

    date1='2011-AUG-12';

    date2=input(date1,yymmdd11.);

run;

Thanks
Steve

1 ACCEPTED SOLUTION

Accepted Solutions
6 REPLIES 6
BrunoMueller
SAS Super FREQ

Hi Steve

I do not know of a format, but you can write a function to make a SAS date value out of the string, see sample below:

proc fcmp outlib=work.myFunctions.dates;
  function xDate( date $);
    length dlm $ 1;
    dlm =
"-";
    year = scan(date,
1, dlm);
    month = scan(date, 2, dlm);
    day = scan(date, 3, dlm);
    newDate = cats(day, month, year);
    newDate_N = input(newDate,
date9.);
    return(newDate_N);
  endsub;
run;


options
 
cmplib=(work.myFunctions)
;


data testFunction;
  date1 = '2011-AUG-12';
  date2 = xDate(date1);
 
format date2 date9.;
run;

Bruno

jcheema
Calcite | Level 5

How about:

data test;   

      date1='2011-AUG-12';    

     date2=input(substr(date1,10,2)||substr(date1,6,3)||substr(date1,1,4),date9.);

     put date1= date2= ;

run;

FriedEgg
SAS Employee

anydtdte11.

Linlin
Lapis Lazuli | Level 10

data test;

    date1='2011-AUG-12';

    date2=input(date1,anydtdte11.);

          format date2 mmddyy10.;

run;

proc print;run;

slolay
Fluorite | Level 6

Thanks - does the job!...now to read up on it......

art297
Opal | Level 21

Not a lot to look up on it (http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002605538.htm), but the anydtdte informat appears to be able to read dates that give all of the other date informats problems.  A useful system option to supplement it is the:

options datestyle=mdy

in order to guide the informat regarding the order of the three date parts.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 1801 views
  • 4 likes
  • 6 in conversation