<?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: max 3 values in a column of total grades and create a new variable with the best 3 grades in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/max-3-values-in-a-column-of-total-grades-and-create-a-new/m-p/590605#M169064</link>
    <description>&lt;P&gt;proc univariate would give you the max, use an ods statement to throw the value into a dataset. To get the max three values of a column (ie a variable) use proc rank: &lt;A href="http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#rank-overview.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#rank-overview.htm&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Sat, 21 Sep 2019 13:19:24 GMT</pubDate>
    <dc:creator>pau13rown</dc:creator>
    <dc:date>2019-09-21T13:19:24Z</dc:date>
    <item>
      <title>max 3 values in a column of total grades and create a new variable with the best 3 grades</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-3-values-in-a-column-of-total-grades-and-create-a-new/m-p/590600#M169059</link>
      <description>&lt;P&gt;I want to identify max 3 values in a column of total grades and create a new variable containing "Top one", "Second" and "third" only then print it. I tried the following code but it put "." in both&amp;nbsp;Total_Grades&amp;nbsp; and&amp;nbsp;Top_three variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data Grades_Addeed_top3;
set work.Grades;
if Total_Grades = max then Top_three=1;
else&amp;nbsp;if (Total_Grades = (max-1)) then Top_three=2;
else&amp;nbsp;if (Total_Grades = (max-2)) then Top_three=3;
else Top_three=.;
run;

proc print data=proc print data=work.diving;
run;&lt;/PRE&gt;&lt;P&gt;Any advice will be greatly appreciated;&lt;/P&gt;&lt;P&gt;M&lt;/P&gt;</description>
      <pubDate>Sat, 21 Sep 2019 13:10:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-3-values-in-a-column-of-total-grades-and-create-a-new/m-p/590600#M169059</guid>
      <dc:creator>mrahouma</dc:creator>
      <dc:date>2019-09-21T13:10:00Z</dc:date>
    </item>
    <item>
      <title>Re: max 3 values in a column of total grades and create a new variable with the best 3 grades</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-3-values-in-a-column-of-total-grades-and-create-a-new/m-p/590605#M169064</link>
      <description>&lt;P&gt;proc univariate would give you the max, use an ods statement to throw the value into a dataset. To get the max three values of a column (ie a variable) use proc rank: &lt;A href="http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#rank-overview.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#rank-overview.htm&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 21 Sep 2019 13:19:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-3-values-in-a-column-of-total-grades-and-create-a-new/m-p/590605#M169064</guid>
      <dc:creator>pau13rown</dc:creator>
      <dc:date>2019-09-21T13:19:24Z</dc:date>
    </item>
    <item>
      <title>Re: max 3 values in a column of total grades and create a new variable with the best 3 grades</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-3-values-in-a-column-of-total-grades-and-create-a-new/m-p/590614#M169071</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/288813"&gt;@mrahouma&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Maybe try this step-by-step approach?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All the best&lt;/P&gt;&lt;P&gt;Bart&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.Grades;
  call streaminit(11);
  do _N_ = 1 to 10;
    Total_Grades = int(rand('uniform')*100);
    some_other_variable = rand('uniform');
    output;
  end;
run;
proc print;
run;

data  GradesV;
  set Grades(keep=Total_Grades) curobs=co; /* to keep original order */
  curobs=co;
run;

proc sort data = GradesV out = GradesV;
  by descending Total_Grades;
run; 

data GradesV;
  set GradesV;
  by descending Total_Grades;  
  if first.Total_Grades then _TEMP_ + 1; 
  drop _TEMP_;
  if _TEMP_ &amp;lt; 4 then Top_three = _TEMP_;
run;

proc sort data = GradesV out = GradesV(drop = curobs);
  by curobs;
run;

data Total_Grades / view = Total_Grades;
  set Grades ;
  set GradesV;
run;

proc print data=Total_Grades;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 21 Sep 2019 14:24:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-3-values-in-a-column-of-total-grades-and-create-a-new/m-p/590614#M169071</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2019-09-21T14:24:28Z</dc:date>
    </item>
    <item>
      <title>Re: max 3 values in a column of total grades and create a new variable with the best 3 grades</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-3-values-in-a-column-of-total-grades-and-create-a-new/m-p/590641#M169089</link>
      <description>&lt;P&gt;There's a PROC for that &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try PROC RANK, which will create the ranks for the variable. If you want just the top 3, filter those out, however you should consider how you'll handle ties. If values are tied it averages the ranks so that can be a slightly harder problem. If you need to consider that, you need to filter it out based on the ranks, which I've done at the bottom of the program. Adapt as needed.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*Create ranks for variables value;
proc rank data=sashelp.class out=class_ranked 
/*      (where= (rank_weight &amp;lt; 4)) */
     ;
var weight;
ranks rank_weight;
run;

*display output;
title 'Unsorted but ranked';
proc print data=class_ranked;
run;

*sort for display;
proc sort data=class_ranked;
by rank_weight;
run;

title 'Sorted and ranked';
proc print data=class_ranked;
run;

*filter out the top 3 regardless of the number of ties;
data final;
set class_ranked;
by rank_weight;
if first.rank_weight then group + 1;
if group &amp;lt;= 3;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/288813"&gt;@mrahouma&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I want to identify max 3 values in a column of total grades and create a new variable containing "Top one", "Second" and "third" only then print it. I tried the following code but it put "." in both&amp;nbsp;Total_Grades&amp;nbsp; and&amp;nbsp;Top_three variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data Grades_Addeed_top3;
set work.Grades;
if Total_Grades = max then Top_three=1;
else&amp;nbsp;if (Total_Grades = (max-1)) then Top_three=2;
else&amp;nbsp;if (Total_Grades = (max-2)) then Top_three=3;
else Top_three=.;
run;

proc print data=proc print data=work.diving;
run;&lt;/PRE&gt;
&lt;P&gt;Any advice will be greatly appreciated;&lt;/P&gt;
&lt;P&gt;M&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 21 Sep 2019 18:44:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-3-values-in-a-column-of-total-grades-and-create-a-new/m-p/590641#M169089</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-09-21T18:44:51Z</dc:date>
    </item>
    <item>
      <title>Re: max 3 values in a column of total grades and create a new variable with the best 3 grades</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-3-values-in-a-column-of-total-grades-and-create-a-new/m-p/590664#M169100</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/288813"&gt;@mrahouma&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC SUMMARY allows for another fairly short solution, provided that your Grades dataset contains a variable which uniquely identifies an observation, such as NAME in SASHELP.CLASS:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=sashelp.class;
output out=top3 idgrp(max(weight) out[3] (name)=_);
run;

data want(drop=_:);
if _n_=1 then set top3;
set sashelp.class;
_w=whichc(name, of __:);
Top_three=ifn(_w,_w,.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: Admittedly, with PROC RANK (as suggested by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;) it could still be shorter. The &lt;A href="https://documentation.sas.com/?docsetId=proc&amp;amp;docsetTarget=p16s2o8e4bnqrin1phywxdaxqba7.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en#p1o4x5q2nuqqx1n1kjks8hktpy5o" target="_blank" rel="noopener"&gt;DESCENDING option&lt;/A&gt; of the PROC RANK statement would help selecting the largest values. The two procedures differ in the options how tied values are dealt with. For example, in PROC SUMMARY you could specify one or more variables to serve as "tie-breakers."&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 21 Sep 2019 23:16:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-3-values-in-a-column-of-total-grades-and-create-a-new/m-p/590664#M169100</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-09-21T23:16:25Z</dc:date>
    </item>
  </channel>
</rss>

