BookmarkSubscribeRSS Feed
zoomzoom
Obsidian | Level 7

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.

7 REPLIES 7
LinusH
Tourmaline | Level 20

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
Reeza
Super User
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.
AskoLötjönen
Quartz | Level 8

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Out of interest, is there a reason that you do not use ISO dates, i.e. YYYYMMDDTHH:MM:SS...?  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.

zoomzoom
Obsidian | Level 7
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
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Steelers_In_DC
Barite | Level 11

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 7 replies
  • 3000 views
  • 0 likes
  • 6 in conversation