<?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: find min and min within two unique values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/find-min-and-min-within-two-unique-values/m-p/561905#M157365</link>
    <description>&lt;P&gt;Does the order of the observations matter? Are we allowed to sort the data?&lt;/P&gt;</description>
    <pubDate>Tue, 28 May 2019 10:33:54 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2019-05-28T10:33:54Z</dc:date>
    <item>
      <title>find min and min within two unique values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-min-and-min-within-two-unique-values/m-p/561896#M157362</link>
      <description>&lt;P&gt;in the following dataset i want to derive a new variable 'TYPE' in which the max and min of val2 within id for each val1 should be populated like&lt;/P&gt;&lt;P&gt;ID VAL1 VAL2&lt;BR /&gt;001 HT 170&lt;BR /&gt;001 HT 170&lt;BR /&gt;001 HT 170&lt;BR /&gt;001 WT 56&lt;BR /&gt;001 WT 78&lt;BR /&gt;001 WT 91&lt;BR /&gt;001 TM 32&lt;BR /&gt;001 TM 31&lt;BR /&gt;001 TM 27&lt;BR /&gt;001 BP 120&lt;BR /&gt;001 BP 111&lt;BR /&gt;001 BP 178&lt;BR /&gt;002 HT 169&lt;BR /&gt;002 HT 169&lt;BR /&gt;002 HT 169&lt;BR /&gt;002 WT 34&lt;BR /&gt;002 WT 89&lt;BR /&gt;002 WT 90&lt;/P&gt;&lt;P&gt;if the val2 is constant for any val2(here HT) I have to keep only one record for that .the output should be like;&lt;/P&gt;&lt;P&gt;ID VAL1 VAL2 TYPE&lt;BR /&gt;001 HT 170&lt;BR /&gt;001 WT 56 min&lt;BR /&gt;001 WT 78&lt;BR /&gt;001 WT 91 max&lt;BR /&gt;001 TM 32 max&lt;BR /&gt;001 TM 31&lt;BR /&gt;001 TM 27 min&lt;BR /&gt;001 BP 120&lt;BR /&gt;001 BP 111 min&lt;BR /&gt;001 BP 178 max&lt;BR /&gt;002 HT 169&lt;BR /&gt;002 WT 34 min&lt;BR /&gt;002 WT 89&lt;BR /&gt;002 WT 90 max&lt;/P&gt;&lt;P&gt;please help.&lt;/P&gt;</description>
      <pubDate>Tue, 28 May 2019 10:03:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-min-and-min-within-two-unique-values/m-p/561896#M157362</guid>
      <dc:creator>AKHILA</dc:creator>
      <dc:date>2019-05-28T10:03:17Z</dc:date>
    </item>
    <item>
      <title>Re: find min and min within two unique values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-min-and-min-within-two-unique-values/m-p/561905#M157365</link>
      <description>&lt;P&gt;Does the order of the observations matter? Are we allowed to sort the data?&lt;/P&gt;</description>
      <pubDate>Tue, 28 May 2019 10:33:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-min-and-min-within-two-unique-values/m-p/561905#M157365</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-05-28T10:33:54Z</dc:date>
    </item>
    <item>
      <title>Re: find min and min within two unique values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-min-and-min-within-two-unique-values/m-p/561907#M157367</link>
      <description>&lt;P&gt;Does the following give you what you want:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input id     $3.
         val1 : $2.
         val2 :  8.
   ;

   datalines;
001 HT 170
001 HT 170
001 HT 170
001 WT 56
001 WT 78
001 WT 91
001 TM 32
001 TM 31
001 TM 27
001 BP 120
001 BP 111
001 BP 178
002 HT 169
002 HT 169
002 HT 169
002 WT 34
002 WT 89
002 WT 90
;

/* process using double DOW loop */
data want(drop = minimum maximum done_constant);
   /* work out min and max for this val1 group */
   do until(last.val1);
      set have;
      by id val1 notsorted;

      minimum = min(minimum,val2);
      maximum = max(maximum,val2);
   end;

   /* work out if this val1 record is constant, else assign 'type' */
   do until(last.val1);
      set have;
      by id val1 notsorted;

      if minimum = maximum then
      do;
         if not done_constant then
         do;
            output;
            done_constant = 1;
         end;
      end;
      else
      do;
         type = ifc(val2 = minimum,'min',ifc(val2 = maximum,'max',''));
         output;
      end;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Tue, 28 May 2019 10:35:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-min-and-min-within-two-unique-values/m-p/561907#M157367</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2019-05-28T10:35:00Z</dc:date>
    </item>
    <item>
      <title>Re: find min and min within two unique values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-min-and-min-within-two-unique-values/m-p/561977#M157387</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA want(drop=min);
   set have end=last;
   by id val1 notsorted;
   retain min .;
   if first.val1 then do;
      type='min';
      min=val2;
   end;
   if last.val1 then do;
      type='max';
      if min eq val2 then call execute('PROC SQL;UPDATE want SET type="" WHERE id eq "'||strip(id)||'" AND val1 eq "'||strip(val1)||'" AND val2 eq '||strip(put(val2,best.))||';QUIT;');
   end;
RUN;

PROC SORT data=want nodupkey;by id val1 val2 type; RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 May 2019 14:17:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-min-and-min-within-two-unique-values/m-p/561977#M157387</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2019-05-28T14:17:58Z</dc:date>
    </item>
  </channel>
</rss>

