<?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 Grouping rows of the same sign into 1 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Grouping-rows-of-the-same-sign-into-1/m-p/850422#M336105</link>
    <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;My data below has id, sign (+1/-1) and value and already in order of time.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The sign supposed to be in the +1 -1 +1 -1 sequence. However, there are rows having the same sign (+1 +1 +1).&lt;/P&gt;
&lt;P&gt;Thus I need to keep only 1 row to restore the right sequence rule.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So for +1+1+1..+1, I only keep the row with the &lt;U&gt;highest&lt;/U&gt; value. This one below, I want to keep&amp;nbsp;&lt;CODE class=" language-sas"&gt;2 3 1 8&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;2 1 1 0
2 2 1 5
2 3 1 8&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And for -1-1-1...-1, I want to keep the row with the &lt;U&gt;lowest&lt;/U&gt; value. This one below, I want to keep&amp;nbsp;&lt;CODE class=" language-sas"&gt;1 5 -1 5&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;1 4 -1 8
1 5 -1 5
1 6 -1 7&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Can you help me with this problem?&lt;/P&gt;
&lt;P&gt;Many thanks,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id time sign value;
datalines;
1 1 1 9
1 2 -1 -8
1 3 1 4
1 4 -1 8
1 5 -1 5
1 6 -1 7
2 1 1 0
2 2 1 5
2 3 1 8
2 4 -1 5
2 5 1 4
2 6 -1 3
2 7 1 5
;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 20 Dec 2022 03:21:20 GMT</pubDate>
    <dc:creator>hhchenfx</dc:creator>
    <dc:date>2022-12-20T03:21:20Z</dc:date>
    <item>
      <title>Grouping rows of the same sign into 1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grouping-rows-of-the-same-sign-into-1/m-p/850422#M336105</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;My data below has id, sign (+1/-1) and value and already in order of time.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The sign supposed to be in the +1 -1 +1 -1 sequence. However, there are rows having the same sign (+1 +1 +1).&lt;/P&gt;
&lt;P&gt;Thus I need to keep only 1 row to restore the right sequence rule.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So for +1+1+1..+1, I only keep the row with the &lt;U&gt;highest&lt;/U&gt; value. This one below, I want to keep&amp;nbsp;&lt;CODE class=" language-sas"&gt;2 3 1 8&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;2 1 1 0
2 2 1 5
2 3 1 8&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And for -1-1-1...-1, I want to keep the row with the &lt;U&gt;lowest&lt;/U&gt; value. This one below, I want to keep&amp;nbsp;&lt;CODE class=" language-sas"&gt;1 5 -1 5&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;1 4 -1 8
1 5 -1 5
1 6 -1 7&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Can you help me with this problem?&lt;/P&gt;
&lt;P&gt;Many thanks,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id time sign value;
datalines;
1 1 1 9
1 2 -1 -8
1 3 1 4
1 4 -1 8
1 5 -1 5
1 6 -1 7
2 1 1 0
2 2 1 5
2 3 1 8
2 4 -1 5
2 5 1 4
2 6 -1 3
2 7 1 5
;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Dec 2022 03:21:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grouping-rows-of-the-same-sign-into-1/m-p/850422#M336105</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-12-20T03:21:20Z</dc:date>
    </item>
    <item>
      <title>Re: Grouping rows of the same sign into 1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grouping-rows-of-the-same-sign-into-1/m-p/850423#M336106</link>
      <description>&lt;P&gt;You can use BY group processing to define the alternating groups.&lt;/P&gt;
&lt;P&gt;Then you can sort.&amp;nbsp; To make so you can use one sort for both types of groups just multiply the sign and the value.&amp;nbsp; Then for both you want the maximum value since the negative number with the smaller absolute value is larger than a negative number with a larger absolute value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id sign notsorted;
  if first.id then group=0;
  group + first.sign;
  number = sign * value;
run;

proc sort;
  by id group number ;
run;

data want;
  set want ;
  by id group;
  if first.group;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Dec 2022 04:47:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grouping-rows-of-the-same-sign-into-1/m-p/850423#M336106</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-12-20T04:47:06Z</dc:date>
    </item>
    <item>
      <title>Re: Grouping rows of the same sign into 1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grouping-rows-of-the-same-sign-into-1/m-p/850456#M336111</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id time sign value;
datalines;
1 1 1 9
1 2 -1 -8
1 3 1 4
1 4 -1 8
1 5 -1 5
1 6 -1 7
2 1 1 0
2 2 1 5
2 3 1 8
2 4 -1 5
2 5 1 4
2 6 -1 3
2 7 1 5
;run;

data want;
min=constant('exactint');max=.;
 do until(last.sign);
   set have;
   by id sign notsorted;
   if value&amp;lt;min then do;min=value;min_time=time;end;
   if value&amp;gt;max then do;max=value;max_time=time;end;
 end;
 if sign=1 then do;value=max;time=max_time;end;
 if sign=-1 then do;value=min;time=min_time;end;
 drop min max min_time max_time;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Dec 2022 08:48:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grouping-rows-of-the-same-sign-into-1/m-p/850456#M336111</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-12-20T08:48:27Z</dc:date>
    </item>
    <item>
      <title>Re: Grouping rows of the same sign into 1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grouping-rows-of-the-same-sign-into-1/m-p/850532#M336136</link>
      <description>&lt;P&gt;Thank you for helping!&lt;/P&gt;
&lt;P&gt;Such an amazing piece of code.&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
      <pubDate>Tue, 20 Dec 2022 19:51:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grouping-rows-of-the-same-sign-into-1/m-p/850532#M336136</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-12-20T19:51:10Z</dc:date>
    </item>
  </channel>
</rss>

