<?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: Numeric date variable with some values dates and some datetime in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Numeric-date-variable-with-some-values-dates-and-some-datetime/m-p/455075#M115076</link>
    <description>&lt;P&gt;Sure.&amp;nbsp; First trick is to get the numeric values.&amp;nbsp; Assuming that the strings you presented actually contain the values in parentheses:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;num_date_mix = input( scan(variablex, 2, '()'), 11.);&lt;/P&gt;
&lt;P&gt;format num_date_mix date_mix.;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The trick is to create the format DATE_MIX. properly, so that it accurately displays both dates and times.&amp;nbsp; You can do that, assuming that you have reasonably current values (not datetimes going back to 1960, not dates in the next century).&amp;nbsp; For example, here's the idea (although you may need to fix the syntax):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc format;&lt;/P&gt;
&lt;P&gt;value date_mix&amp;nbsp; low-40000 = [yymmdd10.]&amp;nbsp; &amp;nbsp;40001-high=[datetime23.];&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course the format needs to exist in order to add the FORMAT in the DATA step.&lt;/P&gt;</description>
    <pubDate>Wed, 18 Apr 2018 09:22:10 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2018-04-18T09:22:10Z</dc:date>
    <item>
      <title>Numeric date variable with some values dates and some datetime</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Numeric-date-variable-with-some-values-dates-and-some-datetime/m-p/455070#M115073</link>
      <description>&lt;P&gt;Hi all&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am fairly sure this is not possible but I want to know if I can have one numeric date variable that has a mixed values some of just dates and other with date and times.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, I have VARIABLEX which is character with values such as 2017-09-27 (1822089600) and 2017-10-12T08:35 (21093) where the number in the brackets is the numeric values of each date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can I make a new numeric variable which these values formatted ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Colm&lt;/P&gt;</description>
      <pubDate>Wed, 18 Apr 2018 09:13:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Numeric-date-variable-with-some-values-dates-and-some-datetime/m-p/455070#M115073</guid>
      <dc:creator>ColmSmyth</dc:creator>
      <dc:date>2018-04-18T09:13:09Z</dc:date>
    </item>
    <item>
      <title>Re: Numeric date variable with some values dates and some datetime</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Numeric-date-variable-with-some-values-dates-and-some-datetime/m-p/455075#M115076</link>
      <description>&lt;P&gt;Sure.&amp;nbsp; First trick is to get the numeric values.&amp;nbsp; Assuming that the strings you presented actually contain the values in parentheses:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;num_date_mix = input( scan(variablex, 2, '()'), 11.);&lt;/P&gt;
&lt;P&gt;format num_date_mix date_mix.;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The trick is to create the format DATE_MIX. properly, so that it accurately displays both dates and times.&amp;nbsp; You can do that, assuming that you have reasonably current values (not datetimes going back to 1960, not dates in the next century).&amp;nbsp; For example, here's the idea (although you may need to fix the syntax):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc format;&lt;/P&gt;
&lt;P&gt;value date_mix&amp;nbsp; low-40000 = [yymmdd10.]&amp;nbsp; &amp;nbsp;40001-high=[datetime23.];&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course the format needs to exist in order to add the FORMAT in the DATA step.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Apr 2018 09:22:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Numeric-date-variable-with-some-values-dates-and-some-datetime/m-p/455075#M115076</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-04-18T09:22:10Z</dc:date>
    </item>
    <item>
      <title>Re: Numeric date variable with some values dates and some datetime</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Numeric-date-variable-with-some-values-dates-and-some-datetime/m-p/455077#M115077</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16581"&gt;@ColmSmyth&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi all&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am fairly sure this is not possible but I want to know if I can have one numeric date variable that has a mixed values some of just dates and other with date and times.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, I have VARIABLEX which is character with values such as 2017-09-27 (1822089600) and 2017-10-12T08:35 (21093) where the number in the brackets is the numeric values of each date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can I make a new numeric variable which these values formatted ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Colm&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I guess you mixed up the numerical values in your example.&lt;/P&gt;
&lt;P&gt;The answer to the question is no. A variable in a dataset has the same format throughout.&lt;/P&gt;
&lt;P&gt;In your case, I'd convert selectively depending on length, and store the result in separate date and time variables, where the time variable is set to zero or missing when only a date is present in the input.&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input variablex :$16.;
format mydate yymmddd10. mytime time8.;
if length(variablex) = 10
then do;
  mydate = input(variablex,yymmdd10.);
  mytime = 0;
end;
else do;
  mydate = datepart(input(variablex,e8601dt16.));
  mytime = timepart(input(variablex,e8601dt16.));
end;
cards;
2017-10-12T08:35
2017-09-27
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Apr 2018 09:26:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Numeric-date-variable-with-some-values-dates-and-some-datetime/m-p/455077#M115077</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-04-18T09:26:38Z</dc:date>
    </item>
    <item>
      <title>Re: Numeric date variable with some values dates and some datetime</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Numeric-date-variable-with-some-values-dates-and-some-datetime/m-p/455079#M115079</link>
      <description>&lt;P&gt;Hi Kurt&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, I did mix those up but yes as expected I did not think this is possible. Ultimately here my goal is to try and compare dates where one may have a time and another may not. If i split per your suggestion and then remake as a datetime value for example SAS will have 00:00:00 etc into it which gets considered in any equation as midnight.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have other ways around this though but thank you for your suggestion. It would be great however if SAS came up with a format for a date that could understand dates and datetimes in one field&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Colm&lt;/P&gt;</description>
      <pubDate>Wed, 18 Apr 2018 09:31:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Numeric-date-variable-with-some-values-dates-and-some-datetime/m-p/455079#M115079</guid>
      <dc:creator>ColmSmyth</dc:creator>
      <dc:date>2018-04-18T09:31:31Z</dc:date>
    </item>
    <item>
      <title>Re: Numeric date variable with some values dates and some datetime</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Numeric-date-variable-with-some-values-dates-and-some-datetime/m-p/455080#M115080</link>
      <description>&lt;P&gt;Hi there&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for your reply, I was only representing the number date in brackets etc, but think I mixed them up anyway. Not to worry though as per my reply below to Kurt what I want is not possible.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;colm&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Apr 2018 09:32:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Numeric-date-variable-with-some-values-dates-and-some-datetime/m-p/455080#M115080</guid>
      <dc:creator>ColmSmyth</dc:creator>
      <dc:date>2018-04-18T09:32:53Z</dc:date>
    </item>
    <item>
      <title>Re: Numeric date variable with some values dates and some datetime</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Numeric-date-variable-with-some-values-dates-and-some-datetime/m-p/455082#M115082</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16581"&gt;@ColmSmyth&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi Kurt&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, I did mix those up but yes as expected I did not think this is possible. Ultimately here my goal is to try and compare dates where one may have a time and another may not. If i split per your suggestion and then remake as a datetime value for example SAS will have 00:00:00 etc into it which gets considered in any equation as midnight.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have other ways around this though but thank you for your suggestion. It would be great however if SAS came up with a format for a date that could understand dates and datetimes in one field&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Colm&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Such variables would not be useful anyway, as you would have to check their contents everytime before any following analysis (like &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;'s format does), making this impractical.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Apr 2018 09:39:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Numeric-date-variable-with-some-values-dates-and-some-datetime/m-p/455082#M115082</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-04-18T09:39:24Z</dc:date>
    </item>
    <item>
      <title>Re: Numeric date variable with some values dates and some datetime</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Numeric-date-variable-with-some-values-dates-and-some-datetime/m-p/455091#M115086</link>
      <description>&lt;P&gt;Agreed.....oddly I have tried your code and the e8601dt16. format is saying not valid yet I see it in the list of formats for SAS EG...VARIABLEX is character so the input should work&lt;/P&gt;</description>
      <pubDate>Wed, 18 Apr 2018 09:43:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Numeric-date-variable-with-some-values-dates-and-some-datetime/m-p/455091#M115086</guid>
      <dc:creator>ColmSmyth</dc:creator>
      <dc:date>2018-04-18T09:43:14Z</dc:date>
    </item>
    <item>
      <title>Re: Numeric date variable with some values dates and some datetime</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Numeric-date-variable-with-some-values-dates-and-some-datetime/m-p/455094#M115089</link>
      <description>&lt;P&gt;Please post that particular log. Which SAS version are you using from EG?&lt;/P&gt;</description>
      <pubDate>Wed, 18 Apr 2018 09:47:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Numeric-date-variable-with-some-values-dates-and-some-datetime/m-p/455094#M115089</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-04-18T09:47:03Z</dc:date>
    </item>
    <item>
      <title>Re: Numeric date variable with some values dates and some datetime</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Numeric-date-variable-with-some-values-dates-and-some-datetime/m-p/455095#M115090</link>
      <description>&lt;P&gt;If all you need is to detect which it is, you don't need a format for that.&amp;nbsp; Just set up a rule as to high large a date can be.&amp;nbsp; Assuming you have already converted the character values to a mix of dates and datetimes:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if num_value &amp;gt; 40000 then type='datetime';&lt;/P&gt;
&lt;P&gt;else type='date';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Better yet (and safer because these sorts of rules have exceptions), create the variable TYPE based on the original VARIABLEX, and save it as part of the data set.&amp;nbsp; That doesn't give you a format, but you don't need it when you have VARIABLEX.&amp;nbsp; It just gives you a clue as to what to do when calculating based on the numeric value.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Apr 2018 09:48:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Numeric-date-variable-with-some-values-dates-and-some-datetime/m-p/455095#M115090</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-04-18T09:48:11Z</dc:date>
    </item>
  </channel>
</rss>

