<?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: PROC RANK group within groups in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-RANK-group-within-groups/m-p/958986#M374236</link>
    <description>&lt;P&gt;I do not follow.&lt;/P&gt;
&lt;P&gt;Do you want to rank by the values of NUMUSEFLAG within the groups defined by MONTHGROUP?&lt;/P&gt;
&lt;P&gt;You have 5 levels of MONTHGROUP and only 9 observations.&amp;nbsp; So not much to rank in this example.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1739318516555.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104549iB7A78CB10940C1AA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1739318516555.png" alt="Tom_0-1739318516555.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sounds more like the&amp;nbsp; issue is how to sort by something other than the values in your variables.&amp;nbsp; The answer to that is to make a new variable (or variables) that can be used to sort.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example if you want missing values sorted first&amp;nbsp; but the non missing values sorted descending here are a couple of options.&lt;/P&gt;
&lt;P&gt;1) Make a new variable that indicates if the value is missing or not and include both variables when sorting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) Make a new variable with the negative of the current value and then sort ascending by that variable instead of sorting by the original variable.&amp;nbsp; So the missing values will still be first.&amp;nbsp; But since the largest positive value is the smallest negative value then sorting them in ascending order will get what you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the goal is not to "rank" the values, but instead to just number them then once you have the observations in the order you want you can just use simple data step to number them.&lt;/P&gt;</description>
    <pubDate>Wed, 12 Feb 2025 00:14:29 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2025-02-12T00:14:29Z</dc:date>
    <item>
      <title>PROC RANK group within groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-RANK-group-within-groups/m-p/958720#M374151</link>
      <description>&lt;P&gt;I am having trouble ranking a variable within groups.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc rank data=MONTHRANK out=FLAGRANK ties=dense; /* Ranking UseFlag within each month with TIES=DENSE */
	by ID TYPE descending MonthGroup NumUseFlag;
    var NumUseFlag;
    ranks FlagRank;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the initial table&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data MONTHRANK;
  length ID $12 Type $3 AdjustmentCode MonthGroup MonthRank NumUseFlag 8
  ;
  infile datalines dsd truncover;
  input ID -- NumUseFlag;
datalines;
123AAABBBC,001,,14,1,1
123AAABBBC,001,3,13,1,0
123AAABBBC,001,2,13,2,1
123AAABBBC,001,,13,3,1
123AAABBBC,001,26,12,1,1
123AAABBBC,001,,12,2,1
123AAABBBC,001,,11,1,1
123AAABBBC,001,,10,1,1
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here is what I want to output&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data MONTHRANK_OUTPUT;
  length ID $12 Type $3 AdjustmentCode MonthGroup MonthRank NumUseFlag FlagRank 8
  ;
  infile datalines dsd truncover;
  input ID -- FlagRank;
datalines;
123AAABBBC,001,,14,1,1,1
123AAABBBC,001,3,13,1,0,1
123AAABBBC,001,2,13,2,1,1
123AAABBBC,001,,13,3,1,2
123AAABBBC,001,26,12,1,1,1
123AAABBBC,001,,12,2,1,2
123AAABBBC,001,,11,1,1,1
123AAABBBC,001,,10,1,1,1
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Sorry if the code isn't exactly correct, I hope this is clear enough.&lt;/P&gt;&lt;P&gt;The error I am getting is "ERROR: Data set WORK.MONTHRANK is not sorted in ascending sequence. The current BY group has NumUseFlag = 1 and the next BY group has NumUseFlag = 0.", which kind of makes sense to me if there were another adjustment line above the line in the 13th month with the NumUseFlag = 1 instead of 0 and the AdjustmentCode = 26 (this is possible within the rest of the data), but I don't really see why that should necessarily matter and I'm unsure of how to get around the issue.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried using descending, but I get the same error just saying it's not sorted in descending sequence. I have also tried using CLASS, but I don't think it works with PROC RANK.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Feb 2025 22:22:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-RANK-group-within-groups/m-p/958720#M374151</guid>
      <dc:creator>wh95</dc:creator>
      <dc:date>2025-02-07T22:22:25Z</dc:date>
    </item>
    <item>
      <title>Re: PROC RANK group within groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-RANK-group-within-groups/m-p/958722#M374152</link>
      <description>Did you sort your data before using PROC RANK?  In general, you cannot use a BY statement in a PROC or a DATA step if the incoming data are not already sorted in a way that matches your BY statement.  Just use PROC SORT first.</description>
      <pubDate>Fri, 07 Feb 2025 21:50:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-RANK-group-within-groups/m-p/958722#M374152</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-02-07T21:50:09Z</dc:date>
    </item>
    <item>
      <title>Re: PROC RANK group within groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-RANK-group-within-groups/m-p/958729#M374153</link>
      <description>&lt;P&gt;I'm sorry, I have edited my original post to also include the AdjustmentCode which is sorted in descending order.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sort data=MONTHRANK;
	by MBI PBP descending MonthGroup descending AdjustmentReasonCode;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is how MONTHRANK is sorted prior to attempting PROC RANK. I believe that there is a case in the data where, for example, a line above the UseFlag = 0 line has AdjustmentCode = 26 and NumUseFlag = 1, so that the order of NumUseFlag goes 1,0,1,1 within MonthGroup = 13.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Feb 2025 22:28:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-RANK-group-within-groups/m-p/958729#M374153</guid>
      <dc:creator>wh95</dc:creator>
      <dc:date>2025-02-07T22:28:00Z</dc:date>
    </item>
    <item>
      <title>Re: PROC RANK group within groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-RANK-group-within-groups/m-p/958737#M374156</link>
      <description>&lt;P&gt;There is nothing to RANK if you include the VARiable to be ranked as part of the BY grouping.&lt;/P&gt;
&lt;P&gt;Remove NUMUSEFLAG from the BY statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data MONTHRANK;
  input ID :$12. Type :$3. AdjustmentCode MonthGroup MonthRank NumUseFlag;
datalines;
123AAABBBC 001  . 14 1 1
123AAABBBC 001  3 13 1 0
123AAABBBC 001  2 13 2 1
123AAABBBC 001  . 13 3 1
123AAABBBC 001 26 12 1 1
123AAABBBC 001  . 12 2 1
123AAABBBC 001  . 11 1 1
123AAABBBC 001  . 10 1 1
;

proc sort data=MONTHRANK ;
  by ID TYPE descending MonthGroup NumUseFlag;
run;

/* Ranking UseFlag within each month with TIES=DENSE */
proc rank data=MONTHRANK out=FLAGRANK ties=dense; 
  by ID TYPE descending MonthGroup ;
  var NumUseFlag;
  ranks FlagRank;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1738974094863.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104496iDCC9DD627079E175/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1738974094863.png" alt="Tom_0-1738974094863.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 08 Feb 2025 00:21:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-RANK-group-within-groups/m-p/958737#M374156</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-02-08T00:21:44Z</dc:date>
    </item>
    <item>
      <title>Re: PROC RANK group within groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-RANK-group-within-groups/m-p/958816#M374198</link>
      <description>Yes, thank you, I was able to find this answer, but it does not satisfy the question unfortunately. I would like to see the FlagRank column output 1,1,1,2,1,2,1,1 in the table you created. I want the grouping to be within each MonthGroup based on the NumUseFlag, but the data must also be sorted in the format you have shown.</description>
      <pubDate>Mon, 10 Feb 2025 16:17:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-RANK-group-within-groups/m-p/958816#M374198</guid>
      <dc:creator>wh95</dc:creator>
      <dc:date>2025-02-10T16:17:11Z</dc:date>
    </item>
    <item>
      <title>Re: PROC RANK group within groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-RANK-group-within-groups/m-p/958820#M374201</link>
      <description>&lt;P&gt;I do not understand.&amp;nbsp; What sorting do you mean?&lt;/P&gt;
&lt;P&gt;If you want to sort data use PROC SORT.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2025 16:46:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-RANK-group-within-groups/m-p/958820#M374201</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-02-10T16:46:46Z</dc:date>
    </item>
    <item>
      <title>Re: PROC RANK group within groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-RANK-group-within-groups/m-p/958980#M374235</link>
      <description>&lt;P&gt;Below, I have modified the table from my first post.&lt;/P&gt;&lt;P&gt;Here is what I want to output&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data MONTHRANK_OUTPUT;
  length ID $12 Type $3 AdjustmentCode MonthGroup MonthRank NumUseFlag FlagRank 8
  ;
  infile datalines dsd truncover;
  input ID -- FlagRank;
datalines;
123AAABBBC,001,,14,1,1,1
123AAABBBC,001,3,13,1,0,1
123AAABBBC,001,2,13,2,1,1
123AAABBBC,001,,13,3,1,2
123AAABBBC,001,26,12,1,1,1&lt;BR /&gt;123AAABBBC,001,3,12,2,0,1
123AAABBBC,001,,12,3,1,2
123AAABBBC,001,,11,1,1,1
123AAABBBC,001,,10,1,1,1
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;All I was saying was that I need AdjustmentCode to be sorted in descending order while also creating the FlagRank based on the NumUseFlag in each MonthGroup. I think that what I am asking to do is honestly just not possible in SAS, at least not without some very involved solution that I don't have enough experience to find. At the end of the day, I need to put the most recent data in the top most row in each MonthGroup. Generally, a higher AdjustmentCode means it is more recent, but certain codes, such as 3, mean that I do not want to use the data in that row at all. My idea is that by creating these rankings and flags, I can filter the data to what I actually want to use. The issue that I'm having is that there are cases where the NumUseFlag is not always sorted in a specific ascending/descending order within each MonthGroup. In the example output I gave, this is the case in MonthGroup 12.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Feb 2025 21:54:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-RANK-group-within-groups/m-p/958980#M374235</guid>
      <dc:creator>wh95</dc:creator>
      <dc:date>2025-02-11T21:54:25Z</dc:date>
    </item>
    <item>
      <title>Re: PROC RANK group within groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-RANK-group-within-groups/m-p/958986#M374236</link>
      <description>&lt;P&gt;I do not follow.&lt;/P&gt;
&lt;P&gt;Do you want to rank by the values of NUMUSEFLAG within the groups defined by MONTHGROUP?&lt;/P&gt;
&lt;P&gt;You have 5 levels of MONTHGROUP and only 9 observations.&amp;nbsp; So not much to rank in this example.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1739318516555.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104549iB7A78CB10940C1AA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1739318516555.png" alt="Tom_0-1739318516555.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sounds more like the&amp;nbsp; issue is how to sort by something other than the values in your variables.&amp;nbsp; The answer to that is to make a new variable (or variables) that can be used to sort.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example if you want missing values sorted first&amp;nbsp; but the non missing values sorted descending here are a couple of options.&lt;/P&gt;
&lt;P&gt;1) Make a new variable that indicates if the value is missing or not and include both variables when sorting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) Make a new variable with the negative of the current value and then sort ascending by that variable instead of sorting by the original variable.&amp;nbsp; So the missing values will still be first.&amp;nbsp; But since the largest positive value is the smallest negative value then sorting them in ascending order will get what you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the goal is not to "rank" the values, but instead to just number them then once you have the observations in the order you want you can just use simple data step to number them.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Feb 2025 00:14:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-RANK-group-within-groups/m-p/958986#M374236</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-02-12T00:14:29Z</dc:date>
    </item>
    <item>
      <title>Re: PROC RANK group within groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-RANK-group-within-groups/m-p/959060#M374253</link>
      <description>&lt;P&gt;This is what I have&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="wh95_0-1739375324656.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104568iCA5CCF54F96B56F0/image-size/medium?v=v2&amp;amp;px=400" role="button" title="wh95_0-1739375324656.png" alt="wh95_0-1739375324656.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;This is what I want&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="wh95_1-1739375351376.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104570i5A4262CDE581F35B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="wh95_1-1739375351376.png" alt="wh95_1-1739375351376.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Feb 2025 15:49:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-RANK-group-within-groups/m-p/959060#M374253</guid>
      <dc:creator>wh95</dc:creator>
      <dc:date>2025-02-12T15:49:35Z</dc:date>
    </item>
    <item>
      <title>Re: PROC RANK group within groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-RANK-group-within-groups/m-p/959064#M374255</link>
      <description>&lt;P&gt;But what is the RULE?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the problem is you want to preserve the current order, but do not have vairables you can use to do that with PROC SORT, then add a new variable that does.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data step1;
   row+1;
   set have;
run;

proc sort data=step1;
  by monthgroup ;
run;

proc rank data=step1 out=want ties=dense;
  by monthgroup;
  var numuseflag;
  rank flagrank;
run;

proc sort data=want;
  by row;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Feb 2025 16:02:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-RANK-group-within-groups/m-p/959064#M374255</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-02-12T16:02:36Z</dc:date>
    </item>
  </channel>
</rss>

