<?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: Imported column may be a date or a datetime in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Imported-column-may-be-a-date-or-a-datetime/m-p/669881#M201013</link>
    <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select int(max(datevar)) into :maxval from imported;
quit;

%if &amp;amp;maxval &amp;gt; 80000 %then %do;

data imported;
set imported;
format datevar yymmddd10.;
datevar = datepart(datevar);
run;

%end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;A value of 80000 would constitute a date very far into the future, but a datetime still on 1960-01-01, so it's a good cutoff.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 16 Jul 2020 13:42:13 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-07-16T13:42:13Z</dc:date>
    <item>
      <title>Imported column may be a date or a datetime</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Imported-column-may-be-a-date-or-a-datetime/m-p/669872#M201007</link>
      <description>&lt;P&gt;Hello - I have some code that reads an excel file, there are two columns in this file.&amp;nbsp; One is a customer identification number and the other is a date or a datetime.&amp;nbsp; I have no control over the contents of the excel file so I need to be able to read the date/datetime field and convert it to a date.&amp;nbsp; When I use the datepart function and the column is a date it turns the date to 1/1/1960 but works perfectly fine if the column is a datetime value.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have searched long and hard for a reasonable solution without success. Is there a way to make the value a date value without compromising the data, regardless of the datatype (date/datetime)&amp;nbsp;of the column?&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jul 2020 12:49:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Imported-column-may-be-a-date-or-a-datetime/m-p/669872#M201007</guid>
      <dc:creator>SyidaRox</dc:creator>
      <dc:date>2020-07-16T12:49:49Z</dc:date>
    </item>
    <item>
      <title>Re: Imported column may be a date or a datetime</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Imported-column-may-be-a-date-or-a-datetime/m-p/669876#M201010</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;have you tried the anydtdte informat ?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename a temp;

data _NULL_;
file a;
put "01Jan2020";
put "05Jan2020:00:00:00";
run;

data a;
infile a;
attrib date format=date9. informat=anydtdte.;
input date;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Jul 2020 13:11:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Imported-column-may-be-a-date-or-a-datetime/m-p/669876#M201010</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-07-16T13:11:10Z</dc:date>
    </item>
    <item>
      <title>Re: Imported column may be a date or a datetime</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Imported-column-may-be-a-date-or-a-datetime/m-p/669879#M201011</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30622"&gt;@gamotte&lt;/a&gt; that would be the perfect solution, if the data would be provided in a reasonable file-format, but &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/49957"&gt;@SyidaRox&lt;/a&gt; has to process an excel-file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/49957"&gt;@SyidaRox&lt;/a&gt;: I would define the variable to be a date in the excel-file an reject any file not fulfilling that requirement. Data has to comply with a specification to get reliable results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following code checks the variable "maybe_date" and creates a data-step with the date-version of the variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc import datafile="crap.xlsx" out=work.narf dbms=xlsx replace;
   sheet='sheet_name';
run;

data _null_;
   set work.narf(obs=1);

   if index(vformat(maybe_date), 'DATETIME') then do;
      call execute('data work.narf_fixed;');
      call execute('set work.narf(rename=(maybe_date=_datetime));');
      call execute('attrib maybe_date length=8 format=date9.;');
      call execute('maybe_date = datepart(_datetime);');
      call execute('drop _datetime;');
      call execute('run;');
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Jul 2020 13:20:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Imported-column-may-be-a-date-or-a-datetime/m-p/669879#M201011</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-07-16T13:20:17Z</dc:date>
    </item>
    <item>
      <title>Re: Imported column may be a date or a datetime</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Imported-column-may-be-a-date-or-a-datetime/m-p/669881#M201013</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select int(max(datevar)) into :maxval from imported;
quit;

%if &amp;amp;maxval &amp;gt; 80000 %then %do;

data imported;
set imported;
format datevar yymmddd10.;
datevar = datepart(datevar);
run;

%end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;A value of 80000 would constitute a date very far into the future, but a datetime still on 1960-01-01, so it's a good cutoff.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jul 2020 13:42:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Imported-column-may-be-a-date-or-a-datetime/m-p/669881#M201013</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-16T13:42:13Z</dc:date>
    </item>
    <item>
      <title>Re: Imported column may be a date or a datetime</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Imported-column-may-be-a-date-or-a-datetime/m-p/669888#M201015</link>
      <description>&lt;P&gt;I would simplify&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;'s solution to be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   if datevar &amp;gt; '01jan2030'd then datevar = datepart(datevar);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This should also handle cases where the date variable changes row by row, sometimes a date and sometimes a datetime.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jul 2020 13:52:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Imported-column-may-be-a-date-or-a-datetime/m-p/669888#M201015</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-07-16T13:52:25Z</dc:date>
    </item>
    <item>
      <title>Re: Imported column may be a date or a datetime</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Imported-column-may-be-a-date-or-a-datetime/m-p/669890#M201016</link>
      <description>&lt;P&gt;The raw value 86400 corresponds to a date of 2196-07-21, so I would use a date in that range as cutoff, to be on the safe side (dates for contract expiration or similar could easily fall beyond 2030).&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jul 2020 13:58:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Imported-column-may-be-a-date-or-a-datetime/m-p/669890#M201016</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-16T13:58:07Z</dc:date>
    </item>
    <item>
      <title>Re: Imported column may be a date or a datetime</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Imported-column-may-be-a-date-or-a-datetime/m-p/669923#M201038</link>
      <description>Worked perfectly. Thank you!</description>
      <pubDate>Thu, 16 Jul 2020 16:15:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Imported-column-may-be-a-date-or-a-datetime/m-p/669923#M201038</guid>
      <dc:creator>SyidaRox</dc:creator>
      <dc:date>2020-07-16T16:15:10Z</dc:date>
    </item>
  </channel>
</rss>

