<?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: only keep last 12 months of data sas in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/only-keep-last-12-months-of-data-sas/m-p/755654#M238506</link>
    <description>&lt;P&gt;If the most recent date in your dataset is the last record, then you can read that record, calculate the date 1 year before, and then use that date in a WHERE statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set have point=lobs nobs=lobs;
  cut_point=intnx('year',lodge_date,-1,'S');
  call symput("cut_point",put(cut_point,date9.));
  stop;
run;
%put &amp;amp;=cut_point;

data want;
  set have;
  where lodge_date&amp;gt;="&amp;amp;cut_point"d;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The first DATA step reads exactly one record - the last one.&amp;nbsp; The cut_point is calculated as one year earlier, and the date is written to a macrovar CUT_PONT.&amp;nbsp; &amp;nbsp;The CUT_POINT text (yes, it's text, not numeric once it is a macrovar), is in date9 format (see the log for the %PUT statement).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then, in the second DATA step, that macrovar fits nicely in the&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;where lodge_date&amp;gt;="&amp;amp;cut_point"d ;&lt;/P&gt;
&lt;P&gt;filter.&lt;/P&gt;</description>
    <pubDate>Wed, 21 Jul 2021 14:59:11 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2021-07-21T14:59:11Z</dc:date>
    <item>
      <title>only keep last 12 months of data sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/only-keep-last-12-months-of-data-sas/m-p/755625#M238493</link>
      <description>&lt;P&gt;I have a dataset in sas which contains many columns. One of which is a date column called `LODGE_DATE`.&lt;/P&gt;
&lt;P&gt;The dataset contains about 3years worth of data and will contiue to grow. I only want to export the last 12months worth of data, how can I do this without choosing the actual months to include/exclude?&lt;/P&gt;
&lt;P&gt;'LODGE_DATE' is formatted MONYY5.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jul 2021 14:02:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/only-keep-last-12-months-of-data-sas/m-p/755625#M238493</guid>
      <dc:creator>sasprogramming</dc:creator>
      <dc:date>2021-07-21T14:02:12Z</dc:date>
    </item>
    <item>
      <title>Re: only keep last 12 months of data sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/only-keep-last-12-months-of-data-sas/m-p/755627#M238494</link>
      <description>&lt;P&gt;If your variable is an actual SAS date, not a character value or simple number that looks like JUL21 (BTW 2 digit years are a bad idea), then anything you want to use a subset based on date is a WHERE statement or data set option&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; where lodge_date ge '01JAN2019'd;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;for example selects all observations where the date in lodge_date is 01JAN2019 or later. The date literal to compare with must be in the Date9 (or Date7) appearance with the quotes and the d. The d tells SAS you want the date value represented by the string.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this doesn't work the show us Proc Contents output describing your data.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jul 2021 14:09:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/only-keep-last-12-months-of-data-sas/m-p/755627#M238494</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-07-21T14:09:04Z</dc:date>
    </item>
    <item>
      <title>Re: only keep last 12 months of data sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/only-keep-last-12-months-of-data-sas/m-p/755629#M238496</link>
      <description>&lt;P&gt;Thanks, this works but I want it to be the last 12months regardless of what date is the last observation.&lt;/P&gt;
&lt;P&gt;Like the logic i am after is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;keep rows for: (last.date - 12months)&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jul 2021 14:13:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/only-keep-last-12-months-of-data-sas/m-p/755629#M238496</guid>
      <dc:creator>sasprogramming</dc:creator>
      <dc:date>2021-07-21T14:13:28Z</dc:date>
    </item>
    <item>
      <title>Re: only keep last 12 months of data sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/only-keep-last-12-months-of-data-sas/m-p/755653#M238505</link>
      <description>&lt;P&gt;'LODGE_DATE' is now formatted&amp;nbsp;yymmd7.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jul 2021 14:56:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/only-keep-last-12-months-of-data-sas/m-p/755653#M238505</guid>
      <dc:creator>sasprogramming</dc:creator>
      <dc:date>2021-07-21T14:56:52Z</dc:date>
    </item>
    <item>
      <title>Re: only keep last 12 months of data sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/only-keep-last-12-months-of-data-sas/m-p/755654#M238506</link>
      <description>&lt;P&gt;If the most recent date in your dataset is the last record, then you can read that record, calculate the date 1 year before, and then use that date in a WHERE statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set have point=lobs nobs=lobs;
  cut_point=intnx('year',lodge_date,-1,'S');
  call symput("cut_point",put(cut_point,date9.));
  stop;
run;
%put &amp;amp;=cut_point;

data want;
  set have;
  where lodge_date&amp;gt;="&amp;amp;cut_point"d;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The first DATA step reads exactly one record - the last one.&amp;nbsp; The cut_point is calculated as one year earlier, and the date is written to a macrovar CUT_PONT.&amp;nbsp; &amp;nbsp;The CUT_POINT text (yes, it's text, not numeric once it is a macrovar), is in date9 format (see the log for the %PUT statement).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then, in the second DATA step, that macrovar fits nicely in the&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;where lodge_date&amp;gt;="&amp;amp;cut_point"d ;&lt;/P&gt;
&lt;P&gt;filter.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jul 2021 14:59:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/only-keep-last-12-months-of-data-sas/m-p/755654#M238506</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-07-21T14:59:11Z</dc:date>
    </item>
    <item>
      <title>Re: only keep last 12 months of data sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/only-keep-last-12-months-of-data-sas/m-p/755677#M238517</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/302184"&gt;@sasprogramming&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;'LODGE_DATE' is now formatted&amp;nbsp;yymmd7.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;FORMATS have no impact on comparison, calculation or other manipulation (unless the code explicitly uses a format to create at text value).&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jul 2021 15:57:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/only-keep-last-12-months-of-data-sas/m-p/755677#M238517</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-07-21T15:57:53Z</dc:date>
    </item>
    <item>
      <title>Re: only keep last 12 months of data sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/only-keep-last-12-months-of-data-sas/m-p/755739#M238541</link>
      <description>&lt;P&gt;And if the "last date" is buried in an arbitrary observation, do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select intnx('year',max(lodge_date),-1,'s') into :cutoff from have;
quit;

data want;
set have;
where lodge_date ge &amp;amp;cutoff.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Jul 2021 20:31:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/only-keep-last-12-months-of-data-sas/m-p/755739#M238541</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-07-21T20:31:20Z</dc:date>
    </item>
  </channel>
</rss>

