<?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: Ranking in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Ranking/m-p/365108#M86681</link>
    <description>&lt;P&gt;Also, what rank do you want to assign when there are ties in the x values?&lt;/P&gt;</description>
    <pubDate>Wed, 07 Jun 2017 17:31:04 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2017-06-07T17:31:04Z</dc:date>
    <item>
      <title>Ranking</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Ranking/m-p/364878#M86595</link>
      <description>&lt;P&gt;Hello dear SAS community.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I have a real struggle at the moment, and I'm running out of time.&amp;nbsp;&lt;BR /&gt;To make things short: I want for each year, rank all firms by X for that year. But only max "5 Ranks" fo reach year, and start in year 1968 for example, and repeat it each year.&lt;BR /&gt;For example:&amp;nbsp;&lt;BR /&gt;FirmID YEAR &amp;nbsp;VAR_X &amp;nbsp; WANT&amp;nbsp;&lt;BR /&gt;1000 &amp;nbsp; &amp;nbsp;1965 &amp;nbsp; &amp;nbsp; 9 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3 &amp;nbsp; 4 (69)&lt;BR /&gt;1000 &amp;nbsp; &amp;nbsp;1966 &amp;nbsp; &amp;nbsp;10 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2 &amp;nbsp;3(69)&lt;BR /&gt;1000 &amp;nbsp; &amp;nbsp;1967 &amp;nbsp; &amp;nbsp;11 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 2(69)&lt;BR /&gt;1000 &amp;nbsp; &amp;nbsp;1968 &amp;nbsp; &amp;nbsp;12 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1(69)&lt;BR /&gt;1000 &amp;nbsp; &amp;nbsp;1969 &amp;nbsp; &amp;nbsp;16 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;1000 &amp;nbsp; &amp;nbsp;1970 &amp;nbsp; &amp;nbsp;19&lt;BR /&gt;1000 &amp;nbsp; &amp;nbsp;1971 &amp;nbsp; &amp;nbsp;20&lt;BR /&gt;1000 &amp;nbsp; &amp;nbsp;1972 &amp;nbsp; &amp;nbsp;21 &amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;.&lt;BR /&gt;XXXX XXXX XXXX XXXX&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;BUT the problem starts when I'm doing this in year 1969, since it contains a "rank" variable in pre 68's.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2017 09:57:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Ranking/m-p/364878#M86595</guid>
      <dc:creator>TorTheHammer</dc:creator>
      <dc:date>2017-06-07T09:57:12Z</dc:date>
    </item>
    <item>
      <title>Re: Ranking</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Ranking/m-p/365023#M86650</link>
      <description>&lt;P&gt;What precisely does the WANT value&amp;nbsp; [&amp;nbsp; 3 4 (60) ] mean in the data line for 1965.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;FirmID YEAR &amp;nbsp;VAR_X &amp;nbsp; WANT&amp;nbsp;&lt;BR /&gt;1000 &amp;nbsp; &amp;nbsp;1965 &amp;nbsp; &amp;nbsp; 9 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3 &amp;nbsp; 4 (69)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It appears you want, for each year, to&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&amp;nbsp;Rank the preceding 4 ( preceding 5?) years.&amp;nbsp; I notice for the "(69)" you don't actually assign a rank value to the 1969 data.&lt;/LI&gt;
&lt;LI&gt;Assign those ranks to those preceding years.&lt;/LI&gt;
&lt;/OL&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;This means you would want, for each year t, 4 RANK vars&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;RANK_0 =&amp;nbsp;rank of X(t) among&amp;nbsp;&amp;nbsp;x{t-3}, x{t-2}, x{t-1}, x{t}&lt;/LI&gt;
&lt;LI&gt;RANK_1 = rank of X{t} among&amp;nbsp; x{t-2} x{t-1}, x{t}, x{t+1}&lt;/LI&gt;
&lt;LI&gt;...&lt;/LI&gt;
&lt;LI&gt;RANK_3 = rank of x{t} among&amp;nbsp; x{t}, t{t+1}, x{t+2}, x{t+3};&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is that what you mean?&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2017 14:50:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Ranking/m-p/365023#M86650</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-06-07T14:50:15Z</dc:date>
    </item>
    <item>
      <title>Re: Ranking</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Ranking/m-p/365024#M86651</link>
      <description>&lt;P&gt;I think it may help to provide a little more raw input data and how you want the result at the end of the process. You say "&amp;nbsp;for each year" but your example only has one year of ranks (apparently).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also please explain where you got your "want" values from. If 3 is supposed to be Rank, how did you get 3? The value of 9 for VAR_x is the smallest shown so I would expect it to be either 1 (if you want to rank or increasing values of var_x) or not ranked if looking at decreaseing values. And what is 4?&lt;/P&gt;
&lt;P&gt;How do you get 2 values of 1? Are you perhaps missing a bunch of different variables to hold multiple periods of consideration but didn't show them?&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2017 14:51:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Ranking/m-p/365024#M86651</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-06-07T14:51:13Z</dc:date>
    </item>
    <item>
      <title>Re: Ranking</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Ranking/m-p/365108#M86681</link>
      <description>&lt;P&gt;Also, what rank do you want to assign when there are ties in the x values?&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2017 17:31:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Ranking/m-p/365108#M86681</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-06-07T17:31:04Z</dc:date>
    </item>
    <item>
      <title>Re: Ranking</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Ranking/m-p/365145#M86694</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The DATA WANT step below does what I think you want.&amp;nbsp; If there are ties among the X's being ranked, then it produces the average of the lowest and highest rank assigned to the X value:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It works by:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Reading in each record for a gien FIRMID and storing X in an array, indexed on year,&amp;nbsp;with a lowest bound 1960 and highest bound of 2010.&amp;nbsp; This assumes that the lowest observable year is 1963 and the highest observed year is 2010.&lt;/LI&gt;
&lt;LI&gt;When the end of a firmid is encountered, then, in a loop,&amp;nbsp;reread each year, and capture the rank of its X value versus (1) years YEAR-3 through YEAR for RANK_), (2) years YEAR-2 through YEAR+1 for RANK_1, etc.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input FirmID YEAR  X;
  output;
  firmid=2000; if mod(_n_,2)=0 then x=x-2; output;
datalines;
1000    1965     9
1000    1966    10
1000    1967    11
1000    1968    12
1000    1969    16           
1000    1970    19
1000    1971    20
1000    1972    21  
run;
proc sort; by firmid year;run;


data want (drop=_:);
  array _x{1960:2010} _temporary_;
  set have (keep=firmid year x );
  by firmid;
  if first.firmid then call missing(of _x{*});
  _x{year}=x;   /* Fill the _X array */

  label rank_0 = "Rank of current X for Current Year-3 through Current Year"
        rank_1 = "Rank of current X for Current Year-2 through Current Year+1"
        rank_2 = "Rank of current X for Current Year-1 through Current Year+2"
        rank_3 = "Rank of current X for Current Year   through Current Year+3" ;
  array rnk{0:3} rank_0-rank_3;

  if last.firmid then do until (last.firmid);
    set have ;
    by firmid;
    do _r=0 to 3;
      do                     _rank_beg=1         by 1 until (largest(_rank_beg  ,_x{year+_r-3},_x{year+_r-2},_x{year+_r-1},_x{year+_r-0})=x);end;
      if _rank_beg&amp;lt;4 then do _rank_end=_rank_beg to 3 while (largest(_rank_end+1,_x{year+_r-3},_x{year+_r-2},_x{year+_r-1},_x{year+_r-0})=x);end;
      else _rank_end=4;
      rnk{_r}=(_rank_beg+_rank_end)/2;
    end;
    output;
  end;
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;Editted addition.&amp;nbsp; To deal with possible&amp;nbsp;ties&amp;nbsp;for X, you see two consecutive loops&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DO _RANK_BEG=&amp;nbsp;1 by 1 until (.....);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IF _RANK_BEG&amp;lt;4 then DO _RANK_END=_RANK_BEG to 3 WHILE (.....);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you expect no ties in X, you could replace them with a single loop:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;&amp;nbsp; DO&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; RNK{_r}=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="SAS Monospace" size="2"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;by&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#008080" face="SAS Monospace" size="2"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;until&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; (largest(rnk{_r},_x{year+_r-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="SAS Monospace" size="2"&gt;3&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="SAS Monospace" size="2"&gt;},_x{year+_r-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="SAS Monospace" size="2"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="SAS Monospace" size="2"&gt;},_x{year+_r-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="SAS Monospace" size="2"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="SAS Monospace" size="2"&gt;},_x{year+_r-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="SAS Monospace" size="2"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="SAS Monospace" size="2"&gt;})=x);&lt;/FONT&gt;&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>Wed, 07 Jun 2017 19:07:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Ranking/m-p/365145#M86694</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-06-07T19:07:48Z</dc:date>
    </item>
    <item>
      <title>Re: Ranking</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Ranking/m-p/365187#M86707</link>
      <description>&lt;P&gt;Yes, this is what I want. Sorry for bad "writing" about it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Sorry for asking rude, but can you help me about it?:)&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2017 22:36:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Ranking/m-p/365187#M86707</guid>
      <dc:creator>TorTheHammer</dc:creator>
      <dc:date>2017-06-07T22:36:27Z</dc:date>
    </item>
  </channel>
</rss>

