<?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: Rank within N rows (moving rank) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Rank-within-N-rows-moving-rank/m-p/701391#M214778</link>
    <description>&lt;P&gt;For one thing, you should insert the current value into the array before calculating percentiles&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Calculate moving percentiles for each window based on this record and previous 199 records */
data PRC; 
    set PRC;
	array a{0:199} _temporary_;
	a{mod(_n_,200)}=ROC1;
	if _n_ &amp;gt;= 200 then do;
		pct_low = pctl(75, of a{*}); 
		pct_mid = pctl(90, of a{*});	
		pct_high = pctl(95, of a{*});	
		pct_top = pctl(98, of a{*});
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(untested)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As for what you expect... you should tell us.&lt;/P&gt;</description>
    <pubDate>Tue, 24 Nov 2020 22:42:54 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2020-11-24T22:42:54Z</dc:date>
    <item>
      <title>Rank within N rows (moving rank)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rank-within-N-rows-moving-rank/m-p/701376#M214773</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a time series data and I would like to perform the following rank:&lt;/P&gt;
&lt;P&gt;for each row, look back 199 rows (so total 200 rows).&lt;/P&gt;
&lt;P&gt;Rank value within 200 rows, so the &lt;STRONG&gt;rank runs from 1 to 200&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;Identify the top 150,180, 190, 196 values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I use the percentile concept but it doesn't work 100% as expected.&lt;/P&gt;
&lt;P&gt;My code is below.&lt;/P&gt;
&lt;P&gt;Can anyone please help?&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*calculate Moving percentile for each window based on this record and previous 199 record;
data PRC; set PRC;
	drop aa1-aa200;
	array a{0:199} aa1-aa200;
	retain aa:;
		pct_low=pctl(75,of a{*}); 
		pct_mid=pctl(90,of a{*});	
		pct_high=pctl(95,of a{*});	
		pct_top=pctl(98,of a{*});

	a{mod(_n_,200)}=ROC1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Nov 2020 21:32:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rank-within-N-rows-moving-rank/m-p/701376#M214773</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2020-11-24T21:32:48Z</dc:date>
    </item>
    <item>
      <title>Re: Rank within N rows (moving rank)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rank-within-N-rows-moving-rank/m-p/701377#M214774</link>
      <description>&lt;P&gt;What do you expect to do with ties? The PCTL function has&amp;nbsp;&lt;STRONG&gt;5&lt;/STRONG&gt; variations with the default being 5. Look up the effect of QNTLDEF to see potential effect.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please describe exactly what was wrong with the percentile approach?&lt;/P&gt;</description>
      <pubDate>Tue, 24 Nov 2020 21:43:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rank-within-N-rows-moving-rank/m-p/701377#M214774</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-11-24T21:43:29Z</dc:date>
    </item>
    <item>
      <title>Re: Rank within N rows (moving rank)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rank-within-N-rows-moving-rank/m-p/701386#M214776</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/49486"&gt;@hhchenfx&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you know the ranks in advance, the &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n0buwtmbjxc2g6n117ijtefss031.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;ORDINAL function&lt;/A&gt; or the &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=p0odr0qov4w671n0zonjy8k1vb09.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;SMALLEST function&lt;/A&gt;&amp;nbsp;-- depending on how you want to handle missing values -- will be easier to use. The SMALLEST function could also be replaced by the &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n1hqpyj2pz85u2n1fcl1uoh5rx2l.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;LARGEST function&lt;/A&gt;. For example, the 196th smallest of 200 non-missing values is the 5th largest. Moreover, you can save the DROP and RETAIN statements as well as the variable names &lt;FONT face="courier new,courier"&gt;aa1-aa200&lt;/FONT&gt; by using a &lt;EM&gt;temporary&lt;/EM&gt; array (EDIT: unless &lt;FONT face="courier new,courier"&gt;aa1-aa200&lt;/FONT&gt; are already contained in dataset PRC):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array a{0:199} _temporary_;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Nov 2020 22:28:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rank-within-N-rows-moving-rank/m-p/701386#M214776</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-11-24T22:28:37Z</dc:date>
    </item>
    <item>
      <title>Re: Rank within N rows (moving rank)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rank-within-N-rows-moving-rank/m-p/701391#M214778</link>
      <description>&lt;P&gt;For one thing, you should insert the current value into the array before calculating percentiles&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Calculate moving percentiles for each window based on this record and previous 199 records */
data PRC; 
    set PRC;
	array a{0:199} _temporary_;
	a{mod(_n_,200)}=ROC1;
	if _n_ &amp;gt;= 200 then do;
		pct_low = pctl(75, of a{*}); 
		pct_mid = pctl(90, of a{*});	
		pct_high = pctl(95, of a{*});	
		pct_top = pctl(98, of a{*});
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(untested)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As for what you expect... you should tell us.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Nov 2020 22:42:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rank-within-N-rows-moving-rank/m-p/701391#M214778</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-11-24T22:42:54Z</dc:date>
    </item>
  </channel>
</rss>

