<?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: How can PROC SQL return only results from the previous month in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774558#M39745</link>
    <description>&lt;P&gt;So it IS a datetime value, not a date. You need to use DATEPART to extract the date from it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input date_column datetime19.;
format date_column dtdate9.;
datalines;
01sep2021:01:02:03
01oct2021:04:05:06
;

proc contents data=have;
run;

proc sql;
select *
from have
where datepart(date_column) = intnx('month',today(),-1,'b');
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Partial result:&lt;/P&gt;
&lt;PRE&gt;#	Variable	Typ	Länge	Ausg.Format
1	date_column	Num	8	DTDATE9.

date_column
01SEP2021
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 15 Oct 2021 16:16:03 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2021-10-15T16:16:03Z</dc:date>
    <item>
      <title>How can PROC SQL return only results from the previous month</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774523#M39733</link>
      <description>Hi folks! I have a run that returns a column in datetime format like this:&lt;BR /&gt;&lt;BR /&gt;01SEP2021&lt;BR /&gt;01AUG2021&lt;BR /&gt;01JUL2021&lt;BR /&gt;&lt;BR /&gt;I’m trying to write a dynamic WHERE statement in Proc SQL that will only return the last month’s results. What I am trying is this:&lt;BR /&gt;&lt;BR /&gt;SELECT *&lt;BR /&gt;FROM table&lt;BR /&gt;WHERE date_column  = intnx(‘month’, today(), -1, ‘same’);&lt;BR /&gt;&lt;BR /&gt;It’s not giving me an error, but it is returning no results. It is currently October, so I was expecting it to return only the rows with 01SEP2021 as a value (and I am certain the results have SEP values).&lt;BR /&gt;&lt;BR /&gt;I am not sure where I am going wrong here and appreciate any advice. Thank you!</description>
      <pubDate>Fri, 15 Oct 2021 15:08:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774523#M39733</guid>
      <dc:creator>Boswser</dc:creator>
      <dc:date>2021-10-15T15:08:58Z</dc:date>
    </item>
    <item>
      <title>Re: How can PROC SQL return only results from the previous month</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774526#M39734</link>
      <description>&lt;P&gt;I'll take a guess that the values&amp;nbsp;&lt;SPAN&gt;01SEP2021 are not numeric (they are not datetime as you said, they are possibly dates if they are numeric).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If that's not it, then we would need to see a portion of your data as a SAS data step (&lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;instructions&lt;/A&gt;) and the full SQL code.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Oct 2021 15:11:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774526#M39734</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-10-15T15:11:44Z</dc:date>
    </item>
    <item>
      <title>Re: How can PROC SQL return only results from the previous month</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774530#M39735</link>
      <description>Thanks, maybe that is the issue.&lt;BR /&gt;&lt;BR /&gt;So earlier in the run, the dates are pulled from the DB, and converted into text because they are transposed with other data. Eventually they ended up on their own column as Character values.&lt;BR /&gt;&lt;BR /&gt;I assumed any kind of date function would not work with character values, so I converted them to a date value using this:&lt;BR /&gt;&lt;BR /&gt;input(date_column, anydtdtm.) as date_column format=dtdate9.&lt;BR /&gt;&lt;BR /&gt;However, I can undo this if there is a better way to get the previous month from a column constructed of character values like SEP2021, AUG2021, etc.</description>
      <pubDate>Fri, 15 Oct 2021 15:15:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774530#M39735</guid>
      <dc:creator>Boswser</dc:creator>
      <dc:date>2021-10-15T15:15:55Z</dc:date>
    </item>
    <item>
      <title>Re: How can PROC SQL return only results from the previous month</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774532#M39736</link>
      <description>&lt;P&gt;Always best to handle dates as numeric. However, I don't think you have explained enough for me to advise actual code. And, as I requested, it would be helpful if you provided a portion of your data in the requested format (and not in any other format).&lt;/P&gt;</description>
      <pubDate>Fri, 15 Oct 2021 15:18:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774532#M39736</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-10-15T15:18:41Z</dc:date>
    </item>
    <item>
      <title>Re: How can PROC SQL return only results from the previous month</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774535#M39737</link>
      <description>where date=put(intnx('month', today(), -1, 'B'), monyy5.); &lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://stackoverflow.com/questions/69576466/where-statement-in-sas-eg-for-getting-the-prior-month-of-a-dataset?noredirect=1#comment122997280_69576466" target="_blank"&gt;https://stackoverflow.com/questions/69576466/where-statement-in-sas-eg-for-getting-the-prior-month-of-a-dataset?noredirect=1#comment122997280_69576466&lt;/A&gt;</description>
      <pubDate>Fri, 15 Oct 2021 15:22:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774535#M39737</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-10-15T15:22:47Z</dc:date>
    </item>
    <item>
      <title>Re: How can PROC SQL return only results from the previous month</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774542#M39738</link>
      <description>&lt;P&gt;If you use this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;WHERE date_column = intnx(‘month’, today(), -1, ‘same’);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;today (2021-10-15), it will look for date_column values with a date of 2021-09-15, and not 2021-09-01. If you want the &lt;EM&gt;first&lt;/EM&gt; day of the previous month, use "b" as the fourth parameter of the INTNX function.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Oct 2021 15:37:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774542#M39738</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-10-15T15:37:45Z</dc:date>
    </item>
    <item>
      <title>Re: How can PROC SQL return only results from the previous month</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774545#M39739</link>
      <description>Thank you Reeza. I did not see your reply. I tried this where statement instead of the one in the OP, but received an error for the expression with = having components of different data types. I must have converted the character values poorly. Should I keep the dates as character values, or should they be converted some other way? Originally, they are character values reading SEP2021 AUG2021 etc.</description>
      <pubDate>Fri, 15 Oct 2021 15:45:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774545#M39739</guid>
      <dc:creator>Boswser</dc:creator>
      <dc:date>2021-10-15T15:45:07Z</dc:date>
    </item>
    <item>
      <title>Re: How can PROC SQL return only results from the previous month</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774546#M39740</link>
      <description>Thank you Kurt! I tried using B and unfortunately got the same result, which is no results returned. There must be a problem with the data type.</description>
      <pubDate>Fri, 15 Oct 2021 15:45:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774546#M39740</guid>
      <dc:creator>Boswser</dc:creator>
      <dc:date>2021-10-15T15:45:52Z</dc:date>
    </item>
    <item>
      <title>Re: How can PROC SQL return only results from the previous month</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774548#M39741</link>
      <description>That code is if for your character variable so it converts the comparison date to character as well.</description>
      <pubDate>Fri, 15 Oct 2021 15:47:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774548#M39741</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-10-15T15:47:56Z</dc:date>
    </item>
    <item>
      <title>Re: How can PROC SQL return only results from the previous month</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774553#M39742</link>
      <description>Thank you. I went back to a previous step in the project to work with the date_column as a character value, prior to its conversion. I kept it as a character and inserted that where statement. Unfortunately it also returned no results.&lt;BR /&gt;&lt;BR /&gt;In the previous iteration of the column, where the dates are character values, they do not include the day. So they are are SEP2021 and not 01SEP2021.&lt;BR /&gt;&lt;BR /&gt;Can them being MMMYYYY affect how the where statement needs to be constructed?</description>
      <pubDate>Fri, 15 Oct 2021 15:55:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774553#M39742</guid>
      <dc:creator>Boswser</dc:creator>
      <dc:date>2021-10-15T15:55:31Z</dc:date>
    </item>
    <item>
      <title>Re: How can PROC SQL return only results from the previous month</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774555#M39743</link>
      <description>&lt;P&gt;A date value is a date value, regardless of the format applied.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But we MUST now know what we are dealing with. Run PROC CONTENTS on your dataset, and post the line from the output that describes your date column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have the MONYY format in use, you will not see the actual dates; change the format to yymmdd10. and see what you get.&lt;/P&gt;
&lt;P&gt;Since you mentioned a datetime once in your initial post, also make sure that you do not have fractions involved; Use the FLOOR function to convert a possible date with fractions to an integer.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Oct 2021 16:00:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774555#M39743</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-10-15T16:00:43Z</dc:date>
    </item>
    <item>
      <title>Re: How can PROC SQL return only results from the previous month</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774557#M39744</link>
      <description>Thank you Kurt. I am pretty new to this!&lt;BR /&gt;&lt;BR /&gt;PROC CONTENTS on the earlier dataset (where the date is a character value and reads SEP2021) provided this on date_column:&lt;BR /&gt;&lt;BR /&gt;Type: Char&lt;BR /&gt;Len: 204&lt;BR /&gt;Format, Informat, and Label are blank. &lt;BR /&gt;&lt;BR /&gt;On the data where I converted it into (what I assumed was) a date format (01SEP2021), it reads:&lt;BR /&gt;&lt;BR /&gt;Type: Num&lt;BR /&gt;Len: 8&lt;BR /&gt;Format: DTDATE9.&lt;BR /&gt;Informat and Label are blank.</description>
      <pubDate>Fri, 15 Oct 2021 16:10:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774557#M39744</guid>
      <dc:creator>Boswser</dc:creator>
      <dc:date>2021-10-15T16:10:22Z</dc:date>
    </item>
    <item>
      <title>Re: How can PROC SQL return only results from the previous month</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774558#M39745</link>
      <description>&lt;P&gt;So it IS a datetime value, not a date. You need to use DATEPART to extract the date from it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input date_column datetime19.;
format date_column dtdate9.;
datalines;
01sep2021:01:02:03
01oct2021:04:05:06
;

proc contents data=have;
run;

proc sql;
select *
from have
where datepart(date_column) = intnx('month',today(),-1,'b');
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Partial result:&lt;/P&gt;
&lt;PRE&gt;#	Variable	Typ	Länge	Ausg.Format
1	date_column	Num	8	DTDATE9.

date_column
01SEP2021
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Oct 2021 16:16:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774558#M39745</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-10-15T16:16:03Z</dc:date>
    </item>
    <item>
      <title>Re: How can PROC SQL return only results from the previous month</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774575#M39746</link>
      <description>This did it! Thank you so much for your time and help, the data types were really throwing me for a loop. I truly appreciate it!</description>
      <pubDate>Fri, 15 Oct 2021 16:54:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774575#M39746</guid>
      <dc:creator>Boswser</dc:creator>
      <dc:date>2021-10-15T16:54:06Z</dc:date>
    </item>
    <item>
      <title>Re: How can PROC SQL return only results from the previous month</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774583#M39747</link>
      <description>&lt;P&gt;Maxim 3 says Know Your Data, and there is a reason why it is so high up on the list &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Being fluent in data types, especially in the way SAS deals with dates and types, is a must.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Oct 2021 17:56:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-can-PROC-SQL-return-only-results-from-the-previous-month/m-p/774583#M39747</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-10-15T17:56:08Z</dc:date>
    </item>
  </channel>
</rss>

