<?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: Import an Excel file containing dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855945#M338230</link>
    <description>&lt;P&gt;if you already have a numeric value use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;YOUR_DTC=put(XLSX_COLUMN_NAME-21916,e8601da.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Difficult to say without knowing your data&lt;/P&gt;</description>
    <pubDate>Fri, 27 Jan 2023 13:28:07 GMT</pubDate>
    <dc:creator>Oligolas</dc:creator>
    <dc:date>2023-01-27T13:28:07Z</dc:date>
    <item>
      <title>Import an Excel file containing dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855720#M338155</link>
      <description>&lt;P&gt;&lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;Hello,&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;I have a database in Excel with a date variable of modalities 01/21/2022, 03/02/2019, 06/01/2010, etc.&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;When I import it into SAS, the date variable contains the modalities 44949.&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;I tried this code to keep date variable as character in order to convert it, it doesn't work.&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;My problem is with the import.&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;Can anyone help me please.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;Any suggestions for improving codes or another alternative are welcome.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;Thanks in advance.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;Gick&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc import datafile="I:\DOC\Questionnaires.xlsx" 
           dbms=xlsx 		   
           out=data replace ;
	mixed=yes;
     	   scantext=yes;
run;  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jan 2023 11:54:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855720#M338155</guid>
      <dc:creator>Gick</dc:creator>
      <dc:date>2023-01-26T11:54:56Z</dc:date>
    </item>
    <item>
      <title>Re: Import an Excel file containing dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855721#M338156</link>
      <description>&lt;P&gt;A quick search of the forum for "Excel dates" turns up plenty of correct answers&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/forums/searchpage/tab/message?q=excel%20dates" target="_blank"&gt;https://communities.sas.com/t5/forums/searchpage/tab/message?q=excel%20dates&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jan 2023 11:54:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855721#M338156</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-01-26T11:54:45Z</dc:date>
    </item>
    <item>
      <title>Re: Import an Excel file containing dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855899#M338211</link>
      <description>&lt;P&gt;Save the sheet to a csv file and read that with a DATA step, where&amp;nbsp;&lt;EM&gt;you&lt;/EM&gt; have full control and can deal with special values which cause the IMPORT problem (reading dates as character).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The number you got is the internal representation of a date in Excel. Add '30dec1899'd to it to get the correct SAS date.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jan 2023 07:50:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855899#M338211</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-01-27T07:50:35Z</dc:date>
    </item>
    <item>
      <title>Re: Import an Excel file containing dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855900#M338212</link>
      <description>Hello,&lt;BR /&gt;&lt;BR /&gt;where do I add it?&lt;BR /&gt;Can you elaborate more please? For example by giving an example of code.&lt;BR /&gt;&lt;BR /&gt;Thanks&lt;BR /&gt;Gick</description>
      <pubDate>Fri, 27 Jan 2023 08:00:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855900#M338212</guid>
      <dc:creator>Gick</dc:creator>
      <dc:date>2023-01-27T08:00:18Z</dc:date>
    </item>
    <item>
      <title>Re: Import an Excel file containing dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855901#M338213</link>
      <description>&lt;P&gt;&lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;Hello,&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;where do I add it ?&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;Can you elaborate more please?&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;For example by giving an example of code.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;Thanks.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="jCAhz ChMk0b"&gt;&lt;SPAN class="ryNqvb"&gt;Gick&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jan 2023 08:01:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855901#M338213</guid>
      <dc:creator>Gick</dc:creator>
      <dc:date>2023-01-27T08:01:02Z</dc:date>
    </item>
    <item>
      <title>Re: Import an Excel file containing dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855905#M338214</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;common source of issue when data providers do not use a suitable database system.&lt;/P&gt;
&lt;P&gt;Either you proceed as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;said, converting to *.csv where format are kept&lt;/P&gt;
&lt;P&gt;or you import as you did and perform the correct date conversion afterwards.&lt;/P&gt;
&lt;P&gt;Excel do not use the same start time reference value as SAS, so you have to convert by yourself.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;YOUR_DTC=put(input(XLSX_COLUMN_NAME,best32.)-21916,e8601da.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;so you get "2023-01-23"&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jan 2023 08:05:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855905#M338214</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2023-01-27T08:05:07Z</dc:date>
    </item>
    <item>
      <title>Re: Import an Excel file containing dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855908#M338215</link>
      <description>it does not work. Here is the message I get in the log&lt;BR /&gt;&lt;BR /&gt;ERROR: Variable date has been defined as both character and numeric.</description>
      <pubDate>Fri, 27 Jan 2023 08:27:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855908#M338215</guid>
      <dc:creator>Gick</dc:creator>
      <dc:date>2023-01-27T08:27:32Z</dc:date>
    </item>
    <item>
      <title>Re: Import an Excel file containing dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855912#M338216</link>
      <description>&lt;P&gt;In order to supply working code, we need usable example data so we know the names and attributes of your variables. Post example data as a working DATA step with DATALINES, so we can easily replicate your data in our environments for testing.&lt;/P&gt;
&lt;P&gt;The ERROR indicates that you either try to convert "in place", which is not possible, or you already have a variable called date with the wrong type.&lt;/P&gt;
&lt;P&gt;In the future, whenever you get WARNINGs or ERRORs, or have other issues with your code, post the complete (all code and all messages) log by copy/pasting into a window opened with this button:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bildschirmfoto 2020-04-07 um 08.32.59.jpg" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/54552i914D97BE1B0F21E5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Bildschirmfoto 2020-04-07 um 08.32.59.jpg" alt="Bildschirmfoto 2020-04-07 um 08.32.59.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jan 2023 10:46:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855912#M338216</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-01-27T10:46:00Z</dc:date>
    </item>
    <item>
      <title>Re: Import an Excel file containing dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855942#M338228</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/416124"&gt;@Gick&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hello,&lt;BR /&gt;&lt;BR /&gt;where do I add it?&lt;BR /&gt;Can you elaborate more please? For example by giving an example of code.&lt;BR /&gt;&lt;BR /&gt;Thanks&lt;BR /&gt;Gick&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The link I gave has oodles of examples of actual SAS code to do this.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jan 2023 13:11:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855942#M338228</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-01-27T13:11:41Z</dc:date>
    </item>
    <item>
      <title>Re: Import an Excel file containing dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855945#M338230</link>
      <description>&lt;P&gt;if you already have a numeric value use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;YOUR_DTC=put(XLSX_COLUMN_NAME-21916,e8601da.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Difficult to say without knowing your data&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jan 2023 13:28:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855945#M338230</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2023-01-27T13:28:07Z</dc:date>
    </item>
    <item>
      <title>Re: Import an Excel file containing dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855960#M338234</link>
      <description>&lt;P&gt;Find below a simple example using the Excel date Values you had in the initial post&lt;BR /&gt;&lt;BR /&gt;A couple of things to note:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Excel stores dates as a number e.g. 1 = 01 Jan 1990, 2 = 02 Jan 1990, ...&lt;/LI&gt;
&lt;LI&gt;Excel doesn't appear to handle dates prior to 1900 (I'm no Excel expert, just an observation when looking into this post)&lt;/LI&gt;
&lt;LI&gt;SAS stores dates as the number of days since 01 Jan 1960 e.g. 0 = 01 Jan 1960, 1 = 02 Jan 1960, ...&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Once you know this it's a relatively simple task to convert Excel date values to SAS date values, you just need to know the number of days to subtract from the Excel date value to convert it to a SAS Date Value. Knowing that 01 Jan 1960 is represented as 21,916 in Excel and 0 in SAS tells us to subtract 21,916 from the Excel Date Value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Simulated date values from Excel */
data have ;
	infile cards ;
	input excelDateValue ;
cards ;
44582
43526
40330
21916
1
;

/* Read the simulated date values and conver to SAS Date values */
data want ;
	format sasDateValue date7. ;
	set have ;
	sasDateValue=excelDateValue-21916 ;
	put sasDateValue= 8. sasDateValue= ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jan 2023 14:24:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855960#M338234</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2023-01-27T14:24:51Z</dc:date>
    </item>
    <item>
      <title>Re: Import an Excel file containing dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855972#M338238</link>
      <description>&lt;P&gt;Do not use &lt;A href="https://en.wikipedia.org/wiki/Magic_number_(programming)" target="_self"&gt;magic numbers in programming&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The number used in your code posted is 30DEC1899 .&lt;/P&gt;
&lt;PRE&gt;   1  %put %sysfunc(putn(-21916,date9));
30DEC1899&lt;/PRE&gt;
&lt;P&gt;So just use the actual date in the code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;sasDateValue=excelDateValue+'30DEC1899'd ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also you left out of your explanation for the reason why you want to use the 30th instead of the 31st.&amp;nbsp; It is because Excel mimics the decision made for Lotus 1-2-3 to treat the year 1900 as a leap year which causes the difference to be off by one day (at least for dates from 01MAR1900 on).&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1674831698700.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/79877i1BA457A08132CF66/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1674831698700.png" alt="Tom_0-1674831698700.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jan 2023 15:02:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855972#M338238</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-01-27T15:02:26Z</dc:date>
    </item>
    <item>
      <title>Re: Import an Excel file containing dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855973#M338239</link>
      <description>&lt;P&gt;Excel does not work with dates prior to 1900 because it perpetuates a bug (actually, a trick to speed up calculations) it inherited from Lotus 1-2-3. It considers 1900 a leap year, which it was not. So all dates before March 1, 1900 are calculated wrongly.&lt;/P&gt;
&lt;P&gt;This is also the reason why the correction value is 30dec1899 and not 31dec1899.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Other office software (e.g. LibreOffice) has this corrected and does therefore work with dates prior to 1900.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jan 2023 15:02:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/855973#M338239</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-01-27T15:02:59Z</dc:date>
    </item>
    <item>
      <title>Re: Import an Excel file containing dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/856055#M338261</link>
      <description>&lt;P&gt;I suggest you try the EXCEL engine rather than XLSX:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc import datafile="I:\DOC\Questionnaires.xlsx" 
           dbms=EXCEL		   
           out=data replace ;
	mixed=yes;
     	   scantext=yes;
run;  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If that doesn't help then try saving the column type in Excel as "Date" or "Short Date" instead of the default "General". Then rerun the above code.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jan 2023 21:26:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/856055#M338261</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2023-01-27T21:26:54Z</dc:date>
    </item>
    <item>
      <title>Re: Import an Excel file containing dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/856087#M338280</link>
      <description>&lt;P&gt;Excel is NOT a database and for this reason you can't rely on getting a consistent result when importing into SAS.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you can at least rely on the structure of the Excel source - like in the first column should always be a string for a specific variable - then below an approach I'm using sometimes.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's reading the Excel with the header row included as data so the resulting SAS variables always become type character and then mapping these initial columns to what I really need.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* read Excel headers as data so all variables become character */
%let path=&amp;lt;some path&amp;gt;;
%let xlsx_file=&amp;lt;excel workbook name&amp;gt;;
proc import 
  file="&amp;amp;path/&amp;amp;xlsx_file"
  out=work._xl_src
  dbms=xlsx
  replace
  ;
  datarow=1;
  getnames=no;
run;

/* map initial variables to desired variable */
data work._dq_&amp;amp;p1._&amp;amp;target_tbl.;
  set work._xl_src(firstobs=2);
  drop a-&amp;lt;last column from Excel import&amp;gt;;

  /* map source to target columns */
  want_var_1            = input(A, ?? 32.   );
  want_var_2            = input(B, ?? $30.  );
  ...and so on...
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 28 Jan 2023 10:58:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-an-Excel-file-containing-dates/m-p/856087#M338280</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-01-28T10:58:00Z</dc:date>
    </item>
  </channel>
</rss>

