<?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: Help with Ranking in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Ranking/m-p/701409#M214789</link>
    <description>&lt;P&gt;The problem is that you are calculating the percentiles PRIOR to assigning value to an element of array a.&amp;nbsp; Therefore the last observation of the first instance is generating percentiles based on 199 values ranging from 1 to 199.&amp;nbsp; It ignores the value=200.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the second instance is generating percentiles based on 199 values of 200, followed by 2 through 199.&amp;nbsp; It ignores the 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why are you assigning values to the array elements prior to percentile calclulation?&lt;/P&gt;</description>
    <pubDate>Wed, 25 Nov 2020 01:07:20 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2020-11-25T01:07:20Z</dc:date>
    <item>
      <title>Help with Ranking</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Ranking/m-p/701404#M214785</link>
      <description>&lt;P&gt;Hi Everyone,&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 rank will be 1 to 200)&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 I think I did something wrong.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For illustration purpose, I create a list of value of 1 to 200.&lt;/P&gt;
&lt;P&gt;In the first code, when data is sorted in ascending order, the code identifies correctly these 150,180, 190, 196 values for the last row.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, when I switch position for number 1 and 200, the code Incorrectly identify the percentile value (151 181 191 197) for the last row.&lt;/P&gt;
&lt;P&gt;My point is that the change I make is just the order and therefore at the last row, the code still look at the full 200 values as before, and it should return the same percentile value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone please help me to fix my code?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*NUMBERS ARE IN ORDER --&amp;gt; GOOD;
data have;
do value=1 to 200;
output;
end;
run;

data want; set have;
	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)}=value;
run;



*SWICH POSITION OF NUMBER 1 and 200 --&amp;gt; NOT GOOD;
data have2; set have;
if _N_=200 then value=1;
if _N_=1 then value=200;run;


data want2; set have2;
	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)}=value;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Nov 2020 00:17:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Ranking/m-p/701404#M214785</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2020-11-25T00:17:37Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Ranking</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Ranking/m-p/701409#M214789</link>
      <description>&lt;P&gt;The problem is that you are calculating the percentiles PRIOR to assigning value to an element of array a.&amp;nbsp; Therefore the last observation of the first instance is generating percentiles based on 199 values ranging from 1 to 199.&amp;nbsp; It ignores the value=200.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the second instance is generating percentiles based on 199 values of 200, followed by 2 through 199.&amp;nbsp; It ignores the 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why are you assigning values to the array elements prior to percentile calclulation?&lt;/P&gt;</description>
      <pubDate>Wed, 25 Nov 2020 01:07:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Ranking/m-p/701409#M214789</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-11-25T01:07:20Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Ranking</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Ranking/m-p/701411#M214791</link>
      <description>&lt;P&gt;This gives the same result for both tables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT; 
  set HAVE end=LASTOBS;
  array A{200} _temporary_;
  A[_N_]=VALUE;
  if LASTOBS;
  PCT_75=pctl(75,of A{*}); 
  PCT_90=pctl(90,of A{*});	
  PCT_95=pctl(95,of A{*});	
  PCT_98=pctl(98,of A{*});
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It is also much more efficient.&lt;/P&gt;
&lt;P&gt;Please do not use tabs, the code you pasted is not formatted properly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Nov 2020 01:12:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Ranking/m-p/701411#M214791</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-11-25T01:12:05Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Ranking</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Ranking/m-p/701413#M214793</link>
      <description>&lt;P&gt;So I move the array definition above the pct and the result are the same now.&lt;/P&gt;
&lt;P&gt;However, the numbers are 150.5 180.5 190.5 and 196.5, still not the exact value yet of&amp;nbsp;&lt;SPAN&gt;(151 181 191 197)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want2; set have2;
	*drop aa1-aa200;

	array a{0:199} aa1-aa200;

	a{mod(_n_,200)}=value;

	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{*});

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Nov 2020 01:38:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Ranking/m-p/701413#M214793</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2020-11-25T01:38:22Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Ranking</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Ranking/m-p/701417#M214796</link>
      <description>&lt;P&gt;If you only moved the assignment statement in the DATA WANT2 step but not the DATA WANT step, then you should not expect the same results.&amp;nbsp; Because the DATA WANT2 step is evaluating 200 obs (200, then 2 through 199, then 1), while the DATA WANT step is still only evaluating 199 values (1 ascending to 199).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In short, data order IS NOT affecting the calculation of percentiles.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Nov 2020 01:50:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Ranking/m-p/701417#M214796</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-11-25T01:50:51Z</dc:date>
    </item>
  </channel>
</rss>

