<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Convert  a $char10. to datetime format DATETIME20. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Convert-a-char10-to-datetime-format-DATETIME20/m-p/908176#M358440</link>
    <description>&lt;P&gt;You can't convert a variable from a character type ($char) to a numeric type (format datetime20.).&amp;nbsp;&amp;nbsp;You have to make a new variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use the INPUT function (somewhat analogous to the input &lt;EM&gt;&lt;STRONG&gt;statement&lt;/STRONG&gt;&lt;/EM&gt;) to "read" the character variable into a date variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;date_var=input(char_var,ddmmyy10.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;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:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  dtime_var=dhms(datevar,0,0,0);
  format dtime_var datetime20. ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to keep DATE_VAR in addition to DTIME_VAR, make sure to format it with a date format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 15 Dec 2023 12:20:21 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2023-12-15T12:20:21Z</dc:date>
    <item>
      <title>Convert  a $char10. to datetime format DATETIME20.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-a-char10-to-datetime-format-DATETIME20/m-p/908172#M358436</link>
      <description>&lt;P&gt;Hi everyone,&lt;BR /&gt;&lt;BR /&gt;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".&lt;BR /&gt;&amp;nbsp;Could you help me, please?&lt;BR /&gt;&lt;BR /&gt;Thanks in advance &lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 11:51:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-a-char10-to-datetime-format-DATETIME20/m-p/908172#M358436</guid>
      <dc:creator>agdato</dc:creator>
      <dc:date>2023-12-15T11:51:41Z</dc:date>
    </item>
    <item>
      <title>Re: Convert  a $char10. to datetime format DATETIME20.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-a-char10-to-datetime-format-DATETIME20/m-p/908173#M358437</link>
      <description>/* Definir la cadena de caracteres */&lt;BR /&gt;data hola;&lt;BR /&gt;cadena = '23/01/2023';&lt;BR /&gt;&lt;BR /&gt;/* Convertir la cadena al formato de fecha utilizando el formato dd/mm/yyyy */&lt;BR /&gt;fecha = input(cadena, ddmmyy10.);&lt;BR /&gt;&lt;BR /&gt;/* Aplicar el formato DATETIME20. */&lt;BR /&gt;fecha_formateada = put(fecha, datetime20.);&lt;BR /&gt;&lt;BR /&gt;/* Mostrar el resultado */&lt;BR /&gt;put fecha_formateada=;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;When i tried this code they send me as answer 01JAN1960:06:23:53</description>
      <pubDate>Fri, 15 Dec 2023 12:16:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-a-char10-to-datetime-format-DATETIME20/m-p/908173#M358437</guid>
      <dc:creator>agdato</dc:creator>
      <dc:date>2023-12-15T12:16:05Z</dc:date>
    </item>
    <item>
      <title>Re: Convert  a $char10. to datetime format DATETIME20.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-a-char10-to-datetime-format-DATETIME20/m-p/908174#M358438</link>
      <description>&lt;P&gt;Assuming you have made a typo somewhere, and 23/01/2023 should be&amp;nbsp;&lt;SPAN&gt;&lt;FONT color="#FF0000"&gt;23&lt;/FONT&gt;JAN2023:00:00:00, this works&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    chardate="23/01/2023";
    numdate=input(chardate,ddmmyy10.);
    numdatetime=dhms(numdate,0,0,0);
    format numdate date9. numdatetime datetime20.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Dec 2023 12:16:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-a-char10-to-datetime-format-DATETIME20/m-p/908174#M358438</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-12-15T12:16:26Z</dc:date>
    </item>
    <item>
      <title>Re: Convert  a $char10. to datetime format DATETIME20.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-a-char10-to-datetime-format-DATETIME20/m-p/908175#M358439</link>
      <description>&lt;P&gt;Thanks ! was perfect, so i need to specify also de time !&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 12:18:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-a-char10-to-datetime-format-DATETIME20/m-p/908175#M358439</guid>
      <dc:creator>agdato</dc:creator>
      <dc:date>2023-12-15T12:18:39Z</dc:date>
    </item>
    <item>
      <title>Re: Convert  a $char10. to datetime format DATETIME20.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-a-char10-to-datetime-format-DATETIME20/m-p/908176#M358440</link>
      <description>&lt;P&gt;You can't convert a variable from a character type ($char) to a numeric type (format datetime20.).&amp;nbsp;&amp;nbsp;You have to make a new variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use the INPUT function (somewhat analogous to the input &lt;EM&gt;&lt;STRONG&gt;statement&lt;/STRONG&gt;&lt;/EM&gt;) to "read" the character variable into a date variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;date_var=input(char_var,ddmmyy10.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;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:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  dtime_var=dhms(datevar,0,0,0);
  format dtime_var datetime20. ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to keep DATE_VAR in addition to DTIME_VAR, make sure to format it with a date format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 12:20:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-a-char10-to-datetime-format-DATETIME20/m-p/908176#M358440</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-12-15T12:20:21Z</dc:date>
    </item>
    <item>
      <title>Re: Convert  a $char10. to datetime format DATETIME20.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-a-char10-to-datetime-format-DATETIME20/m-p/908403#M358480</link>
      <description>&lt;P&gt;Date values are stored in/counted by DAYS.&lt;/P&gt;
&lt;P&gt;Time and Datetime values are stored in/counter by SECONDS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 21:11:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-a-char10-to-datetime-format-DATETIME20/m-p/908403#M358480</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-12-15T21:11:06Z</dc:date>
    </item>
    <item>
      <title>Re: Convert  a $char10. to datetime format DATETIME20.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-a-char10-to-datetime-format-DATETIME20/m-p/908454#M358482</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/460732"&gt;@agdato&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi everyone,&lt;BR /&gt;&lt;BR /&gt;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".&lt;BR /&gt;&amp;nbsp;Could you help me, please?&lt;BR /&gt;&lt;BR /&gt;Thanks in advance &lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;There is a lot of confusion in the way this question is worded. Variables are not "in a format".&amp;nbsp; Variables have formats attached to them that determine the default way to display them.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And you cannot use the DATETIME format with a variable that has DATE values.&amp;nbsp; You need to use a format that works on DATETIME values (number of seconds) not one that works on DATE values (number of days).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And you cannot put numeric values (both DATE and DATETIME values are numbers) into an existing character variable.&amp;nbsp; So you will need to make a NEW variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also you only need 18 characters to display datetime value in ddMMMyyyy:hh:mm:ss style.&amp;nbsp; But the DATETIME format has a bug and if you use DATETIME18 you get only two digits of the year.&amp;nbsp; 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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So it sounds like you meant to ask:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use the INPUT() function with an appropriate INFORMAT to convert a character string into a number.&amp;nbsp; The DDMMYYYY10. informat will make a DATE value from the strings you have.&amp;nbsp; &amp;nbsp;You would then need to convert that into a DATETIME value to be able to attach the DATETIME format (and have it work properly).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  datetime=dhms(input(chardate,mmddyy10.),0,0,0);
  format datetime datetime19.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;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'.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options datestyle=dmy;
data want;
  set have;
  datetime=input(chardate,anydtdtm10.);
  format datetime datetime19.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 16 Dec 2023 00:18:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-a-char10-to-datetime-format-DATETIME20/m-p/908454#M358482</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-12-16T00:18:34Z</dc:date>
    </item>
  </channel>
</rss>

