<?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 datepart(date) as monthyear error in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805474#M317300</link>
    <description>&lt;P&gt;show your sample data please and code.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 01 Apr 2022 10:47:14 GMT</pubDate>
    <dc:creator>tarheel13</dc:creator>
    <dc:date>2022-04-01T10:47:14Z</dc:date>
    <item>
      <title>proc sql datepart(date) as monthyear error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805470#M317297</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;I have a datetime variable and would like to use datepart(datetime) as monthyear and applied as monyy7. format in proc sql.&amp;nbsp;&lt;/P&gt;&lt;P&gt;in the second step I was doing grouping and where clause on monthyear &amp;gt;012022&lt;/P&gt;&lt;P&gt;but not working.&amp;nbsp; It works only &amp;gt;'01jan2022'd&amp;nbsp;&lt;/P&gt;&lt;P&gt;also my grouping giving by date wise when i check the data so what i want by group by monthyear .&lt;/P&gt;&lt;P&gt;I thought it would group by monthyear but internally it store full value of date(01jan2022) . so when we do group by monthyear then my count values coming by date not by monthyear.&lt;/P&gt;&lt;P&gt;note:&amp;nbsp; I tried like month(datepart(date)) as month and year(datepart(date)) as year&lt;/P&gt;&lt;P&gt;and combined as cat(month,year) as monthyear but it will be converted to character, cant use in where statement &amp;gt; &amp;lt; symbols (where monthyear&amp;gt;012022 not working as well.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to do it in proc sql only this grouping. appreciate your thoughts pls.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Apr 2022 10:02:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805470#M317297</guid>
      <dc:creator>vallsas</dc:creator>
      <dc:date>2022-04-01T10:02:45Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql datepart(date) as monthyear error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805474#M317300</link>
      <description>&lt;P&gt;show your sample data please and code.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Apr 2022 10:47:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805474#M317300</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2022-04-01T10:47:14Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql datepart(date) as monthyear error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805479#M317304</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/65449"&gt;@vallsas&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let me first create sample data to have something to work with:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input datetime :datetime. x;
format datetime datetime19.;
cards;
05JAN2022:12:34:56 8
31JAN2022:13:45:00 2
05FEB2022:12:34:56 7
28FEB2022:13:45:00 5
05MAR2022:12:34:56 3
28MAR2022:13:45:00 4
05APR2022:12:34:56 6
28APR2022:13:45:00 9
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;From this HAVE dataset you want to create an aggregated table with a grouping variable MONTHYEAR by means of PROC SQL? Then MONTHYEAR must have the &lt;EM&gt;same&lt;/EM&gt; value for all datetimes in a month. Hence &lt;FONT face="courier new,courier"&gt;datepart(datetime)&lt;/FONT&gt; is not suitable, even if formatted with the &lt;FONT face="courier new,courier"&gt;MONYY7.&lt;/FONT&gt; format, because the internal values, not the formatted values would be used to group observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you could map all datetimes to the first day of the month:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select intnx('month',datepart(datetime),0) format=monyy7. as monthyear, sum(x) as s
from have
group by monthyear
having monthyear&amp;gt;'01JAN2022'd;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;monthyear         s
-------------------
  FEB2022        12
  MAR2022         7
  APR2022        15
&lt;/PRE&gt;
&lt;P&gt;Note that the rows are sorted in chronological order, as they should, because the internal values of MONTHYEAR are SAS date values. This is also the reason why the HAVING clause uses a date literal.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Apr 2022 11:28:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805479#M317304</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-04-01T11:28:11Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql datepart(date) as monthyear error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805480#M317305</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table new as select a.id, datepart(datetime) as monthyear format=monyy7.,&lt;/P&gt;&lt;P&gt;b.id,b.age,b.cti_id&lt;/P&gt;&lt;P&gt;from T1TAB LEFT JOIN T2TAB&lt;/P&gt;&lt;P&gt;ON a.id=b.id&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table new1 as select monthyear,cti_id,count(distinct id) as countid, count(distinct age) as coutage from new&lt;/P&gt;&lt;P&gt;where monthyear &amp;gt;'01jan2022'd&lt;/P&gt;&lt;P&gt;group by monthyear,cti_id;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;here results coming&amp;nbsp;&lt;/P&gt;&lt;P&gt;jan2022 but it wasnot grouping by monthyear when i see the data i was grouping by date .&lt;/P&gt;&lt;P&gt;so i get more number of observation as summary of count.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Apr 2022 11:26:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805480#M317305</guid>
      <dc:creator>vallsas</dc:creator>
      <dc:date>2022-04-01T11:26:41Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql datepart(date) as monthyear error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805482#M317306</link>
      <description>&lt;P&gt;So in your second PROC SQL step you would select&amp;nbsp;&lt;FONT face="courier new,courier"&gt;intnx('month',monthyear,0) format=monyy7.&lt;/FONT&gt; either as a new variable (e.g. &lt;FONT face="courier new,courier"&gt;monthyear1&lt;/FONT&gt;) or again as &lt;FONT face="courier new,courier"&gt;monthyear&lt;/FONT&gt;. In the latter case you would need to refer to &lt;FONT face="courier new,courier"&gt;&lt;EM&gt;calculated&lt;/EM&gt; monthyear&lt;/FONT&gt; in the GROUP BY clause. The HAVING clause would replace the WHERE clause:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;group by calculated monthyear, cti_id
having monthyear&amp;gt;'01JAN2022'd;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Edit: Or adapt the WHERE clause to the fact that it uses the internal values of the original &lt;FONT face="courier new,courier"&gt;monthyear&lt;/FONT&gt; variable:&lt;/P&gt;
&lt;PRE&gt;where monthyear&amp;gt;'&lt;FONT color="#FF0000"&gt;31&lt;/FONT&gt;JAN2022'd
group by calculated monthyear, cti_id;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 Apr 2022 12:00:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805482#M317306</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-04-01T12:00:08Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql datepart(date) as monthyear error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805487#M317311</link>
      <description>thank you so very much, i wll test it on monday and reply .&lt;BR /&gt;</description>
      <pubDate>Fri, 01 Apr 2022 12:34:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805487#M317311</guid>
      <dc:creator>vallsas</dc:creator>
      <dc:date>2022-04-01T12:34:55Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql datepart(date) as monthyear error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805490#M317314</link>
      <description>&lt;P&gt;Other than the statistical procedures like FREQ or MEANS, SQL GROUP BY will use the raw, unformatted values. So you need to create the new variable either as character with the formatted values, or use INTNX to align the dates to the first of the month.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Apr 2022 12:39:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805490#M317314</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-04-01T12:39:04Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql datepart(date) as monthyear error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805620#M317354</link>
      <description>Hi&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;please check the error.&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;proc sql;&lt;BR /&gt;&amp;nbsp;select intnx('month',datepart(datetime),0) format=monyy7. as monthyear,count(distinct x) as s&lt;BR /&gt;&amp;nbsp;from have&lt;BR /&gt;&amp;nbsp;where monthyear&amp;gt;'31JAN2022'd&lt;BR /&gt;group by calculated monthyear, x;&lt;BR /&gt;ERROR: The following columns were not found in the contributing tables: monthyear.</description>
      <pubDate>Sat, 02 Apr 2022 12:33:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805620#M317354</guid>
      <dc:creator>vallsas</dc:creator>
      <dc:date>2022-04-02T12:33:54Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql datepart(date) as monthyear error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805623#M317356</link>
      <description>&lt;P&gt;If variable MONTHYEAR is not contained in the input dataset HAVE, but just being created in the current SELECT statement, you need to write &lt;FONT face="courier new,courier"&gt;&lt;EM&gt;calculated&lt;/EM&gt; monthyear&lt;/FONT&gt;&amp;nbsp;in a WHERE clause (as you did redundantly in the GROUP BY clause). Alternatively, you can use the HAVING clause (but the WHERE clause should be more efficient). My suggested code with "&lt;FONT face="courier new,courier"&gt;where monthyear&lt;/FONT&gt; ..." referred to your second PROC SQL step where MONTHYEAR had already been created in the preceding step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, are you sure that you want to include &lt;FONT face="courier new,courier"&gt;x&lt;/FONT&gt; in the GROUP BY clause when your summary statistic is&amp;nbsp;&lt;FONT face="courier new,courier"&gt;count(distinct x)&lt;/FONT&gt;?&lt;/P&gt;</description>
      <pubDate>Sat, 02 Apr 2022 13:09:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805623#M317356</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-04-02T13:09:01Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql datepart(date) as monthyear error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805629#M317361</link>
      <description>HI Thankyou.&lt;BR /&gt;here is the code i executed two steps separtely but in the second step got error as I would like to check count by distinct x with monthyear &amp;gt;jan2022 (here we cant pass as monthyear as jan2022 so we put 31jan2022,&lt;BR /&gt;but data should full by monthyear all the feb count should be 2, march 2,april 2 and by monthyear.&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table tt as&lt;BR /&gt;select intnx('month',datepart(datetime),0) format=monyy7. as monthyear, x&lt;BR /&gt;from have;&lt;BR /&gt;&lt;BR /&gt;quit;&lt;BR /&gt;MONTHYEAR X&lt;BR /&gt;1 JAN2022 8&lt;BR /&gt;2 JAN2022 2&lt;BR /&gt;3 FEB2022 7&lt;BR /&gt;4 FEB2022 5&lt;BR /&gt;5 MAR2022 3&lt;BR /&gt;6 MAR2022 4&lt;BR /&gt;7 APR2022 6&lt;BR /&gt;8 APR2022 9&lt;BR /&gt;&lt;BR /&gt;69 proc sql;&lt;BR /&gt;70 select intnx('month',monthyear,0) format=monyy7. as monthyear,count(distinct x) as s&lt;BR /&gt;71 from tt&lt;BR /&gt;72 where monthyear&amp;gt;'31JAN2022'd&lt;BR /&gt;73 group by calculated monthyear, S;&lt;BR /&gt;ERROR: Summary functions are restricted to the SELECT and HAVING clauses only.</description>
      <pubDate>Sat, 02 Apr 2022 16:10:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805629#M317361</guid>
      <dc:creator>vallsas</dc:creator>
      <dc:date>2022-04-02T16:10:36Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql datepart(date) as monthyear error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805630#M317362</link>
      <description>&lt;P&gt;Since the group determines the result of a summary function, you cannot use this result to determine the group. You created a circular definition.&lt;/P&gt;
&lt;P&gt;Please provide example data (as shown, in a data step with datalines), and what you want to get out of it.&lt;/P&gt;</description>
      <pubDate>Sat, 02 Apr 2022 16:22:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805630#M317362</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-04-02T16:22:09Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql datepart(date) as monthyear error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805632#M317363</link>
      <description>&lt;P&gt;Just omit S in the GROUP BY clause.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using the HAVE dataset from my earlier post I assume you have created something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table tt as
select datepart(datetime) format=monyy7. as monthyear, x
from have;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you can count the distinct values of &lt;FONT face="courier new,courier"&gt;x&lt;/FONT&gt; per month:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select intnx('month',monthyear,0) format=monyy7. as monthyear, count(distinct x) as s
from tt
where monthyear&amp;gt;'31JAN2022'd
group by calculated monthyear;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;monthyear         s
-------------------
  FEB2022         2
  MAR2022         2
  APR2022         2&lt;/PRE&gt;</description>
      <pubDate>Sat, 02 Apr 2022 17:19:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805632#M317363</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-04-02T17:19:50Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql datepart(date) as monthyear error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805634#M317365</link>
      <description>Yes, now got the expected output. only thing is here we have to do in two steps on month ..&lt;BR /&gt;you have give genius solution as most of them think to get on the same step of first but here by creating the second step we have got expected output. even group by gives unformatted values but now we overcome that and got the result by using this method.&lt;BR /&gt;excellent well appreciated your effort.&lt;BR /&gt;thank you.</description>
      <pubDate>Sat, 02 Apr 2022 18:01:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805634#M317365</guid>
      <dc:creator>vallsas</dc:creator>
      <dc:date>2022-04-02T18:01:10Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql datepart(date) as monthyear error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805635#M317366</link>
      <description>&lt;P&gt;As shown in my first reply (just for a different summary function) you can get the result from HAVE also in one step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a variant of that using a WHERE clause instead of a HAVING clause:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select intnx('month',datepart(datetime),0) format=monyy7. as monthyear, count(distinct x) as s
from have
where datetime&amp;gt;='01FEB2022:0:0'dt
group by monthyear;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 02 Apr 2022 18:14:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-datepart-date-as-monthyear-error/m-p/805635#M317366</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-04-02T18:14:05Z</dc:date>
    </item>
  </channel>
</rss>

