<?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 time to character in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/628069#M185520</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/28913"&gt;@USHAKS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;here how it looks&lt;/P&gt;
&lt;P&gt;date time 16JUL2018:00:00:00&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;and character field&amp;nbsp; 2018-07-19&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You can use the YYMMDD informat to convert strings like '2018-07-19' to a date.&amp;nbsp; You can use teh DATEPART() function to convert number of seconds like '16JUL2018:00:00:00'dt to a date.&amp;nbsp; Then you can just subtract the two numbers.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;diff = datepart(datetime_var) - input(char_var,yymmdd10.);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 27 Feb 2020 23:00:54 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2020-02-27T23:00:54Z</dc:date>
    <item>
      <title>Converting date time to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/627665#M185328</link>
      <description>&lt;P&gt;I need help in calculating the&amp;nbsp;day difference between the following date formats&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;MyDATE&lt;/TD&gt;&lt;TD&gt;Char&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;$10.&lt;/TD&gt;&lt;TD&gt;$10.&lt;/TD&gt;&lt;TD&gt;MyDATE&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;APPDATE&lt;/TD&gt;&lt;TD&gt;Num&lt;/TD&gt;&lt;TD&gt;8&lt;/TD&gt;&lt;TD&gt;DATETIME20.&lt;/TD&gt;&lt;TD&gt;DATETIME20.&lt;/TD&gt;&lt;TD&gt;APPDATE&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Thu, 27 Feb 2020 21:58:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/627665#M185328</guid>
      <dc:creator>USHAKS</dc:creator>
      <dc:date>2020-02-27T21:58:23Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date time to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/627673#M185329</link>
      <description>&lt;P&gt;I'm going to assume you want it to remain numeric but want the format only changed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	attrib date1 informat=datetime20. format=datetime20.;
	attrib date2 informat=datetime20. format=datetime20.;
	input date1 :datetime20. date2 :datetime20.;
	datalines;
16JUL2018:00:00:00 19JUL2019:00:00:00
;
run;

data want;
	set have;
	format formatted1 yymmdd10.;
	format formatted2 yymmdd10.;
	formatted1 = datepart(date1);
	formatted2 = datepart(date2);
	datedif = intck('days', datepart(date1), datepart(date2));
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If for whatever reason you are wanting the YYYYMMDD format to be character you can do this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;character1 = put(datepart(date1), yymmdd10.);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Feb 2020 21:11:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/627673#M185329</guid>
      <dc:creator>Krueger</dc:creator>
      <dc:date>2020-02-26T21:11:54Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date time to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/627674#M185330</link>
      <description>&lt;P&gt;There might be a matter of semantics to ascertain before we can answer:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you currently have a numeric variable that contains a datetime?&lt;/P&gt;
&lt;P&gt;Do you want to create a character variable that contains a string, or do you want a numeric variable containing a SAS date*?&lt;/P&gt;
&lt;P&gt;Asking that since it is odd that you'd want to find the date difference between 2 strings.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;*A third option is keeping the datetime value and displaying just the date part&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 21:09:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/627674#M185330</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-02-26T21:09:09Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date time to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/627675#M185331</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/28913"&gt;@USHAKS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I need help in converting the date time 16JUL2018:00:00:00&amp;nbsp; to character format&amp;nbsp; 2018-07-16&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and then find the day difference between&amp;nbsp;2018-07-16 and&amp;nbsp; 2018-07-19&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;First, date/times should remain numeric. Converting them to character does not help at all in finding differences in days, and you can always display your numeric date/time with a format and so people who are looking at it will see 2018-07-16. In a DATA step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;datevar=datepart(datetimevariable);
format datevar yymmddd10.; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The difference between days, once you take the datepart of your date/time variable, is simply a subtraction.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 21:10:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/627675#M185331</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-02-26T21:10:53Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date time to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/627678#M185334</link>
      <description>&lt;P&gt;I have a date two date fields&lt;/P&gt;&lt;P&gt;I need help in calculating the&amp;nbsp;day difference between the following date formats&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;MyDATE&lt;/TD&gt;&lt;TD&gt;Char&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;$10.&lt;/TD&gt;&lt;TD&gt;$10.&lt;/TD&gt;&lt;TD&gt;MyDATE&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;APPDATE&lt;/TD&gt;&lt;TD&gt;Num&lt;/TD&gt;&lt;TD&gt;8&lt;/TD&gt;&lt;TD&gt;DATETIME20.&lt;/TD&gt;&lt;TD&gt;DATETIME20.&lt;/TD&gt;&lt;TD&gt;APPDATE&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;date time 16JUL2018:00:00:00&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;and character format as per proc contents procedure 2018-07-19&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 21:59:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/627678#M185334</guid>
      <dc:creator>USHAKS</dc:creator>
      <dc:date>2020-02-27T21:59:05Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date time to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/627681#M185337</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/28913"&gt;@USHAKS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a date two date fields&lt;/P&gt;
&lt;P&gt;one is in date time format&lt;/P&gt;
&lt;P&gt;another date is in character format (as&amp;nbsp; shown below)&lt;/P&gt;
&lt;P&gt;date time 16JUL2018:00:00:00&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;and character field&amp;nbsp; 2018-07-19&lt;/P&gt;
&lt;P&gt;So i want the day difference between these two dates which are in different format&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Formats&amp;nbsp; are not relevant to this problem. Also, it doesn't help to tell us that this is a character format -- the key information we need is if the variable (not the format) is numeric or character, this can be found from PROC CONTENTS and other methods.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming it is a numeric variable, to find the difference in days between a datetime variable and a date variable, use something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;difference = datepart (datetimevariable) - datevariable;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 12:47:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/627681#M185337</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-02-27T12:47:42Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date time to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/627682#M185338</link>
      <description>&lt;P&gt;So when you right-click on the columns in question and select Column Attributes they're both showing as Character Type and not Numeric?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm guessing they're both Numeric, the format's are just different for each. One being a datetime and the other a date.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 21:28:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/627682#M185338</guid>
      <dc:creator>Krueger</dc:creator>
      <dc:date>2020-02-26T21:28:32Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date time to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/628061#M185514</link>
      <description>&lt;P&gt;I need help in calculating the&amp;nbsp;day difference between the following date formats&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;MyDATE&lt;/TD&gt;&lt;TD&gt;Char&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;$10.&lt;/TD&gt;&lt;TD&gt;$10.&lt;/TD&gt;&lt;TD&gt;MyDATE&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;APPDATE&lt;/TD&gt;&lt;TD&gt;Num&lt;/TD&gt;&lt;TD&gt;8&lt;/TD&gt;&lt;TD&gt;DATETIME20.&lt;/TD&gt;&lt;TD&gt;DATETIME20.&lt;/TD&gt;&lt;TD&gt;APPDATE&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Thu, 27 Feb 2020 22:00:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/628061#M185514</guid>
      <dc:creator>USHAKS</dc:creator>
      <dc:date>2020-02-27T22:00:34Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date time to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/628064#M185517</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/28913"&gt;@USHAKS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I need help in calculating the&amp;nbsp;day difference between the following date formats&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;MyDATE&lt;/TD&gt;
&lt;TD&gt;Char&lt;/TD&gt;
&lt;TD&gt;10&lt;/TD&gt;
&lt;TD&gt;$10.&lt;/TD&gt;
&lt;TD&gt;$10.&lt;/TD&gt;
&lt;TD&gt;MyDATE&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;APPDATE&lt;/TD&gt;
&lt;TD&gt;Num&lt;/TD&gt;
&lt;TD&gt;8&lt;/TD&gt;
&lt;TD&gt;DATETIME20.&lt;/TD&gt;
&lt;TD&gt;DATETIME20.&lt;/TD&gt;
&lt;TD&gt;APPDATE&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It looks like you have one character variable and one numeric variable. Actually, this is where the conversation over how to do things with dates or datetimes should start, by showing us the PROC CONTENTS so we can see if the variables are numeric or text, rather than just showing us the variable values. The formats are largely irrelevant to determining the difference between the dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But now we need to see how MyDATE appears, as we need to know that, and it seems as if you have edited out that information.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 22:27:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/628064#M185517</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-02-27T22:27:13Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date time to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/628066#M185518</link>
      <description>&lt;P&gt;here how it looks&lt;/P&gt;&lt;P&gt;date time 16JUL2018:00:00:00&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;and character field&amp;nbsp; 2018-07-19&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 22:29:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/628066#M185518</guid>
      <dc:creator>USHAKS</dc:creator>
      <dc:date>2020-02-27T22:29:10Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date time to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/628069#M185520</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/28913"&gt;@USHAKS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;here how it looks&lt;/P&gt;
&lt;P&gt;date time 16JUL2018:00:00:00&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;and character field&amp;nbsp; 2018-07-19&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You can use the YYMMDD informat to convert strings like '2018-07-19' to a date.&amp;nbsp; You can use teh DATEPART() function to convert number of seconds like '16JUL2018:00:00:00'dt to a date.&amp;nbsp; Then you can just subtract the two numbers.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;diff = datepart(datetime_var) - input(char_var,yymmdd10.);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Feb 2020 23:00:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/628069#M185520</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-27T23:00:54Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date time to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/628070#M185521</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/28913"&gt;@USHAKS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;here how it looks&lt;/P&gt;
&lt;P&gt;date time 16JUL2018:00:00:00&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;and character field&amp;nbsp; 2018-07-19&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Okay, now I think we are ready to go! We will use INPUT() to turn the character variable MYDATE into a numeric SAS date value, and we will use datepart to turn the numeric datetime value into a numeric date value. Once they are both&amp;nbsp; numeric SAS date values, the two can be subtracted to determine the difference.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    difference = input(mydate,anydtdte.) - datepart(appdate);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Feb 2020 23:04:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/628070#M185521</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-02-27T23:04:44Z</dc:date>
    </item>
    <item>
      <title>Re: Converting date time to character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/629590#M186211</link>
      <description>Thank you that helped.</description>
      <pubDate>Wed, 04 Mar 2020 20:03:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-date-time-to-character/m-p/629590#M186211</guid>
      <dc:creator>USHAKS</dc:creator>
      <dc:date>2020-03-04T20:03:10Z</dc:date>
    </item>
  </channel>
</rss>

