<?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 to flag first occurrence of maximum score? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-occurrence-of-maximum-score/m-p/672173#M201973</link>
    <description>&lt;P&gt;Here's a somewhat more complex solution.&amp;nbsp; Why go with complexity?&amp;nbsp; Well, it may be that you don't really want to sort your data in SCORE order.&amp;nbsp; Perhaps you want to keep it in order by SUBJ DATE the whole time:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
   by subj date;
run;

data want;
   do until (last.subj);
      set have;
      by subj;
      max_score = max(score, max_score);
   end;
   found_max='N';
   do until (last.subj);
      set have;
      by subj;
      if found_max='N' and score=max_score then do;
         flag=1;
         found_max='Y';
      end;&lt;BR /&gt;      else flag=0;
      output;
   end;
   drop max_score found_max;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The top and bottom loops both read all observations for the same SUBJ.&amp;nbsp; The top loop finds the maximum SCORE for that SUBJ, and the bottom loop calculates FLAG and outputs observations.&lt;/P&gt;</description>
    <pubDate>Fri, 24 Jul 2020 19:44:50 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2020-07-24T19:44:50Z</dc:date>
    <item>
      <title>How to flag first occurrence of maximum score?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-occurrence-of-maximum-score/m-p/672047#M201901</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a data set with following records where I have to flag the first occurrence of maximum score.I have used the following code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=ex;&lt;/P&gt;
&lt;P&gt;by subj score;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data ex_t;&lt;/P&gt;
&lt;P&gt;set ex;&lt;/P&gt;
&lt;P&gt;by subj score;&lt;/P&gt;
&lt;P&gt;if last.subj then flag=1;&lt;/P&gt;
&lt;P&gt;else flag=0;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With the above code , the following records(highlighted) gets flagged&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="455px"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="149.6px" height="29px"&gt;
&lt;P&gt;SUBJ&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="148px" height="29px"&gt;
&lt;P&gt;Score&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="156px" height="29px"&gt;
&lt;P&gt;Date&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="149.6px" height="29px"&gt;
&lt;P&gt;004&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="148px" height="29px"&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="156px" height="29px"&gt;
&lt;P&gt;01FEB2018&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="149.6px" height="28px"&gt;
&lt;P&gt;&lt;FONT color="#FF6600"&gt;004&lt;/FONT&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="148px" height="28px"&gt;
&lt;P&gt;&lt;FONT color="#FF6600"&gt;1&lt;/FONT&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="156px" height="28px"&gt;
&lt;P&gt;&lt;FONT color="#FF6600"&gt;09MAR2018&lt;/FONT&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="149.6px" height="29px"&gt;
&lt;P&gt;005&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="148px" height="29px"&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="156px" height="29px"&gt;
&lt;P&gt;13APR17&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="149.6px" height="29px"&gt;
&lt;P&gt;005&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="148px" height="29px"&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="156px" height="29px"&gt;
&lt;P&gt;14JUN2017&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="149.6px" height="29px"&gt;
&lt;P&gt;&lt;FONT color="#FF6600"&gt;005&lt;/FONT&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="148px" height="29px"&gt;
&lt;P&gt;&lt;FONT color="#FF6600"&gt;2&lt;/FONT&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="156px" height="29px"&gt;
&lt;P&gt;&lt;FONT color="#FF6600"&gt;04APR17&lt;/FONT&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="149.6px" height="29px"&gt;
&lt;P&gt;006&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="148px" height="29px"&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="156px" height="29px"&gt;
&lt;P&gt;17 SEP2018&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="149.6px" height="29px"&gt;
&lt;P&gt;&lt;FONT color="#FF6600"&gt;006&lt;/FONT&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="148px" height="29px"&gt;
&lt;P&gt;&lt;FONT color="#FF6600"&gt;3&lt;/FONT&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="156px" height="29px"&gt;
&lt;P&gt;&lt;FONT color="#FF6600"&gt;12OCT2017&lt;/FONT&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But when the score is same for all records for a subject I need to get first occurrence&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i.e&amp;nbsp; in the above table , for subj 004 I need to get the first record flagged instead of second record.Is there any way I can do this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your replies are highly appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jul 2020 10:48:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-occurrence-of-maximum-score/m-p/672047#M201901</guid>
      <dc:creator>sri1</dc:creator>
      <dc:date>2020-07-24T10:48:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag first occurrence of maximum score?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-occurrence-of-maximum-score/m-p/672050#M201902</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.subj then flag=1;
else flag=0;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Jul 2020 10:58:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-occurrence-of-maximum-score/m-p/672050#M201902</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-07-24T10:58:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag first occurrence of maximum score?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-occurrence-of-maximum-score/m-p/672051#M201903</link>
      <description>Then for subjects 005 and 006, will not get the records with maximum score</description>
      <pubDate>Fri, 24 Jul 2020 11:00:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-occurrence-of-maximum-score/m-p/672051#M201903</guid>
      <dc:creator>sri1</dc:creator>
      <dc:date>2020-07-24T11:00:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag first occurrence of maximum score?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-occurrence-of-maximum-score/m-p/672057#M201905</link>
      <description>proc sort data=ex;&lt;BR /&gt;by subj  descending score;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt; &lt;BR /&gt;&lt;BR /&gt;data ex_t;&lt;BR /&gt;set ex;&lt;BR /&gt;by subj ;&lt;BR /&gt;if first.subj then flag=1;&lt;BR /&gt;else flag=0;&lt;BR /&gt;run;</description>
      <pubDate>Fri, 24 Jul 2020 11:34:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-occurrence-of-maximum-score/m-p/672057#M201905</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-07-24T11:34:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag first occurrence of maximum score?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-occurrence-of-maximum-score/m-p/672109#M201937</link>
      <description>&lt;P&gt;Just add date to your sorting order if what you want is the first date with maximum score:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=ex;
by subj descending score date;
run;

data ex_t;
set ex;
by subj ;
flag = first.subj;
run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Jul 2020 15:04:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-occurrence-of-maximum-score/m-p/672109#M201937</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-07-24T15:04:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag first occurrence of maximum score?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-occurrence-of-maximum-score/m-p/672173#M201973</link>
      <description>&lt;P&gt;Here's a somewhat more complex solution.&amp;nbsp; Why go with complexity?&amp;nbsp; Well, it may be that you don't really want to sort your data in SCORE order.&amp;nbsp; Perhaps you want to keep it in order by SUBJ DATE the whole time:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
   by subj date;
run;

data want;
   do until (last.subj);
      set have;
      by subj;
      max_score = max(score, max_score);
   end;
   found_max='N';
   do until (last.subj);
      set have;
      by subj;
      if found_max='N' and score=max_score then do;
         flag=1;
         found_max='Y';
      end;&lt;BR /&gt;      else flag=0;
      output;
   end;
   drop max_score found_max;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The top and bottom loops both read all observations for the same SUBJ.&amp;nbsp; The top loop finds the maximum SCORE for that SUBJ, and the bottom loop calculates FLAG and outputs observations.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jul 2020 19:44:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-first-occurrence-of-maximum-score/m-p/672173#M201973</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-07-24T19:44:50Z</dc:date>
    </item>
  </channel>
</rss>

