<?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: Monotonicity and equality check across rows by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Monotonicity-and-equality-check-across-rows-by-group/m-p/578840#M164240</link>
    <description>&lt;P&gt;Help me understand where my logic differs from yours:&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 id group descending var;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The makes it so that the data properly organized by id and group.&amp;nbsp; "descending var" means that the first value of var for the id+group combination is going to be the largest in the group.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   retain new_var 0;
   new_var = max(var, new_var);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This data step does both of your rules at once.&amp;nbsp; Since the largest value of the group is first, the max function will assign all of the same id+group combinations the same value.&amp;nbsp; Since max is a monotonic function, new_var will only increase when the next group has a larger value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Perhaps you want new_var to reset for each id, then we need to add some extra code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;&lt;BR /&gt;   by id;
   retain new_var;
   new_var = ifn(first.id, var, max(var, new_var));
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Now the data step will set the value of new_var to var at the first row of an id, and then use the larger of var and new_var thereafter.&lt;/P&gt;</description>
    <pubDate>Fri, 02 Aug 2019 21:47:55 GMT</pubDate>
    <dc:creator>Urban_Science</dc:creator>
    <dc:date>2019-08-02T21:47:55Z</dc:date>
    <item>
      <title>Monotonicity and equality check across rows by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Monotonicity-and-equality-check-across-rows-by-group/m-p/578811#M164228</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;suppose I have dataset looks like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Have;
INPUT id $ group $ var ;
Cards;
AAA group1 0.1
AAA group2 0.2
AAA group2 0.2
AAA group3 0.3
AAA group3 0.2
AAA group3 0.3
AAA group4 0.4
AAA group4 0.4
AAA group5 0.3
BBB group1 0.4
BBB group2 0.3
BBB group2 0.2
BBB group3 0.3
BBB group3 0.3
BBB group3 0.2
BBB group4 0.3
BBB group4 0.2
BBB group5 0.3
;
RUN;

Data WANT;
INPUT id $ group $ var new_var;
Cards;
AAA group1 0.1 0.1
AAA group2 0.2 0.2
AAA group2 0.2 0.2
AAA group3 0.3 0.3
AAA group3 0.2 0.3
AAA group3 0.3 0.3
AAA group4 0.4 0.4
AAA group4 0.4 0.4
AAA group5 0.3 0.4
BBB group1 0.4 0.4
BBB group2 0.3 0.4
BBB group2 0.2 0.4
BBB group3 0.3 0.4
BBB group3 0.3 0.4
BBB group3 0.2 0.4
BBB group4 0.3 0.4
BBB group4 0.2 0.4
BBB group5 0.3 0.4
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;in order to obtain the want data, I have 2 rules to check:&lt;/P&gt;&lt;P&gt;1. var should be monotonically increase from group 1 to group 5 for each id.&lt;/P&gt;&lt;P&gt;2. within the same group, var should be forced to equal to each other.&lt;/P&gt;&lt;P&gt;3. to fix the violation, I need to generate this new_var, and it should be always scanned from top.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please advise how to do this in SAS, thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 20:37:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Monotonicity-and-equality-check-across-rows-by-group/m-p/578811#M164228</guid>
      <dc:creator>lpy0521</dc:creator>
      <dc:date>2019-08-02T20:37:19Z</dc:date>
    </item>
    <item>
      <title>Re: Monotonicity and equality check across rows by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Monotonicity-and-equality-check-across-rows-by-group/m-p/578820#M164234</link>
      <description>&lt;P&gt;Here is a shot in the dark for you.&amp;nbsp; This appears to solve your problem:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data = have;
   by id group descending var;
run;

data want;
   set have;
   retain new_var 0;
   new_var = max(var, new_var);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Aug 2019 20:34:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Monotonicity-and-equality-check-across-rows-by-group/m-p/578820#M164234</guid>
      <dc:creator>Urban_Science</dc:creator>
      <dc:date>2019-08-02T20:34:01Z</dc:date>
    </item>
    <item>
      <title>Re: Monotonicity and equality check across rows by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Monotonicity-and-equality-check-across-rows-by-group/m-p/578822#M164235</link>
      <description>&lt;P&gt;Sorry but I may create some confusion here. New_var doesn't exist, I actually need to generate it by following the check rules.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 20:38:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Monotonicity-and-equality-check-across-rows-by-group/m-p/578822#M164235</guid>
      <dc:creator>lpy0521</dc:creator>
      <dc:date>2019-08-02T20:38:18Z</dc:date>
    </item>
    <item>
      <title>Re: Monotonicity and equality check across rows by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Monotonicity-and-equality-check-across-rows-by-group/m-p/578826#M164236</link>
      <description>&lt;P&gt;The lines:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain new_var 0;
new_var = max(var, new_var);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;first create new_var, setting its value to 0 and it tells SAS that we want to keep the value of new_var when we move to the next row. Then the second line tells SAS to compare new_var's value with the current row's value for var and assign new_var the larger of the two. The result of my data step produces an identical data sets.&amp;nbsp; Validation code here:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Have;
INPUT id $ group $ var ;
Cards;
AAA group1 0.1
AAA group2 0.2
AAA group2 0.2
AAA group3 0.3
AAA group3 0.2
AAA group3 0.3
AAA group4 0.4
AAA group4 0.4
AAA group5 0.3
BBB group1 0.4
BBB group2 0.3
BBB group2 0.2
BBB group3 0.3
BBB group3 0.3
BBB group3 0.2
BBB group4 0.3
BBB group4 0.2
BBB group5 0.3
;
RUN;

proc sort data = have;
   by id group descending var;
run;

data want;
   set have;
   retain new_var 0;
   new_var = max(var, new_var);
run;

Data WANT2;
INPUT id $ group $ var new_var;
Cards;
AAA group1 0.1 0.1
AAA group2 0.2 0.2
AAA group2 0.2 0.2
AAA group3 0.3 0.3
AAA group3 0.2 0.3
AAA group3 0.3 0.3
AAA group4 0.4 0.4
AAA group4 0.4 0.4
AAA group5 0.3 0.4
BBB group1 0.4 0.4
BBB group2 0.3 0.4
BBB group2 0.2 0.4
BBB group3 0.3 0.4
BBB group3 0.3 0.4
BBB group3 0.2 0.4
BBB group4 0.3 0.4
BBB group4 0.2 0.4
BBB group5 0.3 0.4
;
RUN;

proc sort data = WANT2;
   by id group descending var;
run;

proc compare base = want comp = want2;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/31483i8F7BC3E00E119C0E/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 20:43:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Monotonicity-and-equality-check-across-rows-by-group/m-p/578826#M164236</guid>
      <dc:creator>Urban_Science</dc:creator>
      <dc:date>2019-08-02T20:43:31Z</dc:date>
    </item>
    <item>
      <title>Re: Monotonicity and equality check across rows by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Monotonicity-and-equality-check-across-rows-by-group/m-p/578828#M164237</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/230703"&gt;@lpy0521&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi there,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;suppose I have dataset looks like this:&lt;/P&gt;
&lt;P&gt;in order to obtain the want data, I have 2 rules to check:&lt;/P&gt;
&lt;P&gt;1. var should be monotonically increase from group 1 to group 5 for each id.&lt;/P&gt;
&lt;P&gt;2. within the same group, var should be forced to equal to each other.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please advise how to do this in SAS, thanks!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I don't think 1 and 2 make much sense. "forced to equal" is going to have an issue with "increase". I think may be missing a bit of either your description or the rules. As written it appears that you want a single variable to have the same value for multiple records.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 20:46:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Monotonicity-and-equality-check-across-rows-by-group/m-p/578828#M164237</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-08-02T20:46:22Z</dc:date>
    </item>
    <item>
      <title>Re: Monotonicity and equality check across rows by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Monotonicity-and-equality-check-across-rows-by-group/m-p/578831#M164238</link>
      <description>I think what Ipy0521 has a typo there. For both rules, it should be new_var instead of var. And specifically for 2, I think it should be "new_var should be the same value for the same id+group pair, e.g. both rows with id = AAA and group=group2 have new_val=0.2"</description>
      <pubDate>Fri, 02 Aug 2019 21:00:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Monotonicity-and-equality-check-across-rows-by-group/m-p/578831#M164238</guid>
      <dc:creator>Urban_Science</dc:creator>
      <dc:date>2019-08-02T21:00:33Z</dc:date>
    </item>
    <item>
      <title>Re: Monotonicity and equality check across rows by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Monotonicity-and-equality-check-across-rows-by-group/m-p/578837#M164239</link>
      <description>&lt;P&gt;I may not be very clear here. I want my rule to be applied in sequential order, that has been saying:&lt;/P&gt;&lt;P&gt;1. we first check for each id, within each group, make sure all var in this group is equal. (may using most frequent ones to replace the other or such).&lt;/P&gt;&lt;P&gt;2.&amp;nbsp; once we make sure all values within each group are all same, we then next check&amp;nbsp;var_group1 &amp;lt; var_group2 &amp;lt; var_group3 &amp;lt; var_group4 &amp;lt; var_group5&amp;nbsp; is true. (i.e., 0.1 0.2 0.3 0.4 0.5). If we find something like (0.1 0.2 0.1 0.3 0.4) then it need to be modified as (0.1, 0.2,0.2,0.2,0.2);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So here I would like to prioritize within group rule first and then monotonic by group.&lt;/P&gt;&lt;P&gt;Hope this is clear.&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 21:26:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Monotonicity-and-equality-check-across-rows-by-group/m-p/578837#M164239</guid>
      <dc:creator>lpy0521</dc:creator>
      <dc:date>2019-08-02T21:26:59Z</dc:date>
    </item>
    <item>
      <title>Re: Monotonicity and equality check across rows by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Monotonicity-and-equality-check-across-rows-by-group/m-p/578840#M164240</link>
      <description>&lt;P&gt;Help me understand where my logic differs from yours:&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 id group descending var;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The makes it so that the data properly organized by id and group.&amp;nbsp; "descending var" means that the first value of var for the id+group combination is going to be the largest in the group.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   retain new_var 0;
   new_var = max(var, new_var);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This data step does both of your rules at once.&amp;nbsp; Since the largest value of the group is first, the max function will assign all of the same id+group combinations the same value.&amp;nbsp; Since max is a monotonic function, new_var will only increase when the next group has a larger value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Perhaps you want new_var to reset for each id, then we need to add some extra code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;&lt;BR /&gt;   by id;
   retain new_var;
   new_var = ifn(first.id, var, max(var, new_var));
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Now the data step will set the value of new_var to var at the first row of an id, and then use the larger of var and new_var thereafter.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 21:47:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Monotonicity-and-equality-check-across-rows-by-group/m-p/578840#M164240</guid>
      <dc:creator>Urban_Science</dc:creator>
      <dc:date>2019-08-02T21:47:55Z</dc:date>
    </item>
    <item>
      <title>Re: Monotonicity and equality check across rows by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Monotonicity-and-equality-check-across-rows-by-group/m-p/578869#M164248</link>
      <description>&lt;P&gt;The only tricky part is "&lt;EM&gt;may using most frequent ones to replace the other or such&lt;/EM&gt;". The most common value of a set is the mode which is not defined when all values are different (as with BBB group2). I chose to use the maximum value when the mode is undefined:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Have;
INPUT id $ group $ var ;
Cards;
AAA group1 0.1
AAA group2 0.2
AAA group2 0.2
AAA group3 0.3
AAA group3 0.2
AAA group3 0.3
AAA group4 0.4
AAA group4 0.4
AAA group5 0.3
BBB group1 0.4
BBB group2 0.3
BBB group2 0.2
BBB group3 0.3
BBB group3 0.3
BBB group3 0.2
BBB group4 0.3
BBB group4 0.2
BBB group5 0.3
;

proc sort data = have;
   by id group;
run;

proc means data=have noprint;
by id group;
output out=modes(drop=_:) mode=mode max=max;
run;

data want;
merge have modes; by id group;
retain newvar;
if first.id then newvar = coalesce(mode, max);
else if first.group then newvar = max(newvar, coalesce(mode, max));
drop mode max;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 03 Aug 2019 04:41:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Monotonicity-and-equality-check-across-rows-by-group/m-p/578869#M164248</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-08-03T04:41:16Z</dc:date>
    </item>
  </channel>
</rss>

