<?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: How do I rank by group within a variable while retaining rank for one observation? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-rank-by-group-within-a-variable-while-retaining-rank/m-p/482844#M125138</link>
    <description>&lt;P&gt;Yup! That helps out big time. Thanks!&lt;/P&gt;</description>
    <pubDate>Tue, 31 Jul 2018 14:52:24 GMT</pubDate>
    <dc:creator>Ani7</dc:creator>
    <dc:date>2018-07-31T14:52:24Z</dc:date>
    <item>
      <title>How do I rank by group within a variable while retaining rank for one observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-rank-by-group-within-a-variable-while-retaining-rank/m-p/482789#M125123</link>
      <description>&lt;P&gt;Hi, new SAS programmer here using SAS EG. The &lt;STRONG&gt;data I have&lt;/STRONG&gt; is in the following format:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;User_ID | Name | Volume&lt;/P&gt;&lt;P&gt;&lt;FONT face="helvetica"&gt;A12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; | John&amp;nbsp; &amp;nbsp;| 345&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="helvetica"&gt;A12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; | Sam&amp;nbsp; &amp;nbsp;| 243&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="helvetica"&gt;&lt;SPAN&gt;A12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |&amp;nbsp;Rain &amp;nbsp; | 120&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="helvetica"&gt;&lt;SPAN&gt;A12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |&amp;nbsp;Carl&amp;nbsp; &amp;nbsp; | 5&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="helvetica"&gt;&lt;SPAN&gt;A12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |&amp;nbsp;Joe&amp;nbsp; &amp;nbsp; | 10&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="helvetica"&gt;&lt;SPAN&gt;A12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |&amp;nbsp;Bain &amp;nbsp; |&amp;nbsp;600&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="helvetica"&gt;&lt;SPAN&gt;A32&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; | Sam&amp;nbsp; &amp;nbsp;| 243&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="helvetica"&gt;&lt;SPAN&gt;A32&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |&amp;nbsp;Rain &amp;nbsp; | 450&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="helvetica"&gt;&lt;SPAN&gt;I am trying to create a rank variable that ranks&amp;nbsp;Name by descending volume for each User_ID such that Sam always retains the number 1 rank. So the &lt;STRONG&gt;data I want&lt;/STRONG&gt; is:&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;User_ID | Name | Volume | Rank&lt;/P&gt;&lt;P&gt;&lt;FONT face="helvetica"&gt;A12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; | Sam&amp;nbsp; &amp;nbsp;| 243&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;| 1&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="helvetica"&gt;A12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |&amp;nbsp;Bain &amp;nbsp; |&amp;nbsp;600&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;| 2&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="helvetica"&gt;A12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; | John&amp;nbsp; &amp;nbsp;| 345&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;| 3&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="helvetica"&gt;&lt;SPAN&gt;A12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |&amp;nbsp;Rain&amp;nbsp;&amp;nbsp; | 120&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;| 4&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="helvetica"&gt;&lt;SPAN&gt;A12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |&amp;nbsp;Joe&amp;nbsp; &amp;nbsp; | 10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; | 5&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="helvetica"&gt;&lt;SPAN&gt;A12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |&amp;nbsp;Carl&amp;nbsp; &amp;nbsp; | 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;| 6&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="helvetica"&gt;&lt;SPAN&gt;A32&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; | Sam&amp;nbsp; &amp;nbsp;| 243&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;| 1&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="helvetica"&gt;&lt;SPAN&gt;A32&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |&amp;nbsp;Rain&amp;nbsp;&amp;nbsp; | 450&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;| 2&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="helvetica"&gt;The code I am using is:&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2;
set have1;
if Name = "Sam" then Rank = 1;
proc sort; 
by descending Rank descending Volume ;
run;

data have3;
set have2;
retain Rank1;
Rank1 + 1;
run;

data want;
set have3;
if missing(Rank) then Rank = Rank1;
drop Rank1;
proc sort; 
by Name;
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;This seems to work only in situations where I am considering one User_ID. I need to apply this to about 100k User_IDs.&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jul 2018 13:02:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-rank-by-group-within-a-variable-while-retaining-rank/m-p/482789#M125123</guid>
      <dc:creator>Ani7</dc:creator>
      <dc:date>2018-07-31T13:02:17Z</dc:date>
    </item>
    <item>
      <title>Re: How do I rank by group within a variable while retaining rank for one observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-rank-by-group-within-a-variable-while-retaining-rank/m-p/482793#M125126</link>
      <description>&lt;P&gt;Is Sam always the first name in a group of IDs?&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jul 2018 13:06:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-rank-by-group-within-a-variable-while-retaining-rank/m-p/482793#M125126</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-07-31T13:06:32Z</dc:date>
    </item>
    <item>
      <title>Re: How do I rank by group within a variable while retaining rank for one observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-rank-by-group-within-a-variable-while-retaining-rank/m-p/482829#M125133</link>
      <description>No, Sam can be in any location in the data that I have. I want it to be the first name with a Rank of 1 at the end of the process.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 31 Jul 2018 14:08:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-rank-by-group-within-a-variable-while-retaining-rank/m-p/482829#M125133</guid>
      <dc:creator>Ani7</dc:creator>
      <dc:date>2018-07-31T14:08:42Z</dc:date>
    </item>
    <item>
      <title>Re: How do I rank by group within a variable while retaining rank for one observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-rank-by-group-within-a-variable-while-retaining-rank/m-p/482831#M125135</link>
      <description>&lt;P&gt;Why not make a dummy variable ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; have2&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt; have1&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; Name &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;"Sam"&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; _Volume &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; 99999999&lt;SPAN class="token punctuation"&gt;;&lt;BR /&gt; else _Volume=Volume;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;proc&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;sort&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; 
&lt;SPAN class="token statement"&gt;by&lt;/SPAN&gt; descending &lt;SPAN class="token procnames"&gt;Rank&lt;/SPAN&gt; descending &lt;STRONG&gt;_Volume&lt;/STRONG&gt; &lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE class=" language-sas"&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jul 2018 14:10:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-rank-by-group-within-a-variable-while-retaining-rank/m-p/482831#M125135</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-07-31T14:10:30Z</dc:date>
    </item>
    <item>
      <title>Re: How do I rank by group within a variable while retaining rank for one observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-rank-by-group-within-a-variable-while-retaining-rank/m-p/482844#M125138</link>
      <description>&lt;P&gt;Yup! That helps out big time. Thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jul 2018 14:52:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-rank-by-group-within-a-variable-while-retaining-rank/m-p/482844#M125138</guid>
      <dc:creator>Ani7</dc:creator>
      <dc:date>2018-07-31T14:52:24Z</dc:date>
    </item>
    <item>
      <title>Re: How do I rank by group within a variable while retaining rank for one observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-rank-by-group-within-a-variable-while-retaining-rank/m-p/482851#M125139</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use "CASE WHEN" in proc sql to define a custom sort order :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input User_ID $ Name $ Volume;
cards;
A12 John 345
A12 Sam 243
A12 Rain 120
A12 Carl 5
A12 Joe 10
A12 Bain 600
A32 Sam 243
A32 Rain 450
;
run;

proc sql;
    CREATE TABLE have_s AS
    SELECT *
    FROM have
    ORDER BY User_Id, CASE WHEN upcase(NAME)="SAM" THEN 1 ELSE 2 END, VOLUME DESCENDING
    ;
quit;

data want;
    set have_s;
    by User_Id;

    retain rank;
    if first.User_Id then rank=1;
    else rank=rank+1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 31 Jul 2018 15:24:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-rank-by-group-within-a-variable-while-retaining-rank/m-p/482851#M125139</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2018-07-31T15:24:04Z</dc:date>
    </item>
  </channel>
</rss>

