<?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: Calculate the highest two records of Metrc2 for each date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-the-highest-two-records-of-Metrc2-for-each-date/m-p/497128#M131654</link>
    <description>&lt;P&gt;see if this helps&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data tempdata;

Input obs Date $ Age Salary Metric2;
Datalines;
1 JAN1999 56 78 14
2 FEB1999 32 109 14
3 FEB1999 35 106 14
4 FEB1999 47 115 22
5 MAY1999 55 121 31
6 JUN1999 61 114 31
7 JUL1999 67 102 72
8 AUG1999 64 76 72
9 JUL1999 78 77 54
10 JUL1999 81 62 47
11 JUL1999 84 31 52
12 JUL1999 2 44 55
13 JAN2000 37 91 83
14 FEB2000 41 102 27
15 FEB2000 52 98 19
16 FEB2000 61 118 22
17 FEB2000 49 88 29
18 FEB2000 24 101 54
19 JUL2000 45 91 69
20 JUL2000 63 65 53
21 SEP2000 60 49 68
22 JUL2000 78 70 41
23 JUL2000 82 44 58
24 JUL2000 93 57 47
;

proc rank data=tempdata out=want(where=(Metric2rank in (1,2))) ties=low descending;
	
   by Date notsorted;
 
   var Metric2;
   ranks Metric2rank ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 19 Sep 2018 20:32:25 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-09-19T20:32:25Z</dc:date>
    <item>
      <title>Calculate the highest two records of Metrc2 for each date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-the-highest-two-records-of-Metrc2-for-each-date/m-p/497108#M131644</link>
      <description>&lt;P&gt;I have a Data like this and i want to calculate the two highest records for every date&lt;/P&gt;&lt;P&gt;Data tempdata;&lt;/P&gt;&lt;P&gt;Input obs Date $ Age Salary Metric2;&lt;BR /&gt;Datalines;&lt;BR /&gt;1 JAN1999 56 78 14&lt;BR /&gt;2 FEB1999 32 109 14&lt;BR /&gt;3 FEB1999 35 106 14&lt;BR /&gt;4 FEB1999 47 115 22&lt;BR /&gt;5 MAY1999 55 121 31&lt;BR /&gt;6 JUN1999 61 114 31&lt;BR /&gt;7 JUL1999 67 102 72&lt;BR /&gt;8 AUG1999 64 76 72&lt;BR /&gt;9 JUL1999 78 77 54&lt;BR /&gt;10 JUL1999 81 62 47&lt;BR /&gt;11 JUL1999 84 31 52&lt;BR /&gt;12 JUL1999 2 44 55&lt;BR /&gt;13 JAN2000 37 91 83&lt;BR /&gt;14 FEB2000 41 102 27&lt;BR /&gt;15 FEB2000 52 98 19&lt;BR /&gt;16 FEB2000 61 118 22&lt;BR /&gt;17 FEB2000 49 88 29&lt;BR /&gt;18 FEB2000 24 101 54&lt;BR /&gt;19 JUL2000 45 91 69&lt;BR /&gt;20 JUL2000 63 65 53&lt;BR /&gt;21 SEP2000 60 49 68&lt;BR /&gt;22 JUL2000 78 70 41&lt;BR /&gt;23 JUL2000 82 44 58&lt;BR /&gt;24 JUL2000 93 57 47&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Firstly i have converted the date column using monyy7. format then i&amp;nbsp; have tried this code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data Ques3;&lt;BR /&gt;Set Tempdata1;&lt;BR /&gt;By Date1;&lt;BR /&gt;Retain MaxM ;&lt;BR /&gt;IF First.Date1=1 THEN MaxM = MAX( Metric2, MaxM);&lt;BR /&gt;Else MaxM= Metric2;&lt;BR /&gt;If Last.Date1=1;&lt;BR /&gt;Run;&lt;/P&gt;&lt;P&gt;But this code gives me only the highest values for every date&lt;/P&gt;&lt;P&gt;Plz suggest&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 19:57:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-the-highest-two-records-of-Metrc2-for-each-date/m-p/497108#M131644</guid>
      <dc:creator>Nidhi_vats</dc:creator>
      <dc:date>2018-09-19T19:57:32Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate the highest two records of Metrc2 for each date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-the-highest-two-records-of-Metrc2-for-each-date/m-p/497112#M131645</link>
      <description>&lt;P&gt;post the expected output for the sample plz&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 20:12:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-the-highest-two-records-of-Metrc2-for-each-date/m-p/497112#M131645</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-09-19T20:12:52Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate the highest two records of Metrc2 for each date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-the-highest-two-records-of-Metrc2-for-each-date/m-p/497115#M131648</link>
      <description>&lt;P&gt;your output is because you did not tell the process to output the records.&lt;/P&gt;&lt;P&gt;only the last record for the date, where&lt;SPAN&gt; Last.Date1=1;&lt;/SPAN&gt; allow the record to be recorded.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 20:15:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-the-highest-two-records-of-Metrc2-for-each-date/m-p/497115#M131648</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2018-09-19T20:15:53Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate the highest two records of Metrc2 for each date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-the-highest-two-records-of-Metrc2-for-each-date/m-p/497123#M131653</link>
      <description>&lt;P&gt;My code gives me the higest value of matric2 for every date. But i want two highest values for every date. Like for FEB1999 it should gives the values 22 and 14.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 20:27:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-the-highest-two-records-of-Metrc2-for-each-date/m-p/497123#M131653</guid>
      <dc:creator>Nidhi_vats</dc:creator>
      <dc:date>2018-09-19T20:27:11Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate the highest two records of Metrc2 for each date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-the-highest-two-records-of-Metrc2-for-each-date/m-p/497128#M131654</link>
      <description>&lt;P&gt;see if this helps&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data tempdata;

Input obs Date $ Age Salary Metric2;
Datalines;
1 JAN1999 56 78 14
2 FEB1999 32 109 14
3 FEB1999 35 106 14
4 FEB1999 47 115 22
5 MAY1999 55 121 31
6 JUN1999 61 114 31
7 JUL1999 67 102 72
8 AUG1999 64 76 72
9 JUL1999 78 77 54
10 JUL1999 81 62 47
11 JUL1999 84 31 52
12 JUL1999 2 44 55
13 JAN2000 37 91 83
14 FEB2000 41 102 27
15 FEB2000 52 98 19
16 FEB2000 61 118 22
17 FEB2000 49 88 29
18 FEB2000 24 101 54
19 JUL2000 45 91 69
20 JUL2000 63 65 53
21 SEP2000 60 49 68
22 JUL2000 78 70 41
23 JUL2000 82 44 58
24 JUL2000 93 57 47
;

proc rank data=tempdata out=want(where=(Metric2rank in (1,2))) ties=low descending;
	
   by Date notsorted;
 
   var Metric2;
   ranks Metric2rank ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Sep 2018 20:32:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-the-highest-two-records-of-Metrc2-for-each-date/m-p/497128#M131654</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-09-19T20:32:25Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate the highest two records of Metrc2 for each date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-the-highest-two-records-of-Metrc2-for-each-date/m-p/497131#M131656</link>
      <description>&lt;P&gt;Is there any way that we can solve this using Group processing?&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 20:37:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-the-highest-two-records-of-Metrc2-for-each-date/m-p/497131#M131656</guid>
      <dc:creator>Nidhi_vats</dc:creator>
      <dc:date>2018-09-19T20:37:37Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate the highest two records of Metrc2 for each date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-the-highest-two-records-of-Metrc2-for-each-date/m-p/497135#M131658</link>
      <description>&lt;P&gt;One way with a data set, different sort order. I made the dates actual date values so the sort order makes sense with them.&lt;/P&gt;
&lt;PRE&gt;Data tempdata;

Input obs Date :monyy7. Age Salary Metric2;
format date monyy7.;
Datalines;
1 JAN1999 56 78 14
2 FEB1999 32 109 14
3 FEB1999 35 106 14
4 FEB1999 47 115 22
5 MAY1999 55 121 31
6 JUN1999 61 114 31
7 JUL1999 67 102 72
8 AUG1999 64 76 72
9 JUL1999 78 77 54
10 JUL1999 81 62 47
11 JUL1999 84 31 52
12 JUL1999 2 44 55
13 JAN2000 37 91 83
14 FEB2000 41 102 27
15 FEB2000 52 98 19
16 FEB2000 61 118 22
17 FEB2000 49 88 29
18 FEB2000 24 101 54
19 JUL2000 45 91 69
20 JUL2000 63 65 53
21 SEP2000 60 49 68
22 JUL2000 78 70 41
23 JUL2000 82 44 58
24 JUL2000 93 57 47
;
run;

proc sort data=tempdata;
   by date descending metric2;
run;
data want;
  set tempdata;
  by date descending metric2;
  retain flag;
  if first.date then flag=1;
  else flag+1;
  if flag le 2;
  drop flag;
run;
&lt;/PRE&gt;
&lt;P&gt;There are some details you may want to address. Such as if you have 3 identical metric2 values for a date. Do you want 1 (unique value of metric) 2 (ordered) or 3 (keep ties) in the output. Or if the second largest value has a tie with the third.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 20:43:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-the-highest-two-records-of-Metrc2-for-each-date/m-p/497135#M131658</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-09-19T20:43:14Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate the highest two records of Metrc2 for each date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-the-highest-two-records-of-Metrc2-for-each-date/m-p/497142#M131663</link>
      <description>&lt;P&gt;Thank u .. I want my output to be like this .&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 20:49:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-the-highest-two-records-of-Metrc2-for-each-date/m-p/497142#M131663</guid>
      <dc:creator>Nidhi_vats</dc:creator>
      <dc:date>2018-09-19T20:49:57Z</dc:date>
    </item>
  </channel>
</rss>

