DATA Step, Macro, Functions and more

Character date time conversion to numeric date

Reply
Contributor
Posts: 36

Character date time conversion to numeric date

I have a problem with a 'Date'.

 

I have a date value in this format DDMMMYYYY:00:00:00. Now, during some operation, it gets converted to a number (10 digit) while sending it to a character field with some other numeric and character values. I would like to convert it back to a date value, else it does not make sense for presentation purpose.

 

I have tried:

 

data want;

set have;

x=input(strip(value), datetime18.);

format x Date9.;

run;

 

It does not work. Also, will be possible to keep the date at the same character field, after the conversion?  Reason is, the character field is supposed to hold the values of multiple character, numeric and date values.

 

Thank you all in advance.

Super User
Posts: 5,256

Re: Character date time conversion to numeric date

Not understand how and why you do the different conversions.

But the best would be to store the value as a SAS date (dateime) value, then you can use formats to display it almost in any format that you like, without having to do conversions.

Assuming that you have 0:00:00 as a time in your example, this is really a date value, right?

The simplest is to do a substr() and input() using date9. informat.

Data never sleeps
Super User
Posts: 17,784

Re: Character date time conversion to numeric date

A single variable cannot be date, numeric, character at the same time. The usual solution in such a situation is to keep it in a character field.
Contributor
Posts: 44

Re: Character date time conversion to numeric date

I think that your datetime variable is SA datetime variable if it is ten digit long number, number of seconds after 01JAN1960 00:00. You just have to use proper format to display it. You can take the day out with datepart function as separate variable.

Super User
Super User
Posts: 7,399

Re: Character date time conversion to numeric date

Out of interest, is there a reason that you do not use ISO dates, i.e. YYYYMMDDTHH:MMSmiley FrustratedS...?  I would have thought that would be better for a text field and more standard.  It can then be used with the E8601 format, and you can keep date, time, or datetime as a numeric field.  This is what we do in my industry, numeric date, time, and character ISO date.

Contributor
Posts: 36

Re: Character date time conversion to numeric date

The dates came from oracle. I'm calculating maximum value for a list of character, numeric and date variables. And I send all the maximum values to a field called max_value, after transposing them. I use _numeric_ to get all the numeric values and date gets converted to a number in the max value field. As I said, now I need to convert it to some form of readable date, and keep it/push it inside the same max value column. The date from oracle came in this form: 18FEB2010:00:00:00
Super User
Super User
Posts: 7,399

Re: Character date time conversion to numeric date

Lets backtrack a bit, the post doesn't make much sense, you can't take a max of character data.  Perhaps start by posting some example test data (a couple of obs) in the form of a datastep, and what you want the output to look like.

Valued Guide
Posts: 858

Re: Character date time conversion to numeric date

Here's some code that gets you back and forth between char and date formats:

 

data have;
format date datetime18.;
date = '23OCT2015:10:40:30'dt;
date_char = input(put(date,datetime18.),$18.);
run;

data want;
set have(keep=date_char);
format date date9.;
date = input(put(date_char,$18.),date9.);
run;

Ask a Question
Discussion stats
  • 7 replies
  • 357 views
  • 0 likes
  • 6 in conversation