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

Hi there

I am a new SAS user.

I have a character DATETIME variable named "run_date" recorded as "YYYY-MM-DD HH:MM" which has a 17 character length. (2018-OCT-11 09:15).

I want to convert into numeric datetime variable in date19 format.

Hope you can help.

 

Thanks

Yuba 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

There is not such thing as a DATE19. format DATE formats run from 5 to 11.

 

You would want a DATETIME format to display the time component.

 

Congratulations for finding one of the sillier date time formats:

 

data example;
  x="2018-OCT-11 09:15";
  y = dhms(input(x,anydtdte11.),0,0,input(substr(x,13),time.));
  format y datetime19.;
run;

Which uses a specific length  to read the date part from the first 11 characters and the time from the last 5.

If your "dates" don't have 2-digit values for the day this will likely fail.

View solution in original post

6 REPLIES 6
Reeza
Super User
Something like the following should work - ANYDTDTM will automatically detect your datetime informat.

run_date_num = input(run_date, anydtdtm.);
format run_date_num datetime.;
yubaraj
Fluorite | Level 6

Hi Reeza

Thanks a lot for your quick response.

The program you suggested runs perfectly with no error message on the log window. but the output data has all the 'run_date_num' as blank. 

The original variable was in  Character format $17 and informat$17 and not all observations have a 'run date' data.

This is surprising, any idea what could be the reason.

 

Thanks

Yuba 

Reeza
Super User

Show your code and log please. Could be anything from a missing semicolon to a different format for you date than shown in your question. 

 


@yubaraj wrote:

Hi Reeza

Thanks a lot for your quick response.

The program you suggested runs perfectly with no error message on the log window. but the output data has all the 'run_date_num' as blank. 

The original variable was in  Character format $17 and informat$17 and not all observations have a 'run date' data.

This is surprising, any idea what could be the reason.

 

Thanks

Yuba 


 

Reeza
Super User

NVM - its because you have a very unique data format and you'll need a specific informat for it.

 

 "YYYY-MM-DD HH:MM" => this is a standard format and my methods works in this case

2018-OCT-11 09:15 => this is a non-standard format and my method does not work in this case.

 

My apologies, I didn't notice the discrepancy between these initially. See below. 

 

data example;
date_have = "2018-OCT-11 09:15";
date_want = input(date_have, anydtdtm.);
output;
date_have = "2018-10-11 09:15";
date_want = input(date_have, anydtdtm.);
output;
format date_want datetime.;
run;

proc print data=example;
run;
ballardw
Super User

There is not such thing as a DATE19. format DATE formats run from 5 to 11.

 

You would want a DATETIME format to display the time component.

 

Congratulations for finding one of the sillier date time formats:

 

data example;
  x="2018-OCT-11 09:15";
  y = dhms(input(x,anydtdte11.),0,0,input(substr(x,13),time.));
  format y datetime19.;
run;

Which uses a specific length  to read the date part from the first 11 characters and the time from the last 5.

If your "dates" don't have 2-digit values for the day this will likely fail.

yubaraj
Fluorite | Level 6
Great Thanks ballardw , that worked perfectly. Thanks Reeza.

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.

Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 1367 views
  • 3 likes
  • 3 in conversation