BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
agdato
Calcite | Level 5

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 😄

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Assuming you have made a typo somewhere, and 23/01/2023 should be 23JAN2023:00:00:00, this works

 

data want;
    chardate="23/01/2023";
    numdate=input(chardate,ddmmyy10.);
    numdatetime=dhms(numdate,0,0,0);
    format numdate date9. numdatetime datetime20.;
run;
--
Paige Miller

View solution in original post

6 REPLIES 6
agdato
Calcite | Level 5
/* Definir la cadena de caracteres */
data hola;
cadena = '23/01/2023';

/* Convertir la cadena al formato de fecha utilizando el formato dd/mm/yyyy */
fecha = input(cadena, ddmmyy10.);

/* Aplicar el formato DATETIME20. */
fecha_formateada = put(fecha, datetime20.);

/* Mostrar el resultado */
put fecha_formateada=;
run;

When i tried this code they send me as answer 01JAN1960:06:23:53
Tom
Super User Tom
Super User

Date values are stored in/counted by DAYS.

Time and Datetime values are stored in/counter by SECONDS.

 

You told SAS to treat the number of days since 1960 as the number of seconds since 1960 which is why you got '06:23:53't on the first day of 1960.

 

PaigeMiller
Diamond | Level 26

Assuming you have made a typo somewhere, and 23/01/2023 should be 23JAN2023:00:00:00, this works

 

data want;
    chardate="23/01/2023";
    numdate=input(chardate,ddmmyy10.);
    numdatetime=dhms(numdate,0,0,0);
    format numdate date9. numdatetime datetime20.;
run;
--
Paige Miller
agdato
Calcite | Level 5

Thanks ! was perfect, so i need to specify also de time ! 

mkeintz
PROC Star

You can't convert a variable from a character type ($char) to a numeric type (format datetime20.).  You have to make a new variable.

 

You can use the INPUT function (somewhat analogous to the input statement) to "read" the character variable into a date variable.

 

date_var=input(char_var,ddmmyy10.);

Conceptually, the simplest way to achieve your goal, is to apply the above to generate date_var, and then apply the DHMS function to date_var to make a variable to hold a datetime value:

 

  dtime_var=dhms(datevar,0,0,0);
  format dtime_var datetime20. ;

If you want to keep DATE_VAR in addition to DTIME_VAR, make sure to format it with a date format.

 

 

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Tom
Super User Tom
Super User

@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;

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1413 views
  • 0 likes
  • 4 in conversation