<?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: Specify format for date fields exporting to CSV in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Specify-format-for-date-fields-exporting-to-CSV/m-p/461801#M284834</link>
    <description>&lt;P&gt;Thank you Kurt.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That would work well if I was trying to tailor to one specific file format, but the reality is that I need a generic SAS script that can export any kind of SAS dataset to CSV (without knowing which fields it contains)... The script above works very well for what I need, with the exception of date fields, which is exported as integers - and therefore requires the receiving application of the CSV to "know" which fields are date/time coming from SAS, and convert them appropriately.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is it possible to have more control over the export process, so that I can have something like (pseudo-code):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if (field is date) then format="yyyy-MM-dd";&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 12 May 2018 20:08:56 GMT</pubDate>
    <dc:creator>the_yeti</dc:creator>
    <dc:date>2018-05-12T20:08:56Z</dc:date>
    <item>
      <title>Specify format for date fields exporting to CSV</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Specify-format-for-date-fields-exporting-to-CSV/m-p/461782#M284831</link>
      <description>&lt;P&gt;I'm trying to export a SAS dataset (.sas7bdat) to CSV via SAS, and I came up the code below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;    libname xpto '/project/input/';
    
    data My_Data;
       set xpto.myinputfile;
       format _ALL_;
    run;
    
    proc export data=My_Data
       outfile='/project/output/output.csv'
       dbms=csv
       replace;
    run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This works and the CSV file is generated. I'm using `format _ALL_` on purpose because I want to remove all formats.&lt;/P&gt;&lt;P&gt;However, on the case of date fields, I'd like to specify an output format, such as `yyyy-MM-dd HH:mm:ss` for example, because with the code above it exports the date fields as integers.&lt;/P&gt;</description>
      <pubDate>Sat, 12 May 2018 17:56:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Specify-format-for-date-fields-exporting-to-CSV/m-p/461782#M284831</guid>
      <dc:creator>the_yeti</dc:creator>
      <dc:date>2018-05-12T17:56:19Z</dc:date>
    </item>
    <item>
      <title>Re: Specify format for date fields exporting to CSV</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Specify-format-for-date-fields-exporting-to-CSV/m-p/461789#M284832</link>
      <description>&lt;P&gt;Keep the formats (no format _all_; statement), and run the proc export once. Copy the export data step from the log and adapt it to your needs. Use that custom data step from then on.&lt;/P&gt;</description>
      <pubDate>Sat, 12 May 2018 19:15:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Specify-format-for-date-fields-exporting-to-CSV/m-p/461789#M284832</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-05-12T19:15:50Z</dc:date>
    </item>
    <item>
      <title>Re: Specify format for date fields exporting to CSV</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Specify-format-for-date-fields-exporting-to-CSV/m-p/461798#M284833</link>
      <description>&lt;P&gt;I don't know if the following is fool proof, but it worked for my test data. It is an attempt to find all variables that don't have a date or datetime format assigned and remove the formats (if any) from those variables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  informat date date9.;
  informat datetime datetime.;
  format date mmddyy10.;
  format datetime datetime.;
  format number best32.;
  input name $ year date datetime number;
  cards;
John 2018 12May2018 12May2018:14:39:00 12557
Mary 2017 6Jun2017 6Jun2017:14:39:00 20192
;

proc sql;
    select name into :deformat
      separated by ' '
        from dictionary.columns
          where libname='WORK' and
                memname='HAVE' and
                (type eq 'char' or
                 format eq '' or
                 sum(missing(input(putn(21071,format),mmddyy10.)),
                     missing(input(putn(1820534400,format),datetime.))) eq 2 or
                 (format ne '' and type eq 'num' and
                  not(year(input(putn(21071,format),mmddyy10.)) eq 2017 or
                  year(datepart(input(putn(1820534400,format),datetime.)) eq 2017))))
  ;
quit;

data My_Data;
  set have;
  format &amp;amp;deformat.;
run;
    
proc export data=My_Data
  outfile='/folders/myfolders/output.csv'
  dbms=csv
  replace;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 12 May 2018 20:47:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Specify-format-for-date-fields-exporting-to-CSV/m-p/461798#M284833</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2018-05-12T20:47:46Z</dc:date>
    </item>
    <item>
      <title>Re: Specify format for date fields exporting to CSV</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Specify-format-for-date-fields-exporting-to-CSV/m-p/461801#M284834</link>
      <description>&lt;P&gt;Thank you Kurt.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That would work well if I was trying to tailor to one specific file format, but the reality is that I need a generic SAS script that can export any kind of SAS dataset to CSV (without knowing which fields it contains)... The script above works very well for what I need, with the exception of date fields, which is exported as integers - and therefore requires the receiving application of the CSV to "know" which fields are date/time coming from SAS, and convert them appropriately.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is it possible to have more control over the export process, so that I can have something like (pseudo-code):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if (field is date) then format="yyyy-MM-dd";&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 12 May 2018 20:08:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Specify-format-for-date-fields-exporting-to-CSV/m-p/461801#M284834</guid>
      <dc:creator>the_yeti</dc:creator>
      <dc:date>2018-05-12T20:08:56Z</dc:date>
    </item>
    <item>
      <title>Re: Specify format for date fields exporting to CSV</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Specify-format-for-date-fields-exporting-to-CSV/m-p/461804#M284835</link>
      <description>&lt;P&gt;Writing a custom data step is a very minor task compared to writing the documentation that accompanies the file anyway. At least in my experience.&lt;/P&gt;</description>
      <pubDate>Sat, 12 May 2018 20:52:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Specify-format-for-date-fields-exporting-to-CSV/m-p/461804#M284835</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-05-12T20:52:26Z</dc:date>
    </item>
    <item>
      <title>Re: Specify format for date fields exporting to CSV</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Specify-format-for-date-fields-exporting-to-CSV/m-p/461806#M284836</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/189301"&gt;@the_yeti&lt;/a&gt;: Have you tried the code I suggested? I think it will do what you want for all date and datetime fields with the exception of&amp;nbsp;user defined formats that use a picture format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 12 May 2018 21:56:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Specify-format-for-date-fields-exporting-to-CSV/m-p/461806#M284836</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2018-05-12T21:56:04Z</dc:date>
    </item>
  </channel>
</rss>

