<?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: Converting date to datetime character string in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-to-datetime-character-string/m-p/770337#M244386</link>
    <description>The character string inconsistency is dictated by the warehouse, it stores the data in that format unfortunately. So your solution worked well I just had to use 00:0:0 instead of what you had.</description>
    <pubDate>Fri, 24 Sep 2021 18:31:33 GMT</pubDate>
    <dc:creator>A_SAS_Man</dc:creator>
    <dc:date>2021-09-24T18:31:33Z</dc:date>
    <item>
      <title>Converting date to datetime character string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-to-datetime-character-string/m-p/770296#M244372</link>
      <description>&lt;P&gt;I'm trying to automate some internal reporting that queries from a database, I'm doing this by having several macro variables that store dates. The goal is to have just one date that needs updated, and the rest of the formats automatically populate. In order to supply a usable date to our warehouse I need to go from this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let&amp;nbsp;End_Date1 = 2021-09-01;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;To this:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;'09/01/2021 00:0:0'&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;In order to do this I've tried this so far.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let End_Date1 = 2021-09-01;

%let End_Date_Alt= %sysfunc(inputn(&amp;amp;End_Date1., yymmdd10.),mmddyy8.);
%let End_Date_Time_Alt=%sysfunc(putn(&amp;amp;End_Date_Alt,mmddyy8.));&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;I was then going to try to concatenate End_Date_Time_Alt with the string " 00:0:0", but my attempted conversion from a date to a date represented as a character isn't working. Any help is appreciated.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Sep 2021 16:40:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-to-datetime-character-string/m-p/770296#M244372</guid>
      <dc:creator>A_SAS_Man</dc:creator>
      <dc:date>2021-09-24T16:40:24Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date to datetime character string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-to-datetime-character-string/m-p/770302#M244374</link>
      <description>&lt;P&gt;Do you want an actual DATETIME value (number of seconds since 1960)?&lt;/P&gt;
&lt;P&gt;Or just that character string?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why does your example character string have inconsistency in the number of zeros used?&lt;/P&gt;
&lt;P&gt;Do you need one version with two digit years? Or should both versions have four digit years.&amp;nbsp; (NOTE: Using only two digits for the year will cause confusion, remember the Y2K problem.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are generating the string do you really need the quotes in the macro variable? Or just the text string? If you need the quotes do they have to be the single quotes? Or can they be double quote characters instead?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let End_Date1 = 2021-09-01;

%let End_Date_Alt= %sysfunc(inputn(&amp;amp;End_Date1., yymmdd10.),mmddyy10.);
%let End_Date_Time_Alt=&amp;amp;End_Date_Alt 00:00:00;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Sep 2021 17:05:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-to-datetime-character-string/m-p/770302#M244374</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-24T17:05:06Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date to datetime character string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-to-datetime-character-string/m-p/770305#M244375</link>
      <description>&lt;P&gt;You may have a lot better results by starting with which ever is your main "date" to be an actual date value, as in the numeric value that SAS uses to store dates. Then that value can be manipulated directly (more or less).&lt;/P&gt;
&lt;P&gt;The reason your conversion fails is that you forced it to be in effect a random string of digits with the mmddyy8. format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there something really crucial about having a mix of 2 digits for an hour value and single digits for minutes and seconds? That just plain looks odd to me. Instead of lots of ugly concatenation of macro variables I would suggest making your own datetime format since I have to assume that you will be doing this more than one time.&lt;/P&gt;
&lt;PRE&gt;Proc format;
picture mydatetime
low-high ='%0m/%0d/%Y %0H:%M:%S' (datatype=datetime);
run;

%let End_Date1 = 2021-09-01;
%let End_Date_Alt= %sysfunc(inputn(&amp;amp;End_Date1., yymmdd10.));
%let End_Date_Time_Alt=%sysfunc(putn(%sysfunc(dhms(&amp;amp;end_date_alt.,0,0,0)),mydatetime.));

%put &amp;amp;End_Date_Time_Alt;&lt;/PRE&gt;
&lt;P&gt;If you are going to make a lot of these variables I strongly suggest moving the code to a data step and using the CALL SYMPUTX statement to make the variables. The code will be much cleaner without having a bunch of %sysfunc calls. Plus once you have one date, time or datetime value you can use it with all of the functions and formats available.&lt;/P&gt;
&lt;P&gt;And for almost all practical purposes the only time your code should use formatted values for dates, times or datetimes is in the stuff people read: Titles, Filenames perhaps, row and column headings in tables or values in reports. If you are using this to "match" existing values, then you need to fix your data to use actual date values, not random strings like 2019-09-01.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Sep 2021 19:35:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-to-datetime-character-string/m-p/770305#M244375</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-09-24T19:35:44Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date to datetime character string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-to-datetime-character-string/m-p/770337#M244386</link>
      <description>The character string inconsistency is dictated by the warehouse, it stores the data in that format unfortunately. So your solution worked well I just had to use 00:0:0 instead of what you had.</description>
      <pubDate>Fri, 24 Sep 2021 18:31:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-to-datetime-character-string/m-p/770337#M244386</guid>
      <dc:creator>A_SAS_Man</dc:creator>
      <dc:date>2021-09-24T18:31:33Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date to datetime character string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-to-datetime-character-string/m-p/770338#M244387</link>
      <description>In this instance I don't have much choice, I have to match the formatting to be able to pull data from the warehouse, and it uses that strange date time format. Fixing the data would be ideal, but out of my ability to control.</description>
      <pubDate>Fri, 24 Sep 2021 18:33:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-to-datetime-character-string/m-p/770338#M244387</guid>
      <dc:creator>A_SAS_Man</dc:creator>
      <dc:date>2021-09-24T18:33:09Z</dc:date>
    </item>
  </channel>
</rss>

