<?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 Filter a table differently depending on conditions in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Filter-a-table-differently-depending-on-conditions/m-p/621052#M77210</link>
    <description>&lt;P&gt;Hello! I am currently working on a problem where I need to filter a data set differently depending on conditions about that table. The program I am writing will be run on a weekly basis and there may be some times it needs to filter in different ways. The table has a date column and a status column. If the max year in the data set is equal to the current year there is a specific way it needs to be filtered, and if it is not equal to the current year there will be a change to the filter.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried using if logic with multiple set statements in a data step, and found that is not possible (maybe?). I have also tried proc sql with case when statements and have had no success. Lastly, I have tried using a macro variable, but am inexperienced with working with macros. Here is an example of what I have tried:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA want; 
SET have; 
IF MAX(YEAR(have.DATE)) = YEAR(TODAY()) THEN 
SET have(WHERE=(have.DATE &amp;gt;= INTNX('Year', have.DATE, 0, 'B') AND have.STATUS = 'Approved')); 
ElSE SET have(WHERE=(have.DATE &amp;gt;= INTNX('Year', have.DATE, -1, 'B') AND have.STATUS = 'Approved'));&lt;BR /&gt;RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Essentially, if the max date is in the current year then I need the rows in that year, and if the max date is in the previous year, than I need all of the rows from that year.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 30 Jan 2020 04:48:39 GMT</pubDate>
    <dc:creator>gorba004</dc:creator>
    <dc:date>2020-01-30T04:48:39Z</dc:date>
    <item>
      <title>Filter a table differently depending on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Filter-a-table-differently-depending-on-conditions/m-p/621052#M77210</link>
      <description>&lt;P&gt;Hello! I am currently working on a problem where I need to filter a data set differently depending on conditions about that table. The program I am writing will be run on a weekly basis and there may be some times it needs to filter in different ways. The table has a date column and a status column. If the max year in the data set is equal to the current year there is a specific way it needs to be filtered, and if it is not equal to the current year there will be a change to the filter.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried using if logic with multiple set statements in a data step, and found that is not possible (maybe?). I have also tried proc sql with case when statements and have had no success. Lastly, I have tried using a macro variable, but am inexperienced with working with macros. Here is an example of what I have tried:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA want; 
SET have; 
IF MAX(YEAR(have.DATE)) = YEAR(TODAY()) THEN 
SET have(WHERE=(have.DATE &amp;gt;= INTNX('Year', have.DATE, 0, 'B') AND have.STATUS = 'Approved')); 
ElSE SET have(WHERE=(have.DATE &amp;gt;= INTNX('Year', have.DATE, -1, 'B') AND have.STATUS = 'Approved'));&lt;BR /&gt;RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Essentially, if the max date is in the current year then I need the rows in that year, and if the max date is in the previous year, than I need all of the rows from that year.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jan 2020 04:48:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Filter-a-table-differently-depending-on-conditions/m-p/621052#M77210</guid>
      <dc:creator>gorba004</dc:creator>
      <dc:date>2020-01-30T04:48:39Z</dc:date>
    </item>
    <item>
      <title>Re: Filter a table differently depending on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Filter-a-table-differently-depending-on-conditions/m-p/621250#M77217</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/271271"&gt;@gorba004&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can't do it all in the same data step, so here is how you could use proc sql to determine if current year is present in your input and then use macro code to execute an appropriate data step depending on the result. Try to experiment with dates in the test input.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Test data;
data have; 
	format date date9.;
	DATE = '01feb2018'd; STATUS = 'Approved'; output;
	DATE = '01feb2019'd; STATUS = 'Approved'; output;
	DATE = '01feb2020'd; STATUS = 'Approved'; output;
run;

* Create boolean variable 0/1 for current year present in have;
proc sql noprint;
	select max(year(DATE)) = year(date()) into :currentyear
	from have;
quit;
%put &amp;amp;=currentyear;

* Macro conditional execution of one of the two data steps depending on value of currentyear;
%if &amp;amp;currentyear = 1 %then %do;
	data want; 
		set have;
		if DATE &amp;gt;= intnx('Year', date(), 0) AND STATUS = 'Approved'; 
	run;
%end;
%else %do;
	DATA want; 
		set have;
		if DATE &amp;gt;= intnx('Year', date(), -1) AND STATUS = 'Approved'; 
	run;
%end;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 Jan 2020 17:43:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Filter-a-table-differently-depending-on-conditions/m-p/621250#M77217</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2020-01-30T17:43:46Z</dc:date>
    </item>
  </channel>
</rss>

