<?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: proc sql table - how to group DATETIME20 format into month or year in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-table-how-to-group-DATETIME20-format-into-month-or-year/m-p/579165#M164377</link>
    <description>&lt;P&gt;You can use the YEAR() and DATEPART() functions to generate a numeric variable with the YEAR from your DATETIME value.&lt;/P&gt;
&lt;P&gt;For the MONTH level data you either create a date value (or datetime value if you want) that uses a constant day of the month, like the first of the month.&amp;nbsp; Or create a character variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table date_by_segment as
  select 
         A.ID
       , B.segment
       , A.DATE_OPEN
       , year(datepart(A.DATE_OPEN)) as YEAR
       , intnx('dtmonth',A.DATE_OPEN,0,'b') as MONTH_DATETIME format=datetime20.
       , put(A.DATE_OPEN,dtyear4.) as YEAR_CHAR
       , put(A.DATE_OPEN,dtmonyy7.) as MONTH_CHAR
   from ac.dataset1 as A
   left join ac.dataset2 as B
   on a.id = B.id 
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 05 Aug 2019 15:57:45 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-08-05T15:57:45Z</dc:date>
    <item>
      <title>proc sql table - how to group DATETIME20 format into month or year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-table-how-to-group-DATETIME20-format-into-month-or-year/m-p/579154#M164369</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have created a proc sql table and want to output the results into Excel to create a pivot table - however, the DATETIME20 format means that it is difficult to interpret when the pivot table is created. Is there a way to make sure that the outputs are converted - would it be possible to edit my code to create two different versions:&lt;/P&gt;&lt;P&gt;1. By &lt;FONT color="#FF0000"&gt;month&lt;/FONT&gt; (so&amp;nbsp;14JUN2011:00:00:00 would become &lt;FONT color="#FF0000"&gt;JUN2011&lt;/FONT&gt; in the 'Output Data')&lt;/P&gt;&lt;P&gt;2. By &lt;FONT color="#FF0000"&gt;year&amp;nbsp;&lt;/FONT&gt;(so&amp;nbsp;14JUN2011:00:00:00 would become &lt;FONT color="#FF0000"&gt;2011&lt;/FONT&gt; in the 'Output Data')&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;Code&lt;/U&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; create table date_by_segment as&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; select &lt;FONT color="#FF0000"&gt;A.DATE_OPEN&lt;/FONT&gt;,&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;FONT color="#FF0000"&gt; /* How to do I edit the code to create the two versions (1. by month 2. by year)? */&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; A.ID,&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; B.segment&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;from ac.dataset1 as A&lt;BR /&gt;&amp;nbsp; &amp;nbsp;left join ac.dataset2 as B&lt;BR /&gt;&amp;nbsp; &amp;nbsp;on a.id = B.id&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="DATE_OPEN.JPG" style="width: 566px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/31534iFB873D9EC695C637/image-size/large?v=v2&amp;amp;px=999" role="button" title="DATE_OPEN.JPG" alt="DATE_OPEN.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Aug 2019 15:10:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-table-how-to-group-DATETIME20-format-into-month-or-year/m-p/579154#M164369</guid>
      <dc:creator>jeremy4</dc:creator>
      <dc:date>2019-08-05T15:10:10Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql table - how to group DATETIME20 format into month or year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-table-how-to-group-DATETIME20-format-into-month-or-year/m-p/579157#M164372</link>
      <description>&lt;P&gt;Easiest may be to create an actual DATE value:&amp;nbsp; datepart(date_open) as date&lt;/P&gt;
&lt;P&gt;Use the MONYY7. format display the month year and the Year4 format to display just the Year as needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use the appropriate format in a report procedure such as Report or Tabulate and don't worry so&amp;nbsp; much about the SQL "Group by".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The little bit I see of your example values you may actually have been bitten by one of the, IMHO, stupid Microsoft defaults of making date values into datetimes with all hour=0 minute=0 and second=0. If that is the case then definitely use datepart to create a date value instead of datetime.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Aug 2019 15:20:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-table-how-to-group-DATETIME20-format-into-month-or-year/m-p/579157#M164372</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-08-05T15:20:50Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql table - how to group DATETIME20 format into month or year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-table-how-to-group-DATETIME20-format-into-month-or-year/m-p/579165#M164377</link>
      <description>&lt;P&gt;You can use the YEAR() and DATEPART() functions to generate a numeric variable with the YEAR from your DATETIME value.&lt;/P&gt;
&lt;P&gt;For the MONTH level data you either create a date value (or datetime value if you want) that uses a constant day of the month, like the first of the month.&amp;nbsp; Or create a character variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table date_by_segment as
  select 
         A.ID
       , B.segment
       , A.DATE_OPEN
       , year(datepart(A.DATE_OPEN)) as YEAR
       , intnx('dtmonth',A.DATE_OPEN,0,'b') as MONTH_DATETIME format=datetime20.
       , put(A.DATE_OPEN,dtyear4.) as YEAR_CHAR
       , put(A.DATE_OPEN,dtmonyy7.) as MONTH_CHAR
   from ac.dataset1 as A
   left join ac.dataset2 as B
   on a.id = B.id 
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 05 Aug 2019 15:57:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-table-how-to-group-DATETIME20-format-into-month-or-year/m-p/579165#M164377</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-08-05T15:57:45Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql table - how to group DATETIME20 format into month or year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-table-how-to-group-DATETIME20-format-into-month-or-year/m-p/579201#M164389</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table date_by_segment as
select 
    A.DATE_OPEN,
    intnx("DTMONTH", A.DATE_OPEN, 0) as DATE_OPEN_MONTH, 
    intnx("DTYEAR", A.DATE_OPEN, 0) as DATE_OPEN_YEAR, 
    A.ID,
    B.segment
from 
    ac.dataset1 as A left join 
    ac.dataset2 as B on a.id = B.id 
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 05 Aug 2019 18:10:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-table-how-to-group-DATETIME20-format-into-month-or-year/m-p/579201#M164389</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-08-05T18:10:43Z</dc:date>
    </item>
  </channel>
</rss>

