<?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: Dynamic month filter in data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-month-filter-in-data/m-p/678908#M204993</link>
    <description>Then Data should be filter as&lt;BR /&gt;April 2020 and March 2020</description>
    <pubDate>Mon, 24 Aug 2020 15:42:15 GMT</pubDate>
    <dc:creator>harshpatel</dc:creator>
    <dc:date>2020-08-24T15:42:15Z</dc:date>
    <item>
      <title>Dynamic month filter in data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-month-filter-in-data/m-p/678885#M204980</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;I want to filter data based on month and year selection&lt;BR /&gt;&lt;BR /&gt;IF month is January and year is 2020 then data should filter as January2020 and December2019&lt;BR /&gt;&lt;BR /&gt;But if month is not in January and year is 2020 then data should filter as February2020 and January2020 only two Months&lt;BR /&gt;&lt;BR /&gt;I want this in SAS coding&lt;BR /&gt;&lt;BR /&gt;Basically I want to compare two months only but for January scenario bid different as explained above&lt;BR /&gt;Kindly help me on this&lt;BR /&gt;Thanks,&lt;BR /&gt;Harsh</description>
      <pubDate>Mon, 24 Aug 2020 14:22:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-month-filter-in-data/m-p/678885#M204980</guid>
      <dc:creator>harshpatel</dc:creator>
      <dc:date>2020-08-24T14:22:51Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic month filter in data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-month-filter-in-data/m-p/678899#M204987</link>
      <description>&lt;P&gt;Is it only those two months or does it need to be dynamic for other situations?&amp;nbsp;&lt;BR /&gt;Will you ever need March, April? Is the issue here crossing the year boundary where you want the most recent 2 months or something like that?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Either way, you can increment dates easily using INTNX().&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a great, but longer and in depth, reference for dates and times in SAS&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/ta-p/424354" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/ta-p/424354&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/259069"&gt;@harshpatel&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;I want to filter data based on month and year selection&lt;BR /&gt;&lt;BR /&gt;IF month is January and year is 2020 then data should filter as January2020 and December2019&lt;BR /&gt;&lt;BR /&gt;But if month is not in January and year is 2020 then data should filter as February2020 and January2020 only two Months&lt;BR /&gt;&lt;BR /&gt;I want this in SAS coding&lt;BR /&gt;&lt;BR /&gt;Basically I want to compare two months only but for January scenario bid different as explained above&lt;BR /&gt;Kindly help me on this&lt;BR /&gt;Thanks,&lt;BR /&gt;Harsh&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Aug 2020 15:09:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-month-filter-in-data/m-p/678899#M204987</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-08-24T15:09:53Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic month filter in data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-month-filter-in-data/m-p/678905#M204991</link>
      <description>Hi,&lt;BR /&gt;I will pass macro &lt;BR /&gt;% let month= January&lt;BR /&gt;% let year= 2020&lt;BR /&gt;If I will pass these macros then data should be filter as January 2020 and December 2019&lt;BR /&gt;But if I will change macro &lt;BR /&gt;Month= February&lt;BR /&gt;Year= 2020&lt;BR /&gt;Then data should be filter as February 2020 and January 2020</description>
      <pubDate>Mon, 24 Aug 2020 15:36:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-month-filter-in-data/m-p/678905#M204991</guid>
      <dc:creator>harshpatel</dc:creator>
      <dc:date>2020-08-24T15:36:04Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic month filter in data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-month-filter-in-data/m-p/678907#M204992</link>
      <description>What if I pass month=April and year=2020?</description>
      <pubDate>Mon, 24 Aug 2020 15:40:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-month-filter-in-data/m-p/678907#M204992</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-08-24T15:40:05Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic month filter in data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-month-filter-in-data/m-p/678908#M204993</link>
      <description>Then Data should be filter as&lt;BR /&gt;April 2020 and March 2020</description>
      <pubDate>Mon, 24 Aug 2020 15:42:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-month-filter-in-data/m-p/678908#M204993</guid>
      <dc:creator>harshpatel</dc:creator>
      <dc:date>2020-08-24T15:42:15Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic month filter in data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-month-filter-in-data/m-p/678920#M205001</link>
      <description>&lt;P&gt;So your criteria is month specified and previous month. This works but it'll depend exactly on how you're trying to use the parameters. This is a fully worked example that you can run but you'll need to apply the idea to your own code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*input parameters;
%let month = January;
%let year = 2004;


*create macro variables for next step;
data _null_;
*convert to SAS date;
month_select = input(catt('01', substr("&amp;amp;month", 1, 3), &amp;amp;year), date9.);
*previous month;
month_prev = intnx('month', month_select, -1, 'b');

*create macro variables;
call symputx('month_select', month_select);
call symputx('month_prev', month_prev);

run;

*check macro variables;
%put %sysfunc(putn(&amp;amp;month_select, date9)) ;
%put %sysfunc(putn(&amp;amp;month_prev, date9)) ;


*filter data;
data stocks;
set sashelp.stocks;
where intnx('month', date, 0, 'b') in (&amp;amp;month_select., &amp;amp;month_prev.);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note the usage of INTNX() to align the dates to the beginning of hte month for the comparison to work. This is important as 01Jan2019 is not the same as 03Jan2019 and SAS dates require a day component. Setting everything to the first of the month negates this issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a great, but longer and in depth, reference for dates and times in SAS&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/ta-p/424354" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/ta-p/424354&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/259069"&gt;@harshpatel&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Then Data should be filter as&lt;BR /&gt;April 2020 and March 2020&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Aug 2020 16:17:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-month-filter-in-data/m-p/678920#M205001</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-08-24T16:17:15Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic month filter in data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-month-filter-in-data/m-p/679051#M205045</link>
      <description>Thanks Reeza,&lt;BR /&gt;Its Working for me</description>
      <pubDate>Tue, 25 Aug 2020 04:16:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-month-filter-in-data/m-p/679051#M205045</guid>
      <dc:creator>harshpatel</dc:creator>
      <dc:date>2020-08-25T04:16:43Z</dc:date>
    </item>
  </channel>
</rss>

