<?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: Filter on quarter months value from quarter ending date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Filter-on-quarter-months-value-from-quarter-ending-date/m-p/704093#M215814</link>
    <description>&lt;P&gt;Hello.&lt;BR /&gt;I made a modification to your program.&lt;BR /&gt;Extracting year and month from your macro variables can be done by calling %sysfunc function.&lt;/P&gt;
&lt;P&gt;%sysfunc function&lt;BR /&gt;&lt;A href="https://documentation.sas.com/?cdcId=vdmmlcdc&amp;amp;cdcVersion=8.1&amp;amp;docsetId=mcrolref&amp;amp;docsetTarget=p1o13d7wb2zfcnn19s5ssl2zdxvi.htm&amp;amp;locale=en"&gt;https://documentation.sas.com/?cdcId=vdmmlcdc&amp;amp;cdcVersion=8.1&amp;amp;docsetId=mcrolref&amp;amp;docsetTarget=p1o13d7wb2zfcnn19s5ssl2zdxvi.htm&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For quarter calculation , macro variable is a character string so arithmetic calculation is not possible.&lt;BR /&gt;Use %eval function for this purpose.&lt;/P&gt;
&lt;P&gt;%eval function&lt;BR /&gt;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=mcrolref&amp;amp;docsetTarget=n07pr39df9k7m3n1w3x1q09iewta.htm&amp;amp;locale=en"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=mcrolref&amp;amp;docsetTarget=n07pr39df9k7m3n1w3x1q09iewta.htm&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm="09"x;
input ID Purchase_Year Purchase_Month;
datalines;
1	2017	9
1	2017	7
1	2018	11
1	2018	12
;
run;

*macro variable;
%let REPORTINGDATE=20170930;
*Use datastep function by calling %sysfunc function;
*year;
%let y=%sysfunc(year(%sysfunc(inputn(&amp;amp;REPORTINGDATE,anydtdte.))));
*month;
%let m=%sysfunc(month(%sysfunc(inputn(&amp;amp;REPORTINGDATE,anydtdte.))));

*SAS macro string is a set of character so doesn't do arithmetic calculation by it self.
Use %eval function for this purpose.;
options mprint;
Proc SQL;
Create table test as
  select * from have 
    where 
      Purchase_Year=&amp;amp;y and Purchase_Month in (&amp;amp;m, %eval(&amp;amp;m-1),%eval(&amp;amp;m-2));
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 07 Dec 2020 10:06:47 GMT</pubDate>
    <dc:creator>hhinohar</dc:creator>
    <dc:date>2020-12-07T10:06:47Z</dc:date>
    <item>
      <title>Filter on quarter months value from quarter ending date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filter-on-quarter-months-value-from-quarter-ending-date/m-p/704085#M215812</link>
      <description>&lt;P&gt;I've a data as follows. Now I want to filter the data based on the value of macro variable. If I've a&amp;nbsp;macro variable value as&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;%let REPORTINGDATE =20170930;&lt;/EM&gt; then I want to filter the data of year value (e.g. 2017)&amp;nbsp;of the macro variable and the quarter months values (e.g. 7,8,9)&amp;nbsp;from the macro variable&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ID	Purchase_Year	Purchase_Month
1	2017	                9
1	2017	                7
1	2018	                11
1	2018	                12
&lt;/PRE&gt;
&lt;P&gt;I've used the code as below but it's not working as excepted. Instead of filtering the first two records from the data, it is filtering only the first record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Proc SQL;
Create table test as
select * from have 
where Purchase_Year=%substr(&amp;amp;Reportingdate1,1,4) &amp;amp; Purchase_Month in (%substr(&amp;amp;Reportingdate1,5,2), %substr(&amp;amp;Reportingdate1,5,2)-1,%substr(&amp;amp;Reportingdate1,5,2)-2)&lt;/PRE&gt;
&lt;P&gt;Desired Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ID	Purchase_Year	Purchase_Month
1	2017	                9
1	2017	                7&lt;/PRE&gt;
&lt;P&gt;Macro variable value will always be quarter ending date. So I want to filter Purchase_Month values for (1,2,3) or (4,5,6) or (7,8,9) or (10,11,12). Also I'm looking for result using where clause. Any help?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Dec 2020 08:54:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filter-on-quarter-months-value-from-quarter-ending-date/m-p/704085#M215812</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2020-12-07T08:54:44Z</dc:date>
    </item>
    <item>
      <title>Re: Filter on quarter months value from quarter ending date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filter-on-quarter-months-value-from-quarter-ending-date/m-p/704093#M215814</link>
      <description>&lt;P&gt;Hello.&lt;BR /&gt;I made a modification to your program.&lt;BR /&gt;Extracting year and month from your macro variables can be done by calling %sysfunc function.&lt;/P&gt;
&lt;P&gt;%sysfunc function&lt;BR /&gt;&lt;A href="https://documentation.sas.com/?cdcId=vdmmlcdc&amp;amp;cdcVersion=8.1&amp;amp;docsetId=mcrolref&amp;amp;docsetTarget=p1o13d7wb2zfcnn19s5ssl2zdxvi.htm&amp;amp;locale=en"&gt;https://documentation.sas.com/?cdcId=vdmmlcdc&amp;amp;cdcVersion=8.1&amp;amp;docsetId=mcrolref&amp;amp;docsetTarget=p1o13d7wb2zfcnn19s5ssl2zdxvi.htm&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For quarter calculation , macro variable is a character string so arithmetic calculation is not possible.&lt;BR /&gt;Use %eval function for this purpose.&lt;/P&gt;
&lt;P&gt;%eval function&lt;BR /&gt;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=mcrolref&amp;amp;docsetTarget=n07pr39df9k7m3n1w3x1q09iewta.htm&amp;amp;locale=en"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=mcrolref&amp;amp;docsetTarget=n07pr39df9k7m3n1w3x1q09iewta.htm&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm="09"x;
input ID Purchase_Year Purchase_Month;
datalines;
1	2017	9
1	2017	7
1	2018	11
1	2018	12
;
run;

*macro variable;
%let REPORTINGDATE=20170930;
*Use datastep function by calling %sysfunc function;
*year;
%let y=%sysfunc(year(%sysfunc(inputn(&amp;amp;REPORTINGDATE,anydtdte.))));
*month;
%let m=%sysfunc(month(%sysfunc(inputn(&amp;amp;REPORTINGDATE,anydtdte.))));

*SAS macro string is a set of character so doesn't do arithmetic calculation by it self.
Use %eval function for this purpose.;
options mprint;
Proc SQL;
Create table test as
  select * from have 
    where 
      Purchase_Year=&amp;amp;y and Purchase_Month in (&amp;amp;m, %eval(&amp;amp;m-1),%eval(&amp;amp;m-2));
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 07 Dec 2020 10:06:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filter-on-quarter-months-value-from-quarter-ending-date/m-p/704093#M215814</guid>
      <dc:creator>hhinohar</dc:creator>
      <dc:date>2020-12-07T10:06:47Z</dc:date>
    </item>
    <item>
      <title>Re: Filter on quarter months value from quarter ending date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filter-on-quarter-months-value-from-quarter-ending-date/m-p/704097#M215817</link>
      <description>&lt;P&gt;First, make your data intelligent. Date-related values should be stored as SAS date values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data good;
set have;
p_month = mdy(purchase_month,1,purchase_year);
format p_month yymmd7.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can apply functions to filter:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let reportingdate=%sysfunc(inputn(20170930,yymmdd8.));
/* no format necessary, see Maxim 28 */

data want;
set good;
where intnx('qtr',&amp;amp;reportingdate.,0,'b') le p_month le intnx('qtr',&amp;amp;reportingdate.,0,'e');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 07 Dec 2020 10:31:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filter-on-quarter-months-value-from-quarter-ending-date/m-p/704097#M215817</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-12-07T10:31:28Z</dc:date>
    </item>
  </channel>
</rss>

