<?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: Rolling 5 year max for groups with date verification using Proc SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684799#M207593</link>
    <description>&lt;P&gt;Be aware that the expression&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;b.datadate between intnx("YEAR", a.datadate, -4) and intnx("YEAR", a.datadate, -1)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;generates time limits of 01jan 4 calendar years before and 01jan of last year.&amp;nbsp; INTNX has to be told to align things, otherwise it defaults to the start of the resulting calendar year.&amp;nbsp;&amp;nbsp; This pushes back the upper limit of your time range.&amp;nbsp; So to use this properly, you should use the "S" (for "same") parameter, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;b.datadate between intnx("YEAR", a.datadate, -4,'s') and intnx("YEAR", a.datadate, -1,'s')&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Of course this changes the lower limit of your time range too, but maybe it didn't have any impact.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;FINALLY, just don't bother with using INTNX for the upper range limit.&amp;nbsp; Just use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;b.datadate between intnx("YEAR", a.datadate, -4) and datadate-1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which will produce the same results (as long as you don't have two records in less than 365 (or 366) days), and will avoid unnecessary use of the INTNX function.&lt;/P&gt;</description>
    <pubDate>Thu, 17 Sep 2020 21:00:03 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2020-09-17T21:00:03Z</dc:date>
    <item>
      <title>Rolling 5 year max for groups with date verification using Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684739#M207558</link>
      <description>&lt;P&gt;The relevant column in my dataset are "gvkey" (a group id), datadate (ex. YYYYMMDD = 20100531), and revt.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The observation frequency is annual.&lt;/P&gt;&lt;P&gt;I want to to construct a rolling max of "revt" for the PREVIOUS 4 years for some "gvkey".&lt;/P&gt;&lt;P&gt;I don't want to hard code a window size of 5 because this would not handle missing values appropriately.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm very new the SAS so I'm not sure how this should be handled but I think SQL is the way to go.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For sake of clarity, I need something that's a little like&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;```data[&lt;/SPAN&gt;&lt;SPAN&gt;'5yr_revt_max'&lt;/SPAN&gt;&lt;SPAN&gt;]&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;data.&lt;/SPAN&gt;&lt;SPAN&gt;groupby(&lt;/SPAN&gt;&lt;SPAN&gt;"gvkey"&lt;/SPAN&gt;&lt;SPAN&gt;)[&lt;/SPAN&gt;&lt;SPAN&gt;'revt'&lt;/SPAN&gt;&lt;SPAN&gt;].&lt;/SPAN&gt;&lt;SPAN&gt;shift(&lt;/SPAN&gt;&lt;SPAN&gt;1&lt;/SPAN&gt;&lt;SPAN&gt;).&lt;/SPAN&gt;&lt;SPAN class="il"&gt;rolling&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;5&lt;/SPAN&gt;&lt;SPAN&gt;).max()```&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Except this doesn't account for the years and is in Python&lt;/P&gt;</description>
      <pubDate>Thu, 17 Sep 2020 18:20:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684739#M207558</guid>
      <dc:creator>logann2</dc:creator>
      <dc:date>2020-09-17T18:20:19Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 5 year max for groups with date verification using Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684749#M207563</link>
      <description>&lt;P&gt;So how do you want to handle missing years for a given gvkey? Return a missing max or the max of the non-missing years?&lt;/P&gt;</description>
      <pubDate>Thu, 17 Sep 2020 18:51:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684749#M207563</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-09-17T18:51:07Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 5 year max for groups with date verification using Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684752#M207565</link>
      <description>&lt;P&gt;max from the set of remaining valid years. does that make sense?&lt;/P&gt;</description>
      <pubDate>Thu, 17 Sep 2020 19:00:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684752#M207565</guid>
      <dc:creator>logann2</dc:creator>
      <dc:date>2020-09-17T19:00:20Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 5 year max for groups with date verification using Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684776#M207575</link>
      <description>&lt;P&gt;Then, assuming there are never more than one revt value per year for a given gvkey, you would want something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select
	a.gvkey,
	a.datadate,
	max(b.revt) as maxRevt
from
	have as a inner join
	have as b on a.gvkey=b.gvkey and 
		b.datadate between intnx("YEAR", a.datadate, -4) and a.datadate
group by a.gvkey,	a.datadate;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(untested)&lt;/P&gt;</description>
      <pubDate>Thu, 17 Sep 2020 19:40:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684776#M207575</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-09-17T19:40:19Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 5 year max for groups with date verification using Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684789#M207585</link>
      <description>&lt;P&gt;Here's a data step solution that assumes your data are sorted by gvkey/datadate (no data, untested):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have;
  by gvkey;

  array rev_array {0:4} _temporary_;  /* for 0mod5 through 4mod5*/

  cur_yyyy=year(datadate);

  if first.gvkey then call missing(of rev_array{*});

  /* For any missing year (and current year), set rev_array element to missing */
  do _yyyy=lag(_cur_yyyy)+1 to _cur_yyyy;
    if first.gvkey=0 then  rev_array{mod(_yyyy-1,5}}=.;
  end;

  max_revt_prior4=max(of rev_array{*});

  rev_array{mod(_cur_yyyy-1,5}=revt;

run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The idea here is to:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; 1. Find any holes following the year of the previous record.&amp;nbsp; Set the correspond array elements in rev_array (and also the array element for the CURRENT record) to missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; 2. Get the max of the array, which will be the max of the non-missing values among the preceding 4 years.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; 3. After the max has been established, put the current year REVT into the array, making it available for the next 4 years.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note the array rev_array is indexed from 0 through 4, to conform to the result of the mod(_yyyy,5) function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also heed the warning of &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt; .&amp;nbsp; If a company changes its fiscal year, it is possible to have two fiscal year reports (one of these "annual" reports won't cover 12 actual months) in the same calendar year.&amp;nbsp; You will have two identical maximums for that calendar year.&amp;nbsp; And all subsequent records will have a maximum ignoring the earlier datadate of the two.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Sep 2020 20:23:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684789#M207585</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-09-17T20:23:36Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 5 year max for groups with date verification using Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684796#M207590</link>
      <description>&lt;P&gt;So I'd like to only look at the previous 5 years and exclude the current observation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I thought that something like this might work&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;```&lt;/P&gt;&lt;P&gt;b.datadate between intnx("YEAR", a.datadate, -4) and intnx("YEAR", a.datadate, -1)&lt;/P&gt;&lt;P&gt;````&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;^^^ All I did was modify a.datadate to&amp;nbsp;intnx("YEAR", a.datadate, -1) which it doesn't like...&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Sep 2020 20:39:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684796#M207590</guid>
      <dc:creator>logann2</dc:creator>
      <dc:date>2020-09-17T20:39:10Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 5 year max for groups with date verification using Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684797#M207591</link>
      <description>&lt;P&gt;What do you mean by "doesn't like".&amp;nbsp; Does SAS object, or do you get unexpected results.&amp;nbsp; If the latter (which I suspect), provide an example of unexpected results vs the data from which they were derived.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Sep 2020 20:44:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684797#M207591</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-09-17T20:44:45Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 5 year max for groups with date verification using Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684798#M207592</link>
      <description>My table ends up empty, maybe because current date can never equal yesterday's date?</description>
      <pubDate>Thu, 17 Sep 2020 20:46:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684798#M207592</guid>
      <dc:creator>logann2</dc:creator>
      <dc:date>2020-09-17T20:46:22Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 5 year max for groups with date verification using Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684799#M207593</link>
      <description>&lt;P&gt;Be aware that the expression&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;b.datadate between intnx("YEAR", a.datadate, -4) and intnx("YEAR", a.datadate, -1)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;generates time limits of 01jan 4 calendar years before and 01jan of last year.&amp;nbsp; INTNX has to be told to align things, otherwise it defaults to the start of the resulting calendar year.&amp;nbsp;&amp;nbsp; This pushes back the upper limit of your time range.&amp;nbsp; So to use this properly, you should use the "S" (for "same") parameter, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;b.datadate between intnx("YEAR", a.datadate, -4,'s') and intnx("YEAR", a.datadate, -1,'s')&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Of course this changes the lower limit of your time range too, but maybe it didn't have any impact.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;FINALLY, just don't bother with using INTNX for the upper range limit.&amp;nbsp; Just use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;b.datadate between intnx("YEAR", a.datadate, -4) and datadate-1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which will produce the same results (as long as you don't have two records in less than 365 (or 366) days), and will avoid unnecessary use of the INTNX function.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Sep 2020 21:00:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684799#M207593</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-09-17T21:00:03Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 5 year max for groups with date verification using Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684826#M207608</link>
      <description>&lt;P&gt;It should return something... The problem may be elsewhere, please post more of your code.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Sep 2020 22:51:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684826#M207608</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-09-17T22:51:55Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 5 year max for groups with date verification using Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684860#M207621</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc sql;
	create table final as
	select
		a.gvkey,
		a.datadate,
		max(b.revt) as maxRevt
	from
		data as a inner join
		data as b on a.gvkey=b.gvkey and
			b.datadate between intnx("YEAR", a.datadate, -4) and intnx("YEAR", a.datadate, -1)
	group by a.gvkey, a.datadate;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Figured out how to insert code haha.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Sep 2020 03:30:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684860#M207621</guid>
      <dc:creator>logann2</dc:creator>
      <dc:date>2020-09-18T03:30:27Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 5 year max for groups with date verification using Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684861#M207622</link>
      <description>&lt;P&gt;The last option is the only one that worked. The ones with the `ntnx` command produced 0 observation tables. Why would that be?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also for the last one, how does it know to subtract from the years? As far as I understand it views 20100531 as a big integer&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc sql;
	create table final as
	select
		a.gvkey,
		a.datadate,
		max(b.revt) as maxRevt
	from
		data as a inner join
		data as b on a.gvkey=b.gvkey and
			b.datadate between intnx("YEAR", a.datadate, -4) and a.datadate-1
	group by a.gvkey, a.datadate;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Sep 2020 03:43:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684861#M207622</guid>
      <dc:creator>logann2</dc:creator>
      <dc:date>2020-09-18T03:43:18Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 5 year max for groups with date verification using Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684979#M207682</link>
      <description>&lt;P&gt;If you want to avoid the intricacies of intnx and intck functions, you could simply do:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc sql;
	create table final as
	select
		a.gvkey,
		a.datadate,
		max(b.revt) as maxRevt
	from
		data as a inner join
		data as b on a.gvkey=b.gvkey and
			year(b.datadate) between year( a.datadate) -4 and year( a.datadate) -1
	group by a.gvkey, a.datadate;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Sep 2020 14:14:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-5-year-max-for-groups-with-date-verification-using-Proc/m-p/684979#M207682</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-09-18T14:14:08Z</dc:date>
    </item>
  </channel>
</rss>

