@agdato wrote:
Hi everyone, I have "23/01/2023" in character format $CHAR10. and I would like to convert it in a date format DATETIME20. to see the date as "21jJAV2023:00:00:00". Could you help me, please? Thanks in advance 😄
There is a lot of confusion in the way this question is worded. Variables are not "in a format". Variables have formats attached to them that determine the default way to display them.
And you cannot use the DATETIME format with a variable that has DATE values. You need to use a format that works on DATETIME values (number of seconds) not one that works on DATE values (number of days).
Also knowing that a variable has a character format like $CHAR10. attached to it that display 10 bytes does not necessarily tell us what length the character variable actually has.
And you cannot put numeric values (both DATE and DATETIME values are numbers) into an existing character variable. So you will need to make a NEW variable.
Also you only need 18 characters to display datetime value in ddMMMyyyy:hh:mm:ss style. But the DATETIME format has a bug and if you use DATETIME18 you get only two digits of the year. So you must use at least DATETIME19 to insure that all four digits of the year is displayed. There is no value in moving to DATETIME20 instead.
So it sounds like you meant to ask:
I want to convert a character variable of length $10 that has string in the style DD/MM/YYYY into a numeric variable with datetime values with the DATETIME19. format attached to it.
You can use the INPUT() function with an appropriate INFORMAT to convert a character string into a number. The DDMMYYYY10. informat will make a DATE value from the strings you have. You would then need to convert that into a DATETIME value to be able to attach the DATETIME format (and have it work properly).
So assuming your dataset is named HAVE and the variable is named CHARDATE we can make a dataset named WANT with a new variable named DATETIME with code like this:
data want;
set have;
datetime=dhms(input(chardate,mmddyy10.),0,0,0);
format datetime datetime19.;
run;
The ANYDTDTM10. informat will make a DATETIME value, but you will want to make sure the DATESTYLE option is set to DMY or else it might choose the wrong field order for ambiguous strings like '01/04/2023'.
options datestyle=dmy;
data want;
set have;
datetime=input(chardate,anydtdtm10.);
format datetime datetime19.;
run;
... View more