<?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 importing CSV file with different datetime format in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/importing-CSV-file-with-different-datetime-format/m-p/404136#M98240</link>
    <description>&lt;P&gt;Ok, I thought I would solve this easily, but after trying various methods im still not getting it. I use the following code to import csv file which has 6 date&amp;nbsp;fields in original date time format stored as character&amp;nbsp;- Some of the values are missing and denoted by 'NULL'. The date values look like this&lt;/P&gt;
&lt;TABLE width="287"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="287"&gt;2017-10-31 00:00:00.000&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA chpModel;
    INFILE "C:\Desktop\CHP model results\CHP model results 12 Oct.csv" 
    DELIMITER = "," 
    MISSOVER DSD 
    FIRSTOBS=2 
    LRECL=32767;
Length acctno $50	subsno $50	phaseid $50	AcctnoActiveMonth $50 PhaseActiveMonth $50	PhaseStart $50	Phaseend $50	LastmonthBenpkg $20	TotalPremium	Totalpayment	Balance	AR	DeferredRevenue	Grace_period_Term $50	Financial_term $50
;
    INPUT  acctno $	subsno $	phaseid $	AcctnoActiveMonth  $	PhaseActiveMonth $	PhaseStart $	Phaseend $	LastmonthBenpkg $	TotalPremium	Totalpayment	Balance	AR	DeferredRevenue	Grace_period_Term $	Financial_term $
;
format AcctnoActiveMonth mmddyy10 PhaseActiveMonth mmddyy10	PhaseStart mmddyy10	Phaseend mmddyy10 ;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The file is imported fine but the formatting stays the same as original. I also tried&amp;nbsp;importing dates as numeric, but the same result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I know one of the things I can do is converting to date from character once the import is done. But for one, it&amp;nbsp;will change&amp;nbsp;the sequence of the fields and second, with the formatting I have, I was having problem even doing that. I want the final dates to look like&lt;/P&gt;
&lt;TABLE width="127"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="127"&gt;10/31/2017&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
    <pubDate>Fri, 13 Oct 2017 20:53:00 GMT</pubDate>
    <dc:creator>devsas</dc:creator>
    <dc:date>2017-10-13T20:53:00Z</dc:date>
    <item>
      <title>importing CSV file with different datetime format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/importing-CSV-file-with-different-datetime-format/m-p/404136#M98240</link>
      <description>&lt;P&gt;Ok, I thought I would solve this easily, but after trying various methods im still not getting it. I use the following code to import csv file which has 6 date&amp;nbsp;fields in original date time format stored as character&amp;nbsp;- Some of the values are missing and denoted by 'NULL'. The date values look like this&lt;/P&gt;
&lt;TABLE width="287"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="287"&gt;2017-10-31 00:00:00.000&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA chpModel;
    INFILE "C:\Desktop\CHP model results\CHP model results 12 Oct.csv" 
    DELIMITER = "," 
    MISSOVER DSD 
    FIRSTOBS=2 
    LRECL=32767;
Length acctno $50	subsno $50	phaseid $50	AcctnoActiveMonth $50 PhaseActiveMonth $50	PhaseStart $50	Phaseend $50	LastmonthBenpkg $20	TotalPremium	Totalpayment	Balance	AR	DeferredRevenue	Grace_period_Term $50	Financial_term $50
;
    INPUT  acctno $	subsno $	phaseid $	AcctnoActiveMonth  $	PhaseActiveMonth $	PhaseStart $	Phaseend $	LastmonthBenpkg $	TotalPremium	Totalpayment	Balance	AR	DeferredRevenue	Grace_period_Term $	Financial_term $
;
format AcctnoActiveMonth mmddyy10 PhaseActiveMonth mmddyy10	PhaseStart mmddyy10	Phaseend mmddyy10 ;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The file is imported fine but the formatting stays the same as original. I also tried&amp;nbsp;importing dates as numeric, but the same result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I know one of the things I can do is converting to date from character once the import is done. But for one, it&amp;nbsp;will change&amp;nbsp;the sequence of the fields and second, with the formatting I have, I was having problem even doing that. I want the final dates to look like&lt;/P&gt;
&lt;TABLE width="127"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="127"&gt;10/31/2017&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Fri, 13 Oct 2017 20:53:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/importing-CSV-file-with-different-datetime-format/m-p/404136#M98240</guid>
      <dc:creator>devsas</dc:creator>
      <dc:date>2017-10-13T20:53:00Z</dc:date>
    </item>
    <item>
      <title>Re: importing CSV file with different datetime format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/importing-CSV-file-with-different-datetime-format/m-p/404143#M98241</link>
      <description>&lt;P&gt;1) Do not import as character&lt;/P&gt;
&lt;P&gt;2) Use an informat to read the data directly into a SAS Date or datetime variable.&lt;/P&gt;
&lt;P&gt;Maybe something like this BEFORE the INPUT statement:&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#ff0000" face="SAS Monospace" size="2"&gt;informat&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; AcctnoActiveMonth PhaseActiveMonth PhaseStart Phaseend &lt;/FONT&gt;&lt;FONT color="#804040" face="SAS Monospace" size="2"&gt;anydtdtm.&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; ;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3) If you do not want the time portion then you may have to read as datetime and then convert to date.&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;AcctnoActiveMonth &lt;/FONT&gt;= datepart(&lt;FONT face="SAS Monospace" size="2"&gt;AcctnoActiveMonth &lt;/FONT&gt;); repeat for each variable.&lt;/P&gt;
&lt;P&gt;4) correctly assign the date format wanted:&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;format&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; AcctnoActiveMonth &lt;/FONT&gt;&lt;FONT color="#804040" face="SAS Monospace" size="2"&gt;mmddyy10.&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; PhaseActiveMonth &lt;/FONT&gt;&lt;FONT color="#804040" face="SAS Monospace" size="2"&gt;mmddyy10.&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; PhaseStart &lt;/FONT&gt;&lt;FONT color="#804040" face="SAS Monospace" size="2"&gt;mmddyy10.&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; Phaseend &lt;/FONT&gt;&lt;FONT color="#804040" face="SAS Monospace" size="2"&gt;mmddyy10.&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; ;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;Actually you only need the format once at the end of list of variables that use the same format. The . is part of the format named and you did not show any of the error messages your code generated because of this coding error. The periods are needed because mmddyy10 is a valid SAS variable name.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;Date, time and Datetime formats only apply to numeric values. When you read as text as in your example you would need to create a new variable to hold the numeric value and apply the format to.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;This will generate some "invalid data" messages for the null values, but since they aren't dates and the variables will have a missing value that's basically what you want.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Oct 2017 21:10:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/importing-CSV-file-with-different-datetime-format/m-p/404143#M98241</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-10-13T21:10:18Z</dc:date>
    </item>
    <item>
      <title>Re: importing CSV file with different datetime format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/importing-CSV-file-with-different-datetime-format/m-p/404284#M98274</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/40777"&gt;@devsas&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Would something like below do?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options datestyle=dmy;
data demo;
  infile datalines truncover dlm=',' dsd;
  input dttm ??:anydtdtm.;
  format dttm datetime23.3;
  datalines;
01jan2017 10:15:20
.
 
NULL
02jan2017 20:25:35
2017-10-31 00:00:00.000
;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or if you don't want to use the ?? argument so that you still get warnings for unexpected data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
  invalue inDTTM (default=23)
    'NULL' =.
    other=[anydtdtm.]
  ;
run;

options datestyle=dmy;
data demo;
  infile datalines truncover dlm=',' dsd;
  input dttm :inDTTM.;
  format dttm datetime23.3;
  datalines;
01jan2017 10:15:20
.
 
NULL
02jan2017 20:25:35
2017-10-31 00:00:00.000
;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Oct 2017 09:47:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/importing-CSV-file-with-different-datetime-format/m-p/404284#M98274</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-10-15T09:47:07Z</dc:date>
    </item>
  </channel>
</rss>

