<?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: Pull only last 5 year from today in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Pull-only-last-5-year-from-today/m-p/584589#M166533</link>
    <description>&lt;P&gt;Try something like this. Tough without seeing the data/log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table bb as
select DATEPART(TEST_DATE) AS TESTED LABEL = 'TESTED',
          TESC_CODE,
          TEST_DATE,
          TODAY() AS AA ,
          DATEPART(TEST_DATE) as Date format=date9.
        from stg.sortest;
        where calculated Date &amp;gt;= intnx('year', today() ,-5,'sameday');
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In your original code I see a few issues:&lt;/P&gt;
&lt;P&gt;1. Today() - datepart(Test_Date) will give you how many days between today and whatever test_date is. That will not be a year. It will be a numeric value.&lt;/P&gt;
&lt;P&gt;2. where Year &amp;lt; '1825' won't work for two reasons. One Year is currently numeric (from above), so a test against a character will probably cause an error. Also, Year is a calculated column, so you need to use the calculated keyword.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The INTNX function increments a date. I specify to use today's date, use year increments, and go back 5 years from today's date exactly. You an change 'sameday' to a variety of different methods. See&amp;nbsp;&lt;A href="https://go.documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p10v3sa3i4kfxfn1sovhi5xzxh8n.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;INTNX Function&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I also wrote some code with fake data that uses different date functions for you to see:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
Datetime='17OCT2011:12:5'DT;
output;
Datetime='17OCT2012:12:5'DT;
output;
Datetime='17OCT2013:12:5'DT;
output;
Datetime='17OCT2014:12:5'DT;
output;
Datetime='17OCT2015:12:5'DT;
output;
Datetime='17OCT2016:12:5'DT;
output;
Datetime='17OCT2017:12:5'DT;
output;
Datetime='17OCT2018:12:5'DT;
output;
Datetime='17OCT2019:12:5'DT;
output;
format DateTime datetime.;
run;

proc sql;
create table Final as
select DateTime, 
       datepart(DateTime) as Date format=date9.,
	   day(calculated Date) as Day,
	   month(calculated Date) as Month,
       year(calculated Date) as Year,
       intnx('year', today(),-5,'sameday') as FiveYearsAgoFromToday format=date9.
     from test
     where calculated Date &amp;gt;= intnx('year', today(),-5,'sameday');
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 28 Aug 2019 14:51:00 GMT</pubDate>
    <dc:creator>Panagiotis</dc:creator>
    <dc:date>2019-08-28T14:51:00Z</dc:date>
    <item>
      <title>Pull only last 5 year from today</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pull-only-last-5-year-from-today/m-p/584570#M166524</link>
      <description>&lt;P&gt;I am new with SAS&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Trying to pull only last 5 year data from today. Comparing with test_date. This is my code, but doesn't work&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&amp;nbsp;         proc sql;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; create table bb as
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;select DATEPART(TEST_DATE) AS TESTED LABEL = 'TESTED',
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;TESC_CODE,
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;TEST_DATE,
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; TODAY() AS AA ,
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; TODAY() - DATEPART(TEST_DATE) as year

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; from stg.sortest;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; where year &amp;lt; '1825';

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; quit;

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 14:22:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pull-only-last-5-year-from-today/m-p/584570#M166524</guid>
      <dc:creator>lerdem</dc:creator>
      <dc:date>2019-08-28T14:22:06Z</dc:date>
    </item>
    <item>
      <title>Re: Pull only last 5 year from today</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pull-only-last-5-year-from-today/m-p/584574#M166526</link>
      <description>&lt;P&gt;I'm guessing you want this&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where calculated year &amp;lt; 1825&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If that's not it, then please show the complete SAS log of this step (not just the error message) by clicking on the {i} icon and pasting the log into the window that appears.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the future, would you please be so kind as to paste your SAS Code in the window that appears when you click on the "running man" icon. This makes the appearance of the code much more readable. Thanks.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 14:19:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pull-only-last-5-year-from-today/m-p/584574#M166526</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-08-28T14:19:10Z</dc:date>
    </item>
    <item>
      <title>Re: Pull only last 5 year from today</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pull-only-last-5-year-from-today/m-p/584584#M166530</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I corrected how it looks the code.&amp;nbsp; The new addition is not working. No error come out, didn't eliminate anything.&lt;/P&gt;
&lt;P&gt;Thanks&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 14:41:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pull-only-last-5-year-from-today/m-p/584584#M166530</guid>
      <dc:creator>lerdem</dc:creator>
      <dc:date>2019-08-28T14:41:46Z</dc:date>
    </item>
    <item>
      <title>Re: Pull only last 5 year from today</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pull-only-last-5-year-from-today/m-p/584587#M166532</link>
      <description>&lt;P&gt;You need to provide sample data (not necessarily the entire data set) by following these instructions:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/tac-p/580061" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/tac-p/580061&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, please show us the LOG anyway, by clicking on the {i} icon and pasting the log into the window that appears.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 14:48:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pull-only-last-5-year-from-today/m-p/584587#M166532</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-08-28T14:48:00Z</dc:date>
    </item>
    <item>
      <title>Re: Pull only last 5 year from today</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pull-only-last-5-year-from-today/m-p/584589#M166533</link>
      <description>&lt;P&gt;Try something like this. Tough without seeing the data/log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table bb as
select DATEPART(TEST_DATE) AS TESTED LABEL = 'TESTED',
          TESC_CODE,
          TEST_DATE,
          TODAY() AS AA ,
          DATEPART(TEST_DATE) as Date format=date9.
        from stg.sortest;
        where calculated Date &amp;gt;= intnx('year', today() ,-5,'sameday');
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In your original code I see a few issues:&lt;/P&gt;
&lt;P&gt;1. Today() - datepart(Test_Date) will give you how many days between today and whatever test_date is. That will not be a year. It will be a numeric value.&lt;/P&gt;
&lt;P&gt;2. where Year &amp;lt; '1825' won't work for two reasons. One Year is currently numeric (from above), so a test against a character will probably cause an error. Also, Year is a calculated column, so you need to use the calculated keyword.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The INTNX function increments a date. I specify to use today's date, use year increments, and go back 5 years from today's date exactly. You an change 'sameday' to a variety of different methods. See&amp;nbsp;&lt;A href="https://go.documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p10v3sa3i4kfxfn1sovhi5xzxh8n.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;INTNX Function&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I also wrote some code with fake data that uses different date functions for you to see:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
Datetime='17OCT2011:12:5'DT;
output;
Datetime='17OCT2012:12:5'DT;
output;
Datetime='17OCT2013:12:5'DT;
output;
Datetime='17OCT2014:12:5'DT;
output;
Datetime='17OCT2015:12:5'DT;
output;
Datetime='17OCT2016:12:5'DT;
output;
Datetime='17OCT2017:12:5'DT;
output;
Datetime='17OCT2018:12:5'DT;
output;
Datetime='17OCT2019:12:5'DT;
output;
format DateTime datetime.;
run;

proc sql;
create table Final as
select DateTime, 
       datepart(DateTime) as Date format=date9.,
	   day(calculated Date) as Day,
	   month(calculated Date) as Month,
       year(calculated Date) as Year,
       intnx('year', today(),-5,'sameday') as FiveYearsAgoFromToday format=date9.
     from test
     where calculated Date &amp;gt;= intnx('year', today(),-5,'sameday');
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 28 Aug 2019 14:51:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pull-only-last-5-year-from-today/m-p/584589#M166533</guid>
      <dc:creator>Panagiotis</dc:creator>
      <dc:date>2019-08-28T14:51:00Z</dc:date>
    </item>
  </channel>
</rss>

